hello everyone! I am a newbei here...
Pls help me answering this question: How to write data in a new line in a particular text file? Is there any particular method in the RandomAccessFile class in java that is capable of handling such thing.

Here's my code:
import java.io.*;

class CreateBankFile{
	public static void main(String args[]) throws IOException{
		BufferedReader in  = new BufferedReader (new InputStreamReader(System.in));
		
		System.out.print("Enter File's name: " );
		String str = in.readLine();
		str = str.trim();
		
		File file = new File(str);
		
		try{
		
			RandomAccessFile rand = new RandomAccessFile (file, "rw");
			
			rand.seek(file.length());
			rand.writeBytes("\nAccount Number: " + file);
			System.out.print("Enter Account Name: ");
			String name = in.readLine();
			name = name.trim();
			System.out.print("Enter Account Balance: P ");
			String balance = in.readLine();
			balance = balance.trim();
			rand.writeBytes("\nAccount Name: " + name);
			rand.writeBytes("\nAccount Balance: P " + balance);
			rand.close();
			System.out.println("Write Successfully");
		
			} catch (IOException e){
			System.out.println(e.getMessage());
		}	
	
	} 
}


This program outputs a text file that contains the ff data:

Account Number: 1.txt Account Name: cristine Account Balance: P 10,000.00

Question: How can I write those data in a new line (in the text file) like this: 
Account Number: 1.txt 
Account Name: cristine 
Account Balance: P 10,000.00

Yes, append the data at the end of the line.
Is it possible to insert data in betweens? how?
Thank you.

To append at the end of the file, use:

BufferedWriter wr = new BufferedWriter(new FileWriter("filename", [B]true[/B]));

Don't forget to check the API for the above classes.

To write in between you will have to use RandomAccessFile but when I tried that, it overrides what is written at the place you are trying to write.
Usually, you can read the entire file in a Vector (each element would be a line of the file). Then add the line you want at the place you want in the vector and write the file again

before writing anything find file.length() after then you write new data on end of length that is end of file...

This article has been dead for over six months. Start a new discussion instead.