0

I can read file using java.io and java.util.Scanner but I don't know how to read file using only java.util.Scanner:

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        String filePath = "C:\\IdeaProjects\\test\\src\\input.txt";
        Scanner sc = new Scanner(new File(filePath));
        int a, b;
        a = sc.nestInt();
        b = sc.nextInt();
    }
}

Can someone help?

1

3 Answers 3

3

Since Scanner requires a java.io.File object, I don't think there's a way to read with Scanner only without using any java.io classes.

Here are two ways to read a file with the Scanner class - using default encoding and an explicit encoding. This is part of a long guide of how to read files in Java.

Scanner – Default Encoding

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

public class ReadFile_Scanner_NextLine {
  public static void main(String [] pArgs) throws FileNotFoundException {
    String fileName = "c:\\temp\\sample-10KB.txt";
    File file = new File(fileName);

    try (Scanner scanner = new Scanner(file)) {
      String line;
      boolean hasNextLine = false;
      while(hasNextLine = scanner.hasNextLine()) {
        line = scanner.nextLine();
        System.out.println(line);
      }
    }
  }
}

Scanner – Explicit Encoding

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

public class ReadFile_Scanner_NextLine_Encoding {
  public static void main(String [] pArgs) throws FileNotFoundException {
    String fileName = "c:\\temp\\sample-10KB.txt";
    File file = new File(fileName);

    //use UTF-8 encoding
    try (Scanner scanner = new Scanner(file, "UTF-8")) {
      String line;
      boolean hasNextLine = false;
      while(hasNextLine = scanner.hasNextLine()) {
        line = scanner.nextLine();
        System.out.println(line);
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

if you want to read the file until the end:

String filePath = "C:\\IdeaProjects\\test\\src\\input.txt";
File file = new File(filePath);

    try {

        Scanner sc = new Scanner(file);

        while (sc.hasNextLine()) {
            int i = sc.nextInt();
            System.out.println(i);
        }
        sc.close();
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }

p.s If every line have only Integer I suggest you to use Integer.parseInt(sc.readLine());
instead of sc.nextInt();

If you cant read please send me file Context

Comments

-1

Well, if you are using a Mac, you type this into the terminal file.java<input.txt and to output to a file you type this: file.java>output.txt output.txt is a non-existing file, while input.txt is a pre-existing file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.