Help java file manipulation.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    Originally posted by jeffbroodwar
    Still.... it creates 0kb file..... ^^; eheheh i know it's easy for you guys.... sorry. i'm still new in java i guess....
    Can you post the full class that you're using?

    Comment

    • jeffbroodwar
      New Member
      • Oct 2006
      • 118

      #17
      [CODE=java]import java.io.Buffere dInputStream;
      import java.io.Buffere dOutputStream;
      import java.io.FileInp utStream;
      import java.io.FileOut putStream;
      import java.io.IOExcep tion;
      import javax.swing.JOp tionPane;


      // This code goes inside the button's action performed //


      // Paste a file in a folder Button
      String saveVariable = null;
      //READ A FILE
      try
      {
      FileInputStream fin = new FileInputStream ("SampleWebServ ice.war");
      BufferedInputSt ream bin = new BufferedInputSt ream(fin);
      int ch=0;

      while((ch=bin.r ead())> -1)
      {
      StringBuffer buf = new StringBuffer();
      buf.append((cha r)ch);
      System.out.prin t(buf.toString( ));
      saveVariable = String.valueOf( buf);
      }

      //WRITE A FILE
      try
      {

      FileOutputStrea m fos = new FileOutputStrea m("Sample.war") ;

      BufferedOutputS tream bos = new BufferedOutputS tream(fos);

      while(ch != -1)
      {
      bos.write(ch);
      ch = bin.read();

      }
      bos.close();

      }
      catch (Exception e)
      {
      System.out.prin tln("Exception: " + e);
      }

      }
      catch(IOExcepti on ex)
      {
      System.out.prin tln(ex.getMessa ge());
      }
      // END OF CODE //[/CODE]

      Hope we can figure it out.... thanks.

      Best Regards,
      Jeff
      Last edited by r035198x; Jun 13 '07, 10:40 AM. Reason: added code tags

      Comment

      • jeffbroodwar
        New Member
        • Oct 2006
        • 118

        #18
        incase i can't give a reply, i'll try to be online again tomorrow, but if i'll have a solution fast enough i might be able to make this thing work now. thanks in advance for all your help guys. specially r035198x. I ran out of money... i'm in a net cafe. thanks, hope i can make it work...... i'll still wait for your suggestions and hoping to make this thing work before they cut my connection. err...... thanks

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #19
          1.) Please use code tags each time when posting code.
          2.) Do you want to copy a file from one location to another or do you want to rename a file because you are using the same directory (current) in your code?
          3.) Here's the code that can copy a file for you

          [CODE=java]public static void copy(String fromPath, String toPath) {
          try {
          FileInputStream fis = new FileInputStream (fromPath);
          BufferedInputSt ream bis = new BufferedInputSt ream(fis);
          FileOutputStrea m fos = new FileOutputStrea m(toPath);
          BufferedOutputS tream bos = new BufferedOutputS tream(fos);
          int val = bis.read();
          while(val != -1) {
          bos.write(val);
          val = bis.read();
          }

          }
          catch(Exception e) {
          e.printStackTra ce();
          }
          finally {
          bis.close();
          bos.close();
          }
          }[/CODE]

          Putting it in a method like this allows me to call it from anywhere with any arguments I want.

          Comment

          • jeffbroodwar
            New Member
            • Oct 2006
            • 118

            #20
            Thanks man, it worked not yet late !!! Ehehehehe..... though the finally didn't work it says : cannot find symbol... i'm using netbeans 5.5. i don't know why it can't detect the initialization above... anyway. i'll try to figure it out myself. it's now working thanks again r035198x and jos. about the code format, sorry i didn't have the time to make it neat, i'm really in a hurry. thanks again !!


            Best Regards,
            Jeff

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #21
              Originally posted by jeffbroodwar
              Thanks man, it worked not yet late !!! Ehehehehe..... though the finally didn't work it says : cannot find symbol... i'm using netbeans 5.5. i don't know why it can't detect the initialization above... anyway. i'll try to figure it out myself. it's now working thanks again r035198x and jos. about the code format, sorry i didn't have the time to make it neat, i'm really in a hurry. thanks again !!


              Best Regards,
              Jeff
              That's not your netbeans' problem. That's my mistake in the code (so much for posting before testing and so much for trying to give someone all the code)

              You can change your method contents to


              [CODE=java]try {
              FileInputStream fis = new FileInputStream (fromPath);
              BufferedInputSt ream bis = new BufferedInputSt ream(fis);
              FileOutputStrea m fos = new FileOutputStrea m(toPath);
              BufferedOutputS tream bos = new BufferedOutputS tream(fos);
              int val = bis.read();
              while(val != -1) {
              bos.write(val);
              val = bis.read();
              }
              bis.close();
              bos.close();

              }
              catch(Exception e) {
              e.printStackTra ce();
              }[/CODE]

              P.S:I hope you realized why the code didn't work like that.

              Comment

              • jeffbroodwar
                New Member
                • Oct 2006
                • 118

                #22
                yup got it. I just thought that what you gave me doesn't have any errors.. I'm too confident that you've tested it before giving it to me. reason why i blamed myself for the error.......Tha t's a mistake on my part. Thanks again. ^^

                Comment

                • blazedaces
                  Contributor
                  • May 2007
                  • 284

                  #23
                  Originally posted by r035198x
                  That's not your netbeans' problem. That's my mistake in the code (so much for posting before testing and so much for trying to give someone all the code)

                  You can change your method contents to


                  [CODE=java]try {
                  FileInputStream fis = new FileInputStream (fromPath);
                  BufferedInputSt ream bis = new BufferedInputSt ream(fis);
                  FileOutputStrea m fos = new FileOutputStrea m(toPath);
                  BufferedOutputS tream bos = new BufferedOutputS tream(fos);
                  int val = bis.read();
                  while(val != -1) {
                  bos.write(val);
                  val = bis.read();
                  }
                  bis.close();
                  bos.close();

                  }
                  catch(Exception e) {
                  e.printStackTra ce();
                  }[/CODE]

                  P.S:I hope you realized why the code didn't work like that.
                  Just a tip since I was doing something like this earlier, add a finally parameter at the end for closing the streams like this:
                  [CODE=java]try {
                  FileInputStream fis = new FileInputStream (fromPath);
                  BufferedInputSt ream bis = new BufferedInputSt ream(fis);
                  FileOutputStrea m fos = new FileOutputStrea m(toPath);
                  BufferedOutputS tream bos = new BufferedOutputS tream(fos);
                  int val = bis.read();
                  while(val != -1) {
                  bos.write(val);
                  val = bis.read();
                  }

                  }
                  catch(Exception e) {
                  e.printStackTra ce();
                  }
                  finally {
                  bis.close();
                  bos.close();
                  }

                  [/CODE]

                  This way if you either interrupt the program or something goes wrong these don't stay open. If they do, you'll have to stop java.exe from running or it will keep going in the background at times...

                  Finally makes sure those are closed no matter what, either by interruption or exception. Finally is what will always happen following a try clause, no matter what.

                  Good luck,

                  -blazed

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by blazedaces
                    Just a tip since I was doing something like this earlier, add a finally parameter at the end for closing the streams like this:
                    [CODE=java]try {
                    FileInputStream fis = new FileInputStream (fromPath);
                    BufferedInputSt ream bis = new BufferedInputSt ream(fis);
                    FileOutputStrea m fos = new FileOutputStrea m(toPath);
                    BufferedOutputS tream bos = new BufferedOutputS tream(fos);
                    int val = bis.read();
                    while(val != -1) {
                    bos.write(val);
                    val = bis.read();
                    }

                    }
                    catch(Exception e) {
                    e.printStackTra ce();
                    }
                    finally {
                    bis.close();
                    bos.close();
                    }

                    [/CODE]

                    This way if you either interrupt the program or something goes wrong these don't stay open. If they do, you'll have to stop java.exe from running or it will keep going in the background at times...

                    Finally makes sure those are closed no matter what, either by interruption or exception. Finally is what will always happen following a try clause, no matter what.

                    Good luck,

                    -blazed
                    While we're at it: a close() call can throw an IOException too so you'd better
                    change that finally clause to:

                    [code=java]
                    try {
                    ...
                    }
                    catch ( ... ) { ... }
                    finallly {
                    try { bis.close(); } catch (IOException ioe) { /* muffle */ }
                    try { bos.close(); } catch (IOException ioe) { /* muffle */ }
                    }
                    [/code]

                    kind regards,

                    Jos

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #25
                      Originally posted by JosAH
                      While we're at it: a close() call can throw an IOException too so you'd better
                      change that finally clause to:

                      [code=java]
                      try {
                      ...
                      }
                      catch ( ... ) { ... }
                      finallly {
                      try { bis.close(); } catch (IOException ioe) { /* muffle */ }
                      try { bos.close(); } catch (IOException ioe) { /* muffle */ }
                      }
                      [/code]

                      kind regards,

                      Jos
                      Indeed .

                      Comment

                      Working...