while printwirter attend方法只能写入最后搜索到的单词

时间:2018-06-15 09:04:15

标签: java while-loop filewriter printwriter file-writing

尽管使用了出席方法,但是Printwriter只将搜索到的单词的最后一行写入文件而不是每行搜索到的单词。我想我的代码中有问题。有人可以查一下代码并帮我解决这个问题吗?

package JanKozak6;

import java.util.Scanner;
import java.io.*;
public class Main {

public static void main(String[] args) {
    Scanner scan1 = new Scanner(System.in);
    //System.out.println("Write a name of a File.txt you want to check in project home directory");
    //String nameOfFile = scan1.nextLine();
    try {

        FileReader read1 = new FileReader("FileToRead");
        BufferedReader read11 = new BufferedReader(read1);
        System.out.println("Give a word to find in text lines");
        String wordToFind = scan1.nextLine();
        System.out.println("wordToFind: " + wordToFind);
       if (read11.readLine() != null) {

            String FoundWord = read11.readLine();
            System.out.println("Read the line");
            while (FoundWord.contains(wordToFind)) {

                try {
                    System.out.println("Try to write");
                    FileWriter write1 = new FileWriter("FoundWords", true);
                    PrintWriter write11 = new PrintWriter(write1);
                    System.out.println("Writing...");
                    //write11.write(FoundWord);
                    write11.append(FoundWord);
                    System.out.println("Closing BuffereWriter");
                    write11.close();
                }
                catch (IOException ex) {
                    System.out.println("FoundWord have not been written");
                }
            }
        }
        read11.close();
    }
    catch (IOException ex)
    {
        System.out.println("Exception: The file is not found");
    }

}
}

2 个答案:

答案 0 :(得分:1)

您没有循环读取文件。试试这个:

public static void main(String[] args) {
        Scanner scan1 = new Scanner(System.in);
        //System.out.println("Write a name of a File.txt you want to check in project home directory");
        //String nameOfFile = scan1.nextLine();
        try {

            FileReader read1 = new FileReader("FileToRead");
            BufferedReader read11 = new BufferedReader(read1);
            System.out.println("Give a word to find in text lines");
            String wordToFind = scan1.nextLine();
            String FoundWord=null;
            System.out.println("wordToFind: " + wordToFind);
            while((FoundWord=read11.readLine()) != null) {
                System.out.println("Read the line");
                if(FoundWord.contains(wordToFind)) {

                    try {
                        System.out.println("Try to write");
                        FileWriter write1 = new FileWriter("FoundWords", true);
                        PrintWriter write11 = new PrintWriter(write1);
                        System.out.println("Writing...");
                        //write11.write(FoundWord);
                        write11.append(FoundWord);
                        System.out.println("Closing BuffereWriter");
                        write11.close();
                    }
                    catch (IOException ex) {
                        System.out.println("FoundWord have not been written");
                    }
                }
            }
            read11.close();
        }
        catch (IOException ex)
        {
            System.out.println("Exception: The file is not found");
        }

    }
    }

答案 1 :(得分:0)

我完全同意上述答案。 这是处理文件的一种更有效的方法,而不是为每一行打开一次并在结束时关闭。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            final FileReader read1 = new FileReader("FileToRead");
            final BufferedReader reader = new BufferedReader(read1);
            System.out.println("Give a word to find in text lines");
            final String wordToFind = scanner.nextLine();
            System.out.println("wordToFind: " + wordToFind);
            final FileWriter fw = new FileWriter("FoundWords");
            final PrintWriter pw = new PrintWriter(fw);
            final Iterator<String> it = reader.lines().iterator();
            while (it.hasNext()) {
                final String line = it.next();
                System.out.println("Read the line: " + line);
                if (line.contains(wordToFind)) {
                    pw.println(line);
                }
            }
            pw.close();
            fw.close();
            reader.close();
            scanner.close();
        } catch (IOException ex) {
            System.out.println("Exception: The file is not found");
        }
    }
}