- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尽管使用了 attend 方法,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");
}
}
}
最佳答案
您没有循环读取文件。试试这个:
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");
}
}
}
关于java - 使用 printwirter attend 方法的 while 循环仅写入最后搜索的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50872305/
我们正在使用 C# ZKempkeeper (CZKEMClass) dll 从生物识别/指纹扫描仪下载出勤数据。 有时代码运行良好,但大多数情况下它会花费很长时间,甚至不会出现超时错误。这是编译器卡
尽管使用了 attend 方法,Printwriter 仅将包含搜索单词的最后一行写入文件,而不是包含搜索单词的每一行。我认为我的代码有问题。任何人都可以检查代码并帮助我解决这个问题吗? packag
我正在 Android 设备上创建一个本地日历,并将事件插入其中。这是我的代码(现在使用虚拟数据): public static Uri createCalendarWithName(Context
如何使用 ASP.NET 工具包查询“uid X 是否参加事件 Y”? 我需要使用 Facebook SQL 吗? 我有一个有 5000 多名成员的事件,我想枚举当前用户的所有 friend ,看看他
我已经尽我所能解决这个问题,希望这里有人能提供帮助。 我得到的数据库架构如下: Customer: ID FirstName LastName DateOfBirth Instructor: Id F
我在看之前的问题比如unknown column in list但发现它对我的情况没有帮助。 我有一个表格可以记录出勤情况(从我的数据库表“records”中调用姓名和类(class),并将其插入到名
[问题已更新] node.js 代码 const updateLoggedMeetings = function () { return new Promise((res, rej) => {
我正在尝试让 Facebook 用户参加一个 Activity , Request.executePostRequestAsync(...) 但是服务器总是返回以下内容: {Response: re
我的数据库中有两个表,分别是用户表和出勤表。我现在想做的是根据用户在与他们的个人资料相关联的出勤 View 中显示数据库中的出勤数据。这是我在 userController 中的考勤功能。 publ
我是一名优秀的程序员,十分优秀!