在Java中同时读取和写入同一文件

时间:2017-12-27 16:35:48

标签: java string file bufferedreader bufferedwriter

我想要的是写一个"字符串"如果文件B中尚不存在文件B中的文件A,则知道B在开头是空的

BufferedReader bfA=new BufferedReader(new FileReader("A.txt"));
        BufferedReader bfB=new BufferedReader(new FileReader("B.txt"));
        BufferedWriter writerB=new BufferedWriter(new FileWriter("B.txt"));
        String line1,line2;
        boolean bool=false;
        while((line1=bfA.readLine())!=null){
            bfB=new BufferedReader(new FileReader("B.txt"));
            while((line2=bfB.readLine())!=null){

                if(line1.equals(line2)){
                    bool=true;}}

        if(bool==false){
            writerB.write(line1);
        }
        }

问题是如果在文件A中有重复的字符串

1 个答案:

答案 0 :(得分:0)

按照JB Nizet的回答,这里有一些伪代码

readerA, writerB = "", memory = ""
while(readerA.hasNextLine()){
       line = readerA.nextLine()
  if(line isNotPresent in memory){
       add line to memory 
       add line in writerB
   }
}

希望这有帮助