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
24 4094
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...
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.
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
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.
ooow... a bit rude... thanks anyway. gudluck to me. ^^;
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
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.
i was able to read the file and print its contents : - //READ A FILE
-
try
-
{
-
FileInputStream fin = new FileInputStream("SampleWebService.war");
-
BufferedInputStream bin = new BufferedInputStream(fin);
-
int ch=0;
-
while((ch=bin.read())> -1)
-
{
-
StringBuffer buf = new StringBuffer();
-
buf.append((char)ch);
-
System.out.print(buf.toString());
-
saveVariable = String.valueOf(buf);
-
}
-
-
}
-
catch(IOException ex)
-
{
-
System.out.println(ex.getMessage());
-
}
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
1.)Put the logic in a static method with the following signature - public static void boolean(String fromPath, String toPath) {
2.) Use while
what if the value read is 0?
3.)Close those streams after using them.
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.....
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
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
You can simply use -
int val = bis.read();
-
while(val != -1) {
-
bos.write(val);
-
val = bis.read();
-
}
For the reading and writing.
Still.... it creates 0kb file..... ^^; eheheh i know it's easy for you guys.... sorry. i'm still new in java i guess....
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?
- import java.io.BufferedInputStream;
-
import java.io.BufferedOutputStream;
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import javax.swing.JOptionPane;
-
-
-
// 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("SampleWebService.war");
-
BufferedInputStream bin = new BufferedInputStream(fin);
-
int ch=0;
-
-
while((ch=bin.read())> -1)
-
{
-
StringBuffer buf = new StringBuffer();
-
buf.append((char)ch);
-
System.out.print(buf.toString());
-
saveVariable = String.valueOf(buf);
-
}
-
-
//WRITE A FILE
-
try
-
{
-
-
FileOutputStream fos = new FileOutputStream("Sample.war");
-
-
BufferedOutputStream bos = new BufferedOutputStream(fos);
-
-
while(ch != -1)
-
{
-
bos.write(ch);
-
ch = bin.read();
-
-
}
-
bos.close();
-
-
}
-
catch (Exception e)
-
{
-
System.out.println("Exception: " + e);
-
}
-
-
}
-
catch(IOException ex)
-
{
-
System.out.println(ex.getMessage());
-
}
-
// END OF CODE //
Hope we can figure it out.... thanks.
Best Regards,
Jeff
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
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 - public static void copy(String fromPath, String toPath) {
-
try {
-
FileInputStream fis = new FileInputStream(fromPath);
-
BufferedInputStream bis = new BufferedInputStream(fis);
-
FileOutputStream fos = new FileOutputStream(toPath);
-
BufferedOutputStream bos = new BufferedOutputStream(fos);
-
int val = bis.read();
-
while(val != -1) {
-
bos.write(val);
-
val = bis.read();
-
}
-
-
}
-
catch(Exception e) {
-
e.printStackTrace();
-
}
-
finally {
-
bis.close();
-
bos.close();
-
}
-
}
Putting it in a method like this allows me to call it from anywhere with any arguments I want.
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
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 - try {
-
FileInputStream fis = new FileInputStream(fromPath);
-
BufferedInputStream bis = new BufferedInputStream(fis);
-
FileOutputStream fos = new FileOutputStream(toPath);
-
BufferedOutputStream bos = new BufferedOutputStream(fos);
-
int val = bis.read();
-
while(val != -1) {
-
bos.write(val);
-
val = bis.read();
-
}
-
bis.close();
-
bos.close();
-
-
}
-
catch(Exception e) {
-
e.printStackTrace();
-
}
P.S:I hope you realized why the code didn't work like that.
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. ^^
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 - try {
-
FileInputStream fis = new FileInputStream(fromPath);
-
BufferedInputStream bis = new BufferedInputStream(fis);
-
FileOutputStream fos = new FileOutputStream(toPath);
-
BufferedOutputStream bos = new BufferedOutputStream(fos);
-
int val = bis.read();
-
while(val != -1) {
-
bos.write(val);
-
val = bis.read();
-
}
-
bis.close();
-
bos.close();
-
-
}
-
catch(Exception e) {
-
e.printStackTrace();
-
}
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: - try {
-
FileInputStream fis = new FileInputStream(fromPath);
-
BufferedInputStream bis = new BufferedInputStream(fis);
-
FileOutputStream fos = new FileOutputStream(toPath);
-
BufferedOutputStream bos = new BufferedOutputStream(fos);
-
int val = bis.read();
-
while(val != -1) {
-
bos.write(val);
-
val = bis.read();
-
}
-
-
}
-
catch(Exception e) {
-
e.printStackTrace();
-
}
-
finally {
-
bis.close();
-
bos.close();
-
}
-
-
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
Just a tip since I was doing something like this earlier, add a finally parameter at the end for closing the streams like this: - try {
-
FileInputStream fis = new FileInputStream(fromPath);
-
BufferedInputStream bis = new BufferedInputStream(fis);
-
FileOutputStream fos = new FileOutputStream(toPath);
-
BufferedOutputStream bos = new BufferedOutputStream(fos);
-
int val = bis.read();
-
while(val != -1) {
-
bos.write(val);
-
val = bis.read();
-
}
-
-
}
-
catch(Exception e) {
-
e.printStackTrace();
-
}
-
finally {
-
bis.close();
-
bos.close();
-
}
-
-
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: -
try {
-
...
-
}
-
catch ( ... ) { ... }
-
finallly {
-
try { bis.close(); } catch (IOException ioe) { /* muffle */ }
-
try { bos.close(); } catch (IOException ioe) { /* muffle */ }
-
}
-
kind regards,
Jos
While we're at it: a close() call can throw an IOException too so you'd better
change that finally clause to: -
try {
-
...
-
}
-
catch ( ... ) { ... }
-
finallly {
-
try { bis.close(); } catch (IOException ioe) { /* muffle */ }
-
try { bos.close(); } catch (IOException ioe) { /* muffle */ }
-
}
-
kind regards,
Jos
Indeed .
Sign in to post your reply or Sign up for a free account.
Similar topics
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 ,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |