Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程写入本地txt文件时出现的问题_Java_Multithreading_Bufferedwriter - Fatal编程技术网

Java 线程写入本地txt文件时出现的问题

Java 线程写入本地txt文件时出现的问题,java,multithreading,bufferedwriter,Java,Multithreading,Bufferedwriter,我试图使用这个独立的类从大量源的文本中写入,而每次不打开文件的一个实例或连接,因此我添加到ArrayList,它在一个独立的线程中工作,以查看该行,因此,服务类尽可能快地编写信息,避免客户机类等待使用writer类。这个类的问题是,我在构造函数中执行线程,但线程在第一次调用中停止,以执行方法write gerenciadorDeArquivos.escrevemarquivo,它在文件中写入一次,不再写入,当我将程序放入调试时,如果我不这样做,他甚至不会执行一次。也许你能给我一些见解 主窗体类

我试图使用这个独立的类从大量源的文本中写入,而每次不打开文件的一个实例或连接,因此我添加到ArrayList,它在一个独立的线程中工作,以查看该行,因此,服务类尽可能快地编写信息,避免客户机类等待使用writer类。这个类的问题是,我在构造函数中执行线程,但线程在第一次调用中停止,以执行方法write gerenciadorDeArquivos.escrevemarquivo,它在文件中写入一次,不再写入,当我将程序放入调试时,如果我不这样做,他甚至不会执行一次。也许你能给我一些见解

主窗体类

package EscreveArquivo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Tela extends javax.swing.JFrame {

public Tela() {
    initComponents();
}
 private void initComponents() {....} /// Form Components

 EscreveArquivo escritorDeArquivos = new EscreveArquivo();

   private void recordTextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    escritorDeArquivos.AdicionaArquivoEmListaDeInstrucoes(textField.getText());
}               
二等舱

package Escrevearquivo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;*

public class EscreveArquivo {

private static List<String> listaDeInstrucoes = new ArrayList<String>();
private boolean gerenciadorEstaEmExecucao = false;

EscreveArquivo() {
    Thread t = new Thread() {   
        public void run() //Anonymous class overriding run() method of Thread class
        {
            while (true) {
                if (!listaDeInstrucoes.isEmpty()) {
                    gerenciadorDeArquivos();
                }
            }
        }
    };
    t.start();
}

public static void AdicionaArquivoEmListaDeInstrucoes(String arquivoASerEscrito) {
    listaDeInstrucoes.add(arquivoASerEscrito);
}

private void gerenciadorDeArquivos() {

    if (gerenciadorEstaEmExecucao == false) {
        gerenciadorEstaEmExecucao = true;

        ArrayList<String> listaDeInstrucoesCopia = new ArrayList<String>();
        for (String textoAEscrever : listaDeInstrucoes) {
            listaDeInstrucoesCopia.add(textoAEscrever);
        }
        for (String textoAApagar : listaDeInstrucoesCopia) {
            EscreveEmArquivo(textoAApagar);
            listaDeInstrucoesCopia.remove(textoAApagar);
        }
    }
    gerenciadorEstaEmExecucao = false;
}

private static void EscreveEmArquivo(String texto) {
    File arquivo = new File("C://Users//Josué//Desktop//arquivoTexto.txt");
    try {
        if (!arquivo.exists()) {
            arquivo.createNewFile();
        }
        FileWriter writer = new FileWriter(arquivo, true);
        BufferedWriter saida = new BufferedWriter(writer);
        saida.write(texto);
        saida.newLine();
        saida.close();

    } catch (IOException ex) {
        Logger.getLogger(Tela.class
                .getName()).log(Level.SEVERE, null, ex);
    }
}
}本节

for(String textoAApagar : listaDeInstrucoesCopia) {
   EscreveEmArquivo(textoAApagar);
   listaDeInstrucoesCopia.remove(textoAApagar); //error will be thrown here!
}
实际上会为ConcurrentModificationException生成一个错误,因为您在遍历列表listaDeInstrucoesCopia时正在修改它。因此,在这种情况下,调用将不会写入第二个字段。只有第一个调用将被执行

移除线路列表D说明SCOPIA。移除ExtoeApagar,它应该很好用。但同样,您没有清空ArrayList,因此旧值将被重新写入


您应该考虑使用队列实现并弹出Enter元素并连续写入文件。 有任何异常抛出吗?没有异常,在调试中它只写一次,在电子执行中什么都没有发生。