bufferedwriter拒绝写入文本文件(Java)

时间:2019-01-21 05:09:31

标签: java file writer

每当我在代码中调用方法时,尽管正确打开和关闭了BufferedWriter(我认为),它仍然拒绝打印到要打印到的文本文件中 这是我的代码:

public Student(String nameInput, String gradeInput) throws IOException
    {
        BufferedWriter o = new BufferedWriter(new FileWriter("Students.txt"));
        name = nameInput;
        grade = gradeInput;
        o.write(gradeInput);
        o.newLine();
        o.write(nameInput);
        o.close(); 
    }

2 个答案:

答案 0 :(得分:1)

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void Student(String nameInput, String gradeInput) throws IOException
    {
        BufferedWriter o = new BufferedWriter(new FileWriter("Students.txt"));
        String name = nameInput;
        String grade = gradeInput;
        o.write(gradeInput);
        o.newLine();
        o.write(nameInput);
        o.close(); 
    }
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Student("ABC","A");
    }

}

答案 1 :(得分:0)

在关闭o.flush()BufferedWriter之前尝试使用o.close()