在我调用我的IO方法之后,没有生成文件



`公共抽象类TV实现Logger{

protected int currentChannel;
protected int currentVolume;
protected String model;

public TV(String model) 
{
this.model = model;
currentChannel = 2;
currentVolume = 10;
}
public void incChannel() 
{
getcurrentChannel();
currentChannel = currentChannel + 1;
writeToLogFile("Increasing channel to " + currentChannel);
System.out.println("Increasing channel to " + currentChannel);

}
public void decChannel()
{
getcurrentChannel();
currentChannel = currentChannel - 1;
writeToLogFile("Decreasing channel to " + currentChannel);
System.out.println("Decreasing channel to " + currentChannel);
}
public void incVolume()
{
getcurrentVolume();
currentVolume = currentVolume + 1;
writeToLogFile("Increasing volume to " + currentVolume);
System.out.println("Increasing volume to " + currentVolume);
}
public void decVolume()
{
getcurrentVolume();
currentVolume = currentVolume - 1;
writeToLogFile("Decreasing volume to " + currentVolume);
System.out.println("Decreasing volume to " + currentVolume);
}
public void changeChannel(int currentChannel)
{
this.currentChannel = currentChannel;
writeToLogFile("Changing channel to  " + currentChannel);
System.out.println("Changing channel to " + currentChannel);

}

public void writeToLogFile(String message) 
{
model = getModel();
try {
FileReader fr = new FileReader(new File("./model.txt"));
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(new File("./model.txt"), true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
String line = br.readLine();  
while (line != null) { 
Scanner scanLine = new Scanner(line);
String mess = scanLine.next();
pw.println(message);
pw.println(mess);
line = br.readLine();

} 
br.close();
pw.flush();
pw.close();
} 
catch (FileNotFoundException e) 
{
System.out.println("File not found.");
} 
catch (IOException e) 
{
System.out.println("An IO error occurred.");
}
}`

当我调用run程序时,我的writeToLogFile方法无法正常工作。当调用它时,它不会根据需要生成文本文件。我需要writeToLogFile方法来创建文件,然后在其他方法中调用它时附加它。此外,我在这个节目中有两种型号的电视。当我使用其中一台电视(索尼(时,我希望它写入自己的名为Sony.txt的日志文件。然后我有另一台需要有自己日志文件的电视(LG(。我需要写两个单独的if语句来计算电视的品牌,然后将其指向自己的日志文件吗?

问题是同时在同一文件上使用读取器和写入器。一旦我去掉了打开文件的代码,这个方法就可以很好地工作了。我编辑了代码以反映适当的更改。

我想在同一时间对同一文件使用读取器和写入器是有问题的。我建议您删除阅读器,只打开文件进行附加。或者,您可以为编写器打开一个不同的文件,然后在完成写入后,删除原始文件并重命名新文件。–bcr666 6分钟前