0

我有一个名为“input.txt”的文件,我想从该文件中读取某些数据并显示它们。我的 Eclipse 项目文件夹本身中有该文件。我仍然无法读取文件。

我知道它之前已经回答了,但对于我的生活,我似乎无法解决问题,任何帮助都非常感谢。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

class FileOperations {

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        String filename;
        System.out.print("Enter file name: ");
        filename = input.next();
        String txt = new String(Files.readAllBytes(Paths.get(filename)));
        String[] lines = txt.split("\n");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            if (line.charAt(line.length() - 1) == '\r' || line.charAt(line.length() - 1) == '\n')
                lines[i] = line.substring(0, line.length() - 1);
        }

        int result;
        do {
            System.out.println("\nMenu:\n1.Read and count a specific pattern of the file\n" +
                    "2.Read and display a specific line from the file\n" +
                    "3.Sort the file alphabetically (ascending)\n" +
                    "0.Exist");
            result = input.nextInt();
            input.nextLine();  // Consume newline left-over

            if (result == 1) {
                String pattern;
                System.out.print("Enter a pattern: ");
                pattern = input.nextLine();
                int counter = 0;
                for (String line : lines) {
                    counter += line.split(pattern, -1).length - 1;
                }
                if (counter > 0)
                    System.out.println("\nPattern (" + pattern + ") was found " + counter + " times.");
                else
                    System.out.println("\nPattern (" + pattern + ") was not found!");

            } else if (result == 2) {
                String line;
                System.out.print("Enter a line: ");
                line = input.nextLine();
                for (int i = 0; i < lines.length; i++) {
                    if (lines[i].equals(line)) {
                        System.out.println("\nLine (" + line + ") was found at index " + (i + 1) + " .");
                        break;
                    }
                    if (i == lines.length - 1)
                        System.out.println("\nLine (" + line + ") was not found!");
                }

            } else if (result == 3) {
                for (int i = 0; i < lines.length - 1; i++) {
                    for (int j = i + 1; j < lines.length; j++) {
                        if (lines[i].compareTo(lines[j]) > 0) {
                            String temp = lines[i];
                            lines[i] = lines[j];
                            lines[j] = temp;
                        }
                    }
                }

                StringBuilder text = new StringBuilder();
                for (String line : lines) {
                    text.append(line).append("\n");
                }
                Files.write(Paths.get(filename), (text + "").getBytes());
                System.out.println("\nFile is sorted alphabetically.");
            } else {
                System.out.println("\nProgram Existed!");
                result = 0;
            }
        } while (result != 0);
    }
}

输出:

Enter file name: input

Exception in thread "main" java.nio.file.NoSuchFileException: input
    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
    at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:236)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:375)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:426)
    at java.base/java.nio.file.Files.readAllBytes(Files.java:3272)
    at FileOperations.main(FileOperations.java:14)

谁能指出我到底错过了什么?我该如何克服这种方法或这种方法的任何替代方法?

4

1 回答 1

0

您的问题是input.next()返回“input”,而不是“input.txt”,因为您使用的是处理“。”的扫描仪。作为分隔符。也许您想阅读整行

编辑:假设您输入了“input.txt”(上面的输出只显示“input”)。如果你没有,请做。

于 2021-09-12T20:24:48.287 回答