【发布时间】:2019-10-17 22:42:06
【问题描述】:
基本上,我正在尝试创建一个简单的数据库应用程序,并在每个学生条目中自动生成 ID。程序读取前一个最高 ID 并将其加 1 以创建下一个。 然而,即使文本文档中有数字,BufferedReader 在使用 readLine 时仍会返回 null
我检查了我的 int 解析是否是问题,但我意识到它是缓冲读取器,方法是将 readline 保存到一个变量然后打印它,我得到了 null 的结果。我还尝试使用扫描仪文件读取,但它不起作用,我检查了所有相关的类和方法以试图弄清楚。
此代码创建 topsid 文件并写入 0 以对其进行初始化,该文件被读取为 null
if(MiscProcesses.firstStartup() == false) //method that checks if these files exist
{
File topsid = new File("topsid.txt");
FileWriter fw = new FileWriter(topsid);
fw.write("0");
fw.close();
}
此代码负责读取文件并因此找到更高的 id 值
Student (String[] studata)
{
//checking highest SID
File topsid = new File("topsid.txt");
FileWriter fw = new FileWriter(topsid);
FileReader fr = new FileReader(topsid);
BufferedReader br = new BufferedReader(fr);
//checking high sid file and getting new sid
String test = br.readLine();
System.out.println(test+" <test"); <this ends up printing null
int sid;
sid = Integer.parseInt(test)+1;
System.out.println(sid);
fw.write(Integer.toString(sid));
this.id = sid;
...
br.close();
fr.close();
fw.close();
}
当我在第二个代码运行之前打开 topsid 文件时,一切都很好,并且该文件包含一个零。 我希望 bufferedreader 读取“0”,但它只是读取 null,当我在代码运行后打开文件时,里面的数据会被删除。
【问题讨论】:
标签: java file bufferedreader filereader