[英]My method is returning only the last value that was gotten from the while loop
[英]While loop with printwirter attend method writes only the last searched word
Printwriter僅將帶有搜索詞的最后一行寫到文件中,而不是將每個帶有搜索詞的行寫入到文件中,盡管使用了Atten方法。 我認為我的代碼有問題。 任何人都可以檢查代碼並幫助我嗎?
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");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.