Originally posted by jeffbroodwar
Help java file manipulation.....
Collapse
X
-
[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,
JeffComment
-
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...... thanksComment
-
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
-
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,
JeffComment
-
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)Originally posted by jeffbroodwarThanks 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
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
-
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
-
Just a tip since I was doing something like this earlier, add a finally parameter at the end for closing the streams like this:Originally posted by r035198xThat'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.
[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,
-blazedComment
-
While we're at it: a close() call can throw an IOException too so you'd betterOriginally posted by blazedacesJust 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
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,
JosComment
-
Indeed .Originally posted by JosAHWhile 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,
JosComment
Comment