Scanner class does not read an external file- Java

1

I am trying to use the class Scanner but, although I have tried several changes, it continues throwing FileNotFoundException , please if someone had an idea.

Here I put the code

package youtube;

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

public class scaner {
    public static void main(String[] args) throws FileNotFoundException {


         String fileName = "C:\Users\0d sat\Desktop\example.txt";

            File textFile = new File(fileName);

            Scanner in = new Scanner(textFile);

            int value = in.nextInt();
            System.out.println("Read value: " + value);

            in.nextLine();

            int count = 2;
            while(in.hasNextLine()) {
                String line = in.nextLine();

                System.out.println(count + ": " + line);
                count++;
            }

            in.close();
    }
}

throws:

Exception in thread "main" java.io.FileNotFoundException: C:\Users
package youtube;

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

public class scaner {
    public static void main(String[] args) throws FileNotFoundException {


         String fileName = "C:\Users\0d sat\Desktop\example.txt";

            File textFile = new File(fileName);

            Scanner in = new Scanner(textFile);

            int value = in.nextInt();
            System.out.println("Read value: " + value);

            in.nextLine();

            int count = 2;
            while(in.hasNextLine()) {
                String line = in.nextLine();

                System.out.println(count + ": " + line);
                count++;
            }

            in.close();
    }
}
d sat\Desktop\example.txt (El sistema no puede encontrar el archivo especificado) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.util.Scanner.<init>(Scanner.java:611) at youtube.scaner.main(scaner.java:15)
    
asked by Manuel Pérez 27.03.2017 в 19:42
source

3 answers

0

I leave you an example of reading a file, with Scanner, it is similar to your code:

String fileName = "C:\TEMP\folder space\test.txt";

File textFile = new File(fileName);

Scanner in = new Scanner(System.in);
try {
    in = new Scanner(textFile);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
int count = 2;
while (in.hasNextLine()) {
    String line = in.nextLine();
    System.out.println(line);

    count++;
}
in.close();
    
answered by 27.03.2017 в 20:06
0

FileNotFoundException may be because the file is not there or you do not have read permission. It could be this second case since you indicate a path inside the users directory of Windows, try to read a file in C: for example

    
answered by 27.03.2017 в 20:31
0

Verify well the path where your file is located the error is because you can not find it try to put it as the example that will leave you, try not to put space in the names of the folders.

You only have to create one instance of the scanner and you are ready to read the file.

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

/**
 *
 * Programa Java para leer archivos usando la clase de escáner en Java
 * @author
 */
public class ScannerExample {

    public static void main(String args[]) throws FileNotFoundException {

        //Crear una instancia de archivo para referenciar el archivo de texto en Java
        File text = new File("C:/TEMP/test.txt");

        //Creación de la instancia del escáner para leer el archivo en Java
        Scanner scnr = new Scanner(text);

        //Lectura de cada línea de archivo mediante la clase Escáner
        int lineNumber = 1;
        while(scnr.hasNextLine()){
            String line = scnr.nextLine();
            System.out.println("Linea " + lineNumber + " :" + line);
            lineNumber++;
        }       

    }   

}

Note: you can put the route as I put it if you are in the unit C: (C: /TEMP/test.txt)

    
answered by 27.03.2017 в 20:32