473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help java file manipulation.....

jeffbroodwar
118 100+
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
Jun 13 '07 #1
24 4094
jeffbroodwar
118 100+
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...
Jun 13 '07 #2
r035198x
13,262 8TB
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 BufferedInputStream and BufferedOutputStream 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.
Jun 13 '07 #3
jeffbroodwar
118 100+
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
Jun 13 '07 #4
r035198x
13,262 8TB
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.
Jun 13 '07 #5
jeffbroodwar
118 100+
ooow... a bit rude... thanks anyway. gudluck to me. ^^;
Jun 13 '07 #6
JosAH
11,448 Expert 8TB
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
Jun 13 '07 #7
r035198x
13,262 8TB
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.
Jun 13 '07 #8
jeffbroodwar
118 100+
i was able to read the file and print its contents :


Expand|Select|Wrap|Line Numbers
  1.  //READ A FILE
  2.                 try
  3.                 {
  4.                     FileInputStream fin = new FileInputStream("SampleWebService.war");
  5.                     BufferedInputStream bin = new BufferedInputStream(fin);
  6.                     int ch=0;
  7.                     while((ch=bin.read())> -1)
  8.                     {
  9.                             StringBuffer buf = new StringBuffer();
  10.                             buf.append((char)ch);
  11.                             System.out.print(buf.toString());
  12.                             saveVariable = String.valueOf(buf);
  13.                     }
  14.  
  15.                 }
  16.                 catch(IOException ex)
  17.                 {
  18.                     System.out.println(ex.getMessage());
  19.                 }

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

hope you guys can help me...

Regards,
Jeff
Jun 13 '07 #9
r035198x
13,262 8TB
1.)Put the logic in a static method with the following signature
Expand|Select|Wrap|Line Numbers
  1. public static void boolean(String fromPath, String toPath) {
2.) Use while
Expand|Select|Wrap|Line Numbers
  1. while(ch != -1) {
what if the value read is 0?
3.)Close those streams after using them.
Jun 13 '07 #10
jeffbroodwar
118 100+
how can i write it? i mean after placing it inside... where's my buffer output stream? i tried this code :

