简体   繁体   English

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

[英]bufferedwriter refuses to write to text file(Java)

Whenever I call my method in my code, despite opening and closing my BufferedWriter correctly(I think) it still refuses to print to the text file that it is meant to print to Here is my code :每当我在我的代码中调用我的方法时,尽管正确地打开和关闭了我的 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(); 
    }
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");
    }

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM