Help java file manipulation.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeffbroodwar
    New Member
    • Oct 2006
    • 118

    #1

    Help java file manipulation.....

    Hi everyone,

    Please help me accomplish the following :

    1. how can i copy a file in java then paste it in a desired location

    2. how can i run a .jar file run by just double clicking on it....

    I need to know the answer asap... please reply asap... thanks.


    Regards,
    Jeff
  • jeffbroodwar
    New Member
    • Oct 2006
    • 118

    #2
    hey i found how to run the .jar by double clicking on it.... i noticed that there are 2 files that are recommended to open it.... Java(TM)2 SE Binary and Java(TM) SE Binary. i chose the Java(TM) SE Binary and it worked.... guess i need to clean my machine and try to make a clean install of Java. 1 down....how to copy and paste a file is left.... please help...

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by jeffbroodwar
      Hi everyone,

      Please help me accomplish the following :

      1. how can i copy a file in java then paste it in a desired location

      2. how can i run a .jar file run by just double clicking on it....

      I need to know the answer asap... please reply asap... thanks.


      Regards,
      Jeff

      To copy files, you must read the source file and write it to the destination file. You can use a BufferedInputSt ream and BufferedOutputS tream for that. N.B To move files, you can simply use the renameTo method in java.io.File class.

      For number 2, read the jar file tutorial from sun.

      Comment

      • jeffbroodwar
        New Member
        • Oct 2006
        • 118

        #4
        will this work if for example i want to put sample.war in c:\Jeffbroodwar folder from C:\ .I know this is gonna be spoon feeding... but can you help me with this, i mean can you give me a working code? cause i really need to finish this before i lose my job..... ^^; besides, i'm not that type....... i.e. if you dont mind r035198x Thanks,


        Best Regards,
        Jeff

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by jeffbroodwar
          will this work if for example i want to put sample.war in c:\Jeffbroodwar folder from C:\ .I know this is gonna be spoon feeding... but can you help me with this, i mean can you give me a working code? cause i really need to finish this before i lose my job..... ^^; besides, i'm not that type....... i.e. if you dont mind r035198x Thanks,


          Best Regards,
          Jeff
          If you don't want to lose your job you might as well start to write that program now. The code will just be about 15 or so lines anyway. Read the file from its current location and write it to the new location. It really is just as easy as that.

          Comment

          • jeffbroodwar
            New Member
            • Oct 2006
            • 118

            #6
            ooow... a bit rude... thanks anyway. gudluck to me. ^^;

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Also note that if the file contains binary data (non-characters) it's better to use
              streams instead of Readers and Writers; the latter apply character encoding and
              deconding while streams read and write byte values as they are.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by jeffbroodwar
                ooow... a bit rude... thanks anyway. gudluck to me. ^^;
                Remember that we are waiting here to see what you come up with. Don't make us wait too long. If you open the specs for those streams, this shouldn't take you 10 minutes.

                Comment

                • jeffbroodwar
                  New Member
                  • Oct 2006
                  • 118

                  #9
                  i was able to read the file and print its contents :


                  [CODE=java] //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);
                  }

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


                  the only problem is how can i write it....to a new file....example putting it inside
                  the folder DestinationFold er...

                  hope you guys can help me...

                  Regards,
                  Jeff
                  Last edited by r035198x; Jun 13 '07, 09:36 AM. Reason: added code tags

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    1.)Put the logic in a static method with the following signature
                    [CODE=java]public static void boolean(String fromPath, String toPath) {[/CODE]

                    2.) Use while [CODE=java]while(ch != -1) {[/CODE] what if the value read is 0?
                    3.)Close those streams after using them.

                    Comment

                    • jeffbroodwar
                      New Member
                      • Oct 2006
                      • 118

                      #11
                      how can i write it? i mean after placing it inside... where's my buffer output stream? i tried this code :

                      //WRITE FILE
                      try
                      {

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

                      BufferedOutputS tream bos = new BufferedOutputS tream(fos);

                      bos.write(Byte. parseByte(saveV ariable));


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


                      errrr..... but it gave me 0 bytes when i tried to look at it in the directory. the original file is 6 bytes.....

                      Comment

                      • jeffbroodwar
                        New Member
                        • Oct 2006
                        • 118

                        #12
                        Please help me with this one... i can make a code that will put the files in its desired location later. what i need now is how to write a new file after reading the original one... and placing it in other directory / even just renaming the new file and placing it in the same directory

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by jeffbroodwar
                          Please help me with this one... i can make a code that will put the files in its desired location later. what i need now is how to write a new file after reading the original one... and placing it in other directory / even just renaming the new file and placing it in the same directory
                          properly close() your BufferedOutputS tream (it will close the FileOutputStrea m
                          for you too), when you're done with it.

                          kind regards,

                          Jos

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            You can simply use

                            [CODE=java]
                            int val = bis.read();
                            while(val != -1) {
                            bos.write(val);
                            val = bis.read();
                            }[/CODE]
                            For the reading and writing.

                            Comment

                            • jeffbroodwar
                              New Member
                              • Oct 2006
                              • 118

                              #15
                              Still.... it creates 0kb file..... ^^; eheheh i know it's easy for you guys.... sorry. i'm still new in java i guess....

                              Comment

                              Working...