java读取输入/输出将重复字符串打印到文件

hrysbysz  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(437)

嗨,我正在解决这个问题,假设一个图书馆正在处理一个包含书名的输入文件,以便识别重复的图书。编写一个程序,从名为booktitles.inp的输入文件中读取所有标题,然后将它们写入名为duplicatetitles.out的输出文件。完成后,输出文件应包含输入文件中重复的所有标题。请注意,即使输入文件可能多次包含相同的标题,重复的标题也应写入一次。如果输入文件中没有重复的标题,则输出文件应为空。使用记事本或其他文本编辑器创建输入文件,每行一个标题。确保你有许多副本,包括一些有三个或更多副本。
到目前为止,我有这个,但它是打印重复不止一次,如果我改变输入文件的顺序。谢谢。

  1. import java.io.*;
  2. public class Library
  3. {
  4. public static void main(String[] args) throws IOException
  5. {
  6. String line3="";
  7. boolean dup = false;
  8. // PrintWriter object for output.txt
  9. PrintWriter pw = new PrintWriter("C:\\Users\\Ilyas\\Desktop\\tempBookTitles.txt");
  10. PrintWriter pw2 = new PrintWriter("C:\\Users\\Ilyas\\Desktop\\duplicateTitles.txt");
  11. // BufferedReader object for input.txt
  12. BufferedReader br1 = new BufferedReader(new
  13. FileReader("C:\\Users\\Ilyas\\Desktop\\bookTitles.txt")); //read input file
  14. String line1 = br1.readLine();
  15. // loop for each line of input.txt
  16. while(line1 != null)
  17. {
  18. boolean flag = false;
  19. // BufferedReader object for output.txt
  20. BufferedReader br2 = new BufferedReader(new
  21. FileReader("C:\\Users\\Ilyas\\Desktop\\tempBookTitles.txt"));
  22. BufferedReader br3 = new BufferedReader(new
  23. FileReader("C:\\Users\\Ilyas\\Desktop\\duplicateTitles.txt")); //try
  24. String line2 = br2.readLine();
  25. // loop for each line of output.txt
  26. while(line2 != null)
  27. {
  28. if(line1.equals(line2))
  29. {
  30. line3 = br3.readLine();
  31. flag = true;
  32. if(line1.equals(line3))
  33. {
  34. line1 = null;
  35. }
  36. else
  37. {
  38. pw2.println(line1);
  39. pw2.flush();
  40. //break;
  41. }
  42. }
  43. }
  44. line2 = br2.readLine();
  45. }
  46. // if flag = false
  47. // write line of input.txt to output.txt
  48. if(flag==false)
  49. {
  50. pw.println(line1); //print to temp file, delete temp file at end
  51. pw.flush();
  52. }
  53. line1 = br1.readLine();
  54. }
  55. br1.close();
  56. pw.close();
  57. pw2.close();
  58. System.out.println("File operation performed successfully");
  59. }
  60. }
tjjdgumg

tjjdgumg1#

要在没有Map的情况下解决它,可以使用方法contains,而不是直接写入输出文件,创建输出变量字符串,然后更改

  1. {
  2. pw2.println(line1);
  3. pw2.flush();
  4. }

对于

  1. if (!output.contains(line1+"\n"))
  2. output=output + line1+"\n"

循环后打印输出到文件

sdnqo3pr

sdnqo3pr2#

您应该尝试使程序可读,并通过分解成块的方法使其更小。
请看下面我的建议。

  1. private List<String> getAllTitles(String filepath){
  2. List<String> titles = new ArrayList<>();
  3. // read the file,
  4. // for each of the titles, insert into the list
  5. return titles;
  6. }
  7. private Set<String> getDuplicates(List<String> titles){
  8. Set<String> alreadyReadSet = new HashSet<>();
  9. Set<String> duplicateSet = new HashSet<>();
  10. for (String title : titles) {
  11. if(/*alreadyReadSet contains the title*/){
  12. // put title into duplicateList
  13. }
  14. // put the title into alreadyReadSet
  15. }
  16. return duplicateSet;
  17. }
  18. private void printDuplicateList(Collection<String> duplicateList){
  19. // print to a file
  20. }
展开查看全部

相关问题