有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

连接Java:文件读取/添加到文件程序时未按预期工作(逻辑错误)

我正在尝试编写一个程序:

  • 通过用户给定的文件路径扫描文件
  • 数一数它的话
  • 将其内容打印到终端
  • 通过用户指定的另一个文件路径,将其添加到新文件中

它还可以计算新文件的字数,并将其内容打印到终端

但我对这个新文件有两个问题: 1-它只保存我的ADITION而不保存旧文件的内容(即使我使用了.concat()方法) 2-它无法计算新文件的字数

除此之外,它的工作,我尝试了很多解决这些问题,但我不能

package com;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;

public class FileReader {
  public static void main(String[] args) throws IOException {
    Scanner scanTwo = new Scanner(System.in);
    System.out.println("Please Enter Your File Path");
    String filePath = scanTwo.nextLine();  
    File fileInput = new File(filePath);
    Scanner fileScanner = new Scanner(fileInput);
    System.out.println(fileScanner.nextLine());
    System.out.println("Commands: PRINT.FILE --> Prints all file    COUNT.WORDS --> Counts all words   ADD.TO.FILE --> add to selected file");
    System.out.println("Type Command:");
    
    Scanner scan = new Scanner(System.in);
    String printFileCommand = scan.nextLine();
     
     
     if (printFileCommand.contains("PRINT.FILE")) {
       while (fileScanner.hasNextLine()) {
         System.out.println(fileScanner.nextLine());
        }
      } else if (printFileCommand.contains("COUNT.WORDS")) {
        int wordCount = 0;
        while (fileScanner.hasNext()) {
          String fileWords = fileScanner.next();
          wordCount++;
        }
        System.out.println(wordCount);
      } else if (printFileCommand.contains("ADD.TO.FILE")) {
        System.out.println("Please Enter Your new file path");
        String newFilePath = scan.nextLine();
        FileWriter addToFile = new FileWriter(newFilePath);
        System.out.println("Please Enter Your Additions: ");
        String newFileContent = scan.nextLine();
        File newFileInput = new File(newFilePath);
        Scanner newFileScanner = new Scanner(newFileInput); 
        while (newFileScanner.hasNextLine()) {
          newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
        }
        addToFile.write(newFileContent);
        addToFile.close();
        System.out.println("Commands: PRINT.FILE --> Prints all file    COUNT.WORDS --> Counts all words");
        System.out.println("Please Enter Your Command:");
        String newCommand = scan.nextLine();
        if (newCommand.contains("PRINT.FILE")) {
          while (newFileScanner.hasNextLine()) {
            System.out.println(newFileScanner.nextLine());
           }
        } else if (newCommand.contains("COUNT.WORD")) {
          int newWordCount = 0;
           while (newFileScanner.hasNext()) {
           String newFileWords = newFileScanner.next();
           newWordCount++;
         }
         System.out.println(newWordCount);
        } else {
          System.out.println("COMMAND INVALID!");
        }
        addToFile.close();
        // newFileScanner.close();
      }
      else {
        System.out.println("COMMAND INVALID!");
      }
      scanTwo.close();
      fileScanner.close();
      scan.close();
    } 
  }

共 (1) 个答案

  1. # 1 楼答案

    您确定要打开旧文件进行读取和压缩吗?我看到您通过newFilePath路径打开新文件,而此文件为空,这是您的代码:

    File newFileInput = new File(newFilePath);
    Scanner newFileScanner = new Scanner(newFileInput); 
    while (newFileScanner.hasNextLine()) {
        newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
    }
    

    另外,如果您已经使用Scanner读取文件,则需要创建新的Scanner实例(Resetting a .nextLine() Scanner