//WRITE FILE
try
{

FileOutputStream fos = new FileOutputStream("Sample.war");

BufferedOutputStream bos = new BufferedOutputStream(fos);

bos.write(Byte.parseByte(saveVariable));


}
catch (Exception e)
{
System.out.println("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.....
Jun 13 '07 #11
jeffbroodwar
118 100+
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
Jun 13 '07 #12
JosAH
11,448 Expert 8TB
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 BufferedOutputStream (it will close the FileOutputStream
for you too), when you're done with it.

kind regards,

Jos
Jun 13 '07 #13
r035198x
13,262 8TB
You can simply use

Expand|Select|Wrap|Line Numbers
  1. int val = bis.read();
  2. while(val != -1) {
  3.        bos.write(val);
  4.        val = bis.read();
  5. }
For the reading and writing.
Jun 13 '07 #14
jeffbroodwar
118 100+
Still.... it creates 0kb file..... ^^; eheheh i know it's easy for you guys.... sorry. i'm still new in java i guess....
Jun 13 '07 #15
r035198x
13,262 8TB
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?
Jun 13 '07 #16
jeffbroodwar
118 100+
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import javax.swing.JOptionPane;
  7.  
  8.  
  9. // This code goes inside the button's action performed //
  10.  
  11.  
  12. // Paste a file in a folder Button
  13. String saveVariable = null;
  14.                 //READ A FILE
  15.                 try
  16.                 {
  17.                     FileInputStream fin = new FileInputStream("SampleWebService.war");
  18.                     BufferedInputStream bin = new BufferedInputStream(fin);
  19.                     int ch=0;
  20.  
  21.                     while((ch=bin.read())> -1)
  22.                     {
  23.                             StringBuffer buf = new StringBuffer();
  24.                             buf.append((char)ch);
  25.                             System.out.print(buf.toString());
  26.                             saveVariable = String.valueOf(buf);
  27.                     }
  28.  
  29.                             //WRITE A FILE
  30.                             try 
  31.                             {
  32.  
  33.                               FileOutputStream fos = new FileOutputStream("Sample.war");
  34.  
  35.                               BufferedOutputStream bos = new BufferedOutputStream(fos);
  36.  
  37.                                 while(ch != -1)
  38.                                 {
  39.                                        bos.write(ch);
  40.                                        ch = bin.read();
  41.  
  42.                                 }
  43.                                 bos.close();
  44.  
  45.                             } 
  46.                             catch (Exception e) 
  47.                             {
  48.                               System.out.println("Exception: " + e);
  49.                             }
  50.  
  51.                 }
  52.                 catch(IOException ex)
  53.                 {
  54.                     System.out.println(ex.getMessage());
  55.                 }
  56. // END OF CODE //
Hope we can figure it out.... thanks.

Best Regards,
Jeff
Jun 13 '07 #17
jeffbroodwar
118 100+
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
Jun 13 '07 #18
r035198x
13,262 8TB
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

Expand|Select|Wrap|Line Numbers
  1. public static void copy(String fromPath, String toPath) {
  2.         try {
  3.             FileInputStream fis = new FileInputStream(fromPath);
  4.             BufferedInputStream bis = new BufferedInputStream(fis);
  5.             FileOutputStream fos = new FileOutputStream(toPath);
  6.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  7.             int val = bis.read();
  8.             while(val != -1) {
  9.                 bos.write(val);
  10.                 val = bis.read();
  11.             }
  12.  
  13.         }
  14.         catch(Exception e) {
  15.             e.printStackTrace();
  16.         }
  17.         finally {
  18.             bis.close();
  19.             bos.close();
  20.         }
  21.     }
Putting it in a method like this allows me to call it from anywhere with any arguments I want.
Jun 13 '07 #19
jeffbroodwar
118 100+
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
Jun 13 '07 #20
r035198x
13,262 8TB
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


Expand|Select|Wrap|Line Numbers
  1. try {
  2.             FileInputStream fis = new FileInputStream(fromPath);
  3.             BufferedInputStream bis = new BufferedInputStream(fis);
  4.             FileOutputStream fos = new FileOutputStream(toPath);
  5.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  6.             int val = bis.read();
  7.             while(val != -1) {
  8.                 bos.write(val);
  9.                 val = bis.read();
  10.             }
  11.             bis.close();
  12.             bos.close();
  13.  
  14.         }
  15.         catch(Exception e) {
  16.             e.printStackTrace();
  17.         }
P.S:I hope you realized why the code didn't work like that.
Jun 13 '07 #21
jeffbroodwar
118 100+
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.......That's a mistake on my part. Thanks again. ^^
Jun 14 '07 #22
blazedaces
284 100+
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


Expand|Select|Wrap|Line Numbers
  1. try {
  2.             FileInputStream fis = new FileInputStream(fromPath);
  3.             BufferedInputStream bis = new BufferedInputStream(fis);
  4.             FileOutputStream fos = new FileOutputStream(toPath);
  5.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  6.             int val = bis.read();
  7.             while(val != -1) {
  8.                 bos.write(val);
  9.                 val = bis.read();
  10.             }
  11.             bis.close();
  12.             bos.close();
  13.  
  14.         }
  15.         catch(Exception e) {
  16.             e.printStackTrace();
  17.         }
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:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.             FileInputStream fis = new FileInputStream(fromPath);
  3.             BufferedInputStream bis = new BufferedInputStream(fis);
  4.             FileOutputStream fos = new FileOutputStream(toPath);
  5.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  6.             int val = bis.read();
  7.             while(val != -1) {
  8.                 bos.write(val);
  9.                 val = bis.read();
  10.             }
  11.  
  12.         }
  13.         catch(Exception e) {
  14.             e.printStackTrace();
  15.         }
  16.         finally {
  17.             bis.close();
  18.             bos.close();
  19.         }
  20.  
  21.  
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
Jun 14 '07 #23
JosAH
11,448 Expert 8TB
Just a tip since I was doing something like this earlier, add a finally parameter at the end for closing the streams like this:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.             FileInputStream fis = new FileInputStream(fromPath);
  3.             BufferedInputStream bis = new BufferedInputStream(fis);
  4.             FileOutputStream fos = new FileOutputStream(toPath);
  5.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  6.             int val = bis.read();
  7.             while(val != -1) {
  8.                 bos.write(val);
  9.                 val = bis.read();
  10.             }
  11.  
  12.         }
  13.         catch(Exception e) {
  14.             e.printStackTrace();
  15.         }
  16.         finally {
  17.             bis.close();
  18.             bos.close();
  19.         }
  20.  
  21.  
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:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    ...
  3. }
  4. catch ( ... ) { ... }
  5. finallly {
  6.    try { bis.close(); } catch (IOException ioe) { /* muffle */ }
  7.    try { bos.close(); } catch (IOException ioe) { /* muffle */ }
  8. }
  9.  
kind regards,

Jos
Jun 14 '07 #24
r035198x
13,262 8TB
While we're at it: a close() call can throw an IOException too so you'd better
change that finally clause to:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    ...
  3. }
  4. catch ( ... ) { ... }
  5. finallly {
  6.    try { bis.close(); } catch (IOException ioe) { /* muffle */ }
  7.    try { bos.close(); } catch (IOException ioe) { /* muffle */ }
  8. }
  9.  
kind regards,

Jos
Indeed .
Jun 14 '07 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jagdeesh | last post by:
Hai Colleagues, I am using Tomcat 4.1.24 and JDK 1.4.0_03 in my winXP machine. I've transferred a set of folders(containing jsp files) into tomcat's webapps directory(to /webapps/bob ,...
15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
2
by: Goran Petrovic | last post by:
I wrote a vb function, but i need it i jaavscript..i know java sintax but am not sure how to make it work. this extracts teh file name from a path...i thought strcpy..or something..but dont really...
47
by: Theatre Mgmt | last post by:
Sun project seeks to solve Java memory, execution issues http://story.news.yahoo.com/news?tmpl=story&u=/infoworld/20050325/tc_infoworld/58059&e=1&ncid= San Francisco (InfoWorld) - Sun...
30
by: somebody | last post by:
I'm posting a new message regarding this subject, since the one posted previously is getting rather large. I just received the June 2005 issue of Dr. Dobb's Journal, and it contains an article...
8
by: DKM | last post by:
Here are the source code files to a Java applet that utilizes LiveConnect to communicate with Javascript, and the HTML file. The thing works both in IE 6.0 and FireFox 1.4. but with some...
1
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre...
4
dfound
by: dfound | last post by:
Hi, I'm very new to Java. I am a C++ and VB programmer but I always wanted to develop platform independent softwares and wanted to use Java. last day when I went to download Java from the sun...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.