Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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 而使用printwirter Attribute方法的循环只写入最后搜索的单词_Java_While Loop_Filewriter_Printwriter_File Writing - Fatal编程技术网

Java 而使用printwirter Attribute方法的循环只写入最后搜索的单词

Java 而使用printwirter Attribute方法的循环只写入最后搜索的单词,java,while-loop,filewriter,printwriter,file-writing,Java,While Loop,Filewriter,Printwriter,File Writing,Printwriter只将带有搜索词的最后一行写入到文件中,而不是将带有搜索词的每一行写入到文件中,尽管使用了Attribute方法。我想我的代码有问题。有人能帮我查一下密码吗 package JanKozak6; import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) { Scanner scan1 = new Scanner(Sys

Printwriter只将带有搜索词的最后一行写入到文件中,而不是将带有搜索词的每一行写入到文件中,尽管使用了Attribute方法。我想我的代码有问题。有人能帮我查一下密码吗

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");
    }

}
}

您不是在循环中读取文件。试试这个:

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");
        }

    }
    }

我完全同意上述答案。 这里有一种更有效的处理文件的方法,而不是每行打开一次,然后在最后关闭

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");
        }
    }
}

很好,谢谢。我努力寻找一个错误,但在这种情况下一无所获。