Reading and Writing to a .txt file in Java

后端 未结 4 558
[愿得一人]
[愿得一人] 2020-12-22 04:00

Here is my prompt for the assignment: Your program needs to read the information from a text file instead of using a scanner to read from command line. Your program also ne

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 05:01

    like @Shobit said previously, use a BufferedWriter in conjunction with a FileWriter and with the methods write() and newLine() you can insert the lines you want to the file in stead of using Println.

    BufferedWriter writer = new BufferedWriter(new FileWriter("path-of-file")); //you don't need to create a File object, FileWriter takes a string for the filepath as well
    writer.write("Student number..."); 
    writer.writeLine(); //for a new line in the file
    

    and when you are done writing to the file,

    writer.close();
    

提交回复
热议问题