0

I have a file called "input.txt", from that file i want to read certain data and display them. I have that file in my eclipse project folder itself. Still i'm unable to read the file.

I know its been answered before but for the life of me I can't seem to solve the problem , any help is greatly appreciate it.

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);
    }
}

Output:

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)

Can anyone point where exactly i missed? and how can i overcome this or any alternate methods for this method?

Oboe
  • 2,643
  • 2
  • 9
  • 17
  • 1
    The error message tells you you're trying to open a file "input", not "input.txt'. `Exception in thread "main" java.nio.file.NoSuchFileException: input` – user16632363 Sep 13 '21 at 01:44
  • Instead of Files.readAllBytes, use [Files.readAllLines](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/file/Files.html#readAllLines(java.nio.file.Path)). – VGR Sep 13 '21 at 12:49

1 Answers1

0

Your problem is that input.next() returns "input", not "input.txt" as you are using Scanner, which treats "." as a delimiter. Perhaps you wanted to read the entire line?

EDIT: assuming that you entered "input.txt" (the output above only displays "input"). If you didn't, please do.

ewramner
  • 5,810
  • 2
  • 17
  • 33