So I want to create and write to a file in Java. I know how to do it with one line, but I'm wondering how I write multiple lines to a single file.
import java.io.FileWriter;
import java.io.BufferedWriter;
public class WritingToAFile {
public static void main(String[] args) {
try{
FileWriter fstream = new FileWriter ("P:/Computer Science/greeting.txt");
BufferedWriter info = new BufferedWriter(fstream);
for(int i = 1; i < 6; i++){
info.write("Hello");
info.newLine();
info.write("Bonjour");
info.newLine();
info.write("Guten tag");
info.newLine();
info.write("Aloha");
info.newLine();
info.write("Nihao");
info.close();
}catch(Exception e){
System.out.println("A write error has occurred");
}
}
}
I know it's wrong right now, but I'm not sure what I did wrong. Help would be much appreciated! I'm only a beginner so I'm really lost.