dcsimg
file handlilng in java
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   vijitc_tc
Posted On:   Friday, December 16, 2005 03:00 AM

how do i append a file using FileInputStream and How do i handle Exceptions occuring during reading/writing in the file?

Re: file handlilng in java

Posted By:   dtr_prasad  
Posted On:   Thursday, January 12, 2006 10:07 AM

use new FileOutputStream("myfile.txt",true) constructor while writing.
else use nio channels to read into ByteBuffer and appending content ,create a new channel(with file name for FileOutputStream)and write this ByteBuffer and force it.
checked exceptions like file existence/EOF are to be handled in catch block and with loop

int c;
while((c=in.read())!=-1)
{
...manipulate c....
}

Re: file handlilng in java

Posted By:   Murali_hk  
Posted On:   Friday, December 16, 2005 05:16 AM

you can use FileOutputStream setting append true parameter
sample code follows


import java.io.File;

import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileTester {
public static void main(String[] args) {
try {
File appendFile = new File("test.txt");
FileOutputStream fos = new FileOutputStream(appendFile,true);
String appendText = "Hello World " + System.currentTimeMillis() + "
";
fos.write(appendText.getBytes());
fos.close();
} catch(FileNotFoundException fe){
System.out.println("Exception thrown :: FileNotFoundException ");
fe.printStackTrace();

} catch(IOException ie) {
System.out.println("Exception thrown :: IOException ");
ie.printStackTrace();
}
}
}

















}

Re: file handlilng in java

Posted By:   Almagest_FUTT  
Posted On:   Friday, December 16, 2005 03:40 AM

how do i append a file using FileInputStream

With this constructor or that constructor (or with a FileWriter).
How do i handle Exceptions occuring during reading/writing in the file

That's a thing only you can tell. For a start (i.e. testing purposes) you may simply catch and log them.
About | Sitemap | Contact