调试文件搜索/合并代码(Debugging File Search / Merge Code)

编程入门 行业动态 更新时间:2024-10-27 17:24:49
调试文件搜索/合并代码(Debugging File Search / Merge Code)

此程序旨在查看位于特定文件夹中的两个文件,然后合并这两个文件并创建第三个文件。 从第三个合并文件开始,它会搜索一个关键字,例如“test”,一旦找到关键字,它就会打印出关键字的位置和关键字。 发生的事情是当我运行程序时它在第一次找到关键字后停止但它不会继续搜索该行。 因此,如果行中有多个关键字'test',它将只找到第一个关键字并吐出位置和行。 我希望它能打印两个或多个关键字。 我认为这是因为IndexOf逻辑引起了这个问题。

import com.sun.deploy.util.StringUtils; import java.io.*; import java.lang.*; import java.util.Scanner; public class Concatenate { public static void main(String[] args) { String sourceFile1Path = "C:/Users/me/Desktop/test1.txt"; String sourceFile2Path = "C:/Users/me/Desktop/test2.txt"; String mergedFilePath = "C:/Users/me/Desktop/merged.txt"; File[] files = new File[2]; files[0] = new File(sourceFile1Path); files[1] = new File(sourceFile2Path); File mergedFile = new File(mergedFilePath); mergeFiles(files, mergedFile); stringSearch(args); } private static void mergeFiles(File[] files, File mergedFile) { FileWriter fstream = null; BufferedWriter out = null; try { fstream = new FileWriter(mergedFile, true); out = new BufferedWriter(fstream); } catch (IOException e1) { e1.printStackTrace(); } for (File f : files) { System.out.println("merging: " + f.getName()); FileInputStream fis; try { fis = new FileInputStream(f); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String aLine; while ((aLine = in.readLine()) != null) { out.write(aLine); out.newLine(); } in.close(); } catch (IOException e) { e.printStackTrace(); } } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } private static void stringSearch(String args[]) { try { String stringSearch = "test"; BufferedReader bf = new BufferedReader(new FileReader("C:/Users/me/Desktop/merged.txt")); int linecount = 0; String line; System.out.println("Searching for " + stringSearch + " in file"); while (( line = bf.readLine()) != null){ linecount++; int indexfound = line.indexOf(stringSearch); if (indexfound > -1) { System.out.println(stringSearch + " was found at position " + indexfound + " on line " + linecount); System.out.println(line); } } bf.close(); } catch (IOException e) { System.out.println("IO Error Occurred: " + e.toString()); } } }

This program is meant to see two files located in a particular folder and then merge those two files and create a third file which is does. From the third merged file it is then searching for a keyword such as "test", once it finds that key word it prints out the location and the line of the keyword which is what is somewhat doing. What is happening is when I run the program it stops after the finds the keyword the first time in a line but it will not continue to search that line. So if there is multiple keyword 'test' in the line it will only find the first one and spit back the position and line. I want it to print both or multiple keywords. I think it is because of the IndexOf logic which is causing the issue.

import com.sun.deploy.util.StringUtils; import java.io.*; import java.lang.*; import java.util.Scanner; public class Concatenate { public static void main(String[] args) { String sourceFile1Path = "C:/Users/me/Desktop/test1.txt"; String sourceFile2Path = "C:/Users/me/Desktop/test2.txt"; String mergedFilePath = "C:/Users/me/Desktop/merged.txt"; File[] files = new File[2]; files[0] = new File(sourceFile1Path); files[1] = new File(sourceFile2Path); File mergedFile = new File(mergedFilePath); mergeFiles(files, mergedFile); stringSearch(args); } private static void mergeFiles(File[] files, File mergedFile) { FileWriter fstream = null; BufferedWriter out = null; try { fstream = new FileWriter(mergedFile, true); out = new BufferedWriter(fstream); } catch (IOException e1) { e1.printStackTrace(); } for (File f : files) { System.out.println("merging: " + f.getName()); FileInputStream fis; try { fis = new FileInputStream(f); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String aLine; while ((aLine = in.readLine()) != null) { out.write(aLine); out.newLine(); } in.close(); } catch (IOException e) { e.printStackTrace(); } } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } private static void stringSearch(String args[]) { try { String stringSearch = "test"; BufferedReader bf = new BufferedReader(new FileReader("C:/Users/me/Desktop/merged.txt")); int linecount = 0; String line; System.out.println("Searching for " + stringSearch + " in file"); while (( line = bf.readLine()) != null){ linecount++; int indexfound = line.indexOf(stringSearch); if (indexfound > -1) { System.out.println(stringSearch + " was found at position " + indexfound + " on line " + linecount); System.out.println(line); } } bf.close(); } catch (IOException e) { System.out.println("IO Error Occurred: " + e.toString()); } } }

最满意答案

这是因为你在while循环中每行搜索一次单词。 循环的每次迭代都会将您带到文件的下一行,因为您正在调用bf.readLine()。 尝试类似以下的内容。 你可能需要调整它,但这应该让你接近。

while (( line = bf.readLine()) != null){ linecount++; int indexfound = line.indexOf(stringSearch); while(indexfound > -1) { System.out.println(stringSearch + " was found at position " + indexfound + " on line " + linecount); System.out.println(line); indexfound = line.indexOf(stringSearch, indexfound); } }

It's because you are searching for the word once per line in your while loop. Each iteration of the loop takes you to the next line of the file because you are calling bf.readLine(). Try something like the following. You may have to tweak it but this should get you close.

while (( line = bf.readLine()) != null){ linecount++; int indexfound = line.indexOf(stringSearch); while(indexfound > -1) { System.out.println(stringSearch + " was found at position " + indexfound + " on line " + linecount); System.out.println(line); indexfound = line.indexOf(stringSearch, indexfound); } }

更多推荐

本文发布于:2023-08-04 17:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   文件   Debugging   File   Merge

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!