gpt4 book ai didi

java - 使用多线程写入文本文件

转载 作者:行者123 更新时间:2023-12-01 21:42:11 32 4
gpt4 key购买 nike

我正在尝试使用一个专用线程来定期覆盖文件,以便文件中唯一实际更改的内容是我在 GUI 中更改的内容。相关代码如下:

    private ThreadClass writeToFile = new ThreadClass("toFile"){
@Override
public void run(){
synchronized(lock){
NameAddressID store[] = new NameAddressID[10];
boolean tmpFile = true;
Scanner loadOver;
PrintWriter overWrite;
while(true){

try {
if(tmpFile){
loadOver = new Scanner(new File("NAIList.txt"));
overWrite = new PrintWriter("NAIList.tmp", "UTF-8");
}else{
loadOver = new Scanner(new File("NAIList.tmp"));
overWrite = new PrintWriter("NAIList.txt", "UTF-8");
}
for(int i = 0; i < count-1; i++){
for(int j = 0; j<10; j++){
if(loadOver.hasNextLine()){
String[] tokens = loadOver.nextLine().split(";");
store[j] = new NameAddressID(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], Integer.valueOf(tokens[6]), Integer.valueOf(tokens[7]));
}else{
store[j] = null;
}
}
for(int j = 0; j<store.length;j++){
if(store[j] != null){
overWrite.print(store[j].getFirst()+";");
System.out.print(store[j].getFirst()+" ");
overWrite.print(store[j].getMiddle()+";");
System.out.print(store[j].getMiddle()+" ");
overWrite.print(store[j].getLast()+";");
System.out.print(store[j].getLast()+" ");
overWrite.print(store[j].getStreet()+";");
System.out.print(store[j].getStreet()+" ");
overWrite.print(store[j].getCity()+";");
System.out.print(store[j].getCity()+" ");
overWrite.print(store[j].getState()+";");
System.out.print(store[j].getState()+" ");
overWrite.print(store[j].getZip()+";");
System.out.print(store[j].getZip()+" ");
overWrite.println(store[j].getId());
System.out.println(store[j].getId());
store[j] = null;
overWrite.flush();
}else{
break;
}
}

}
saveArrayText("NAIList.tmp", overWrite);
for(int i = 0; i < addressList.size(); i++){
if(loadOver.hasNextLine()){
String skip = loadOver.nextLine();
}
}
while(loadOver.hasNextLine()){
int index = 0;
String[] tokens = loadOver.nextLine().split(";");
store[index++] = new NameAddressID(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], Integer.valueOf(tokens[6]), Integer.valueOf(tokens[7]));
for(int j = 0; j<store.length;j++){
if(store[j] != null){
overWrite.print(store[j].getFirst()+";");
overWrite.print(store[j].getMiddle()+";");
overWrite.print(store[j].getLast()+";");
overWrite.print(store[j].getStreet()+";");
overWrite.print(store[j].getCity()+";");
overWrite.print(store[j].getState()+";");
overWrite.print(store[j].getZip()+";");
overWrite.println(store[j].getId());
store[j] = null;
overWrite.flush();
break;
}
}
}
tmpFile = !tmpFile;
} catch (IOException e1) {
e1.printStackTrace();
}

lock.notifyAll();
System.out.println("Saved");
try {
lock.wait();
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};

我知道它到达了 for 循环,我在其中获取 store[] 中的所有元素并将它们写入文件,并且它确实将正确的数据打印到控制台。但是,任何时候它修改 NAIList.txt 或 NAIList.tmp 时,都会使它们完全空白。我不知道为什么它不起作用,因为代码本质上是从主线程上运行的另一个方法复制的,该方法确实按照我想要的方式工作。

啊,忘了补充一下,作为老师给我们的人为限制,我们在任何给定时间的 RAM 中只能有 10 个 NameAddressID 对象。

编辑:固定代码,唯一改变的是 overWrite.flush(); 添加了两次

最佳答案

不需要刷新 PrintWriter 吗?

查看 PrintWriter 构造函数的 javadoc:

Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.

关于java - 使用多线程写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337030/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com