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
cristine.apin 0 Newbie Poster
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
cristine.apin 0 Newbie Poster
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
musthafa.aj 6 Posting Whiz
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.