Snkini
Snkini

Reputation: 641

How to solve the java.nio.file.NoSuchFileException?

I have a file called "result.csv", 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.

 public static void main(String [] args) {
    int i=0;
    String filename="result.csv";
    Path pathToFile = Paths.get(filename);

    try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) { 
        // read the first line from the text file 
        String line = br.readLine(); 
        // loop until all lines are read 
        while (i<10) { 
            // use string.split to load a string array with the values from 
            // each line of 
            // the file, using a comma as the delimiter
            String[] attributes = line.split(","); 
            double x=Double.parseDouble(attributes[8]);
            double y=Double.parseDouble(attributes[9]);
            System.out.println(GeoHash.withCharacterPrecision(x, y, 10));


            // read next line before looping 
            // if end of file reached, line would be null 
            line = br.readLine(); 
            i++;
        } 
    } catch (IOException ioe) { 
            ioe.printStackTrace(); 
    } 
}

OUTPUT:

java.nio.file.NoSuchFileException: result.csv
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.spi.FileSystemProvider.newInputStream(Unknown Source)
at java.nio.file.Files.newInputStream(Unknown Source)
at java.nio.file.Files.newBufferedReader(Unknown Source)
at com.uvce.cse.searchiot.geohash.TestGeoHash.main(TestGeoHash.java:19)

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

Upvotes: 32

Views: 256223

Answers (7)

ashique pc
ashique pc

Reputation: 79

this can also happen when the you dont specify full file path. eg: /home/ubuntu/kafka_2.13-2.8.1/bin/client.properties this works for me

Upvotes: 1

Miguel Tom&#225;s
Miguel Tom&#225;s

Reputation: 1911

Not discarding all possible solutions here, this error also occurs when your running Android Studio on Windows environment and using a project directory on an external hard drive formatted with other than NFTS. ]

If this is the case, simply move your project into the main HDD (NTFS) and reload the project again , this time from the main HDD folder path.

Upvotes: 0

Dovydas Giršvaldas
Dovydas Giršvaldas

Reputation: 243

I had the same error caused by escaped characters on windows file path. For example, my application was looking for "C:\Users\david\my%20folder%20name\source.txt" meanwhile the real path was "C:\Users\david\my folder name\source.txt".

Upvotes: 0

DodgyCodeException
DodgyCodeException

Reputation: 6123

The problem is that your default directory at application startup is not what you think it is. Try adding the following line to your code, just after you create the path:

public static void main(String [] args) {
    int i=0;
    String filename="result.csv";
    Path pathToFile = Paths.get(filename);
    System.out.println(pathToFile.toAbsolutePath());

That way, you'll see exactly where it is looking for the file.

How to fix it is your decision. You can use a full path spec instead of just a filename, or put the filename in a special "Resources" directory and reference it using a relative path, or move the file to wherever your default directory is.

Upvotes: 41

myhe
myhe

Reputation: 93

If your file("result.csv") in the src directory, you should use the "src/result.csv" instead of "result.csv".

Upvotes: 9

Nimesh Bumb
Nimesh Bumb

Reputation: 1

If you're a MacOSX user please type the file path manually instead of copying it from "Get Info". You'll get something like this if you copied it from "Get Info": /Users/username<200e><2068><2068>/Desktop<2069>/source.txt

Upvotes: -1

Alex Cuadr&#243;n
Alex Cuadr&#243;n

Reputation: 688

The problem there is that java isn't able to find the "result.csv" file in the project folder. Thus, try to use the fully qualified path to the file, e.g. C:\your_folder\project\result.csv in the Path variable. Also I think It would be better to use bufferedreader like this: BufferedReader br = new BufferedReader(new FileReader(insert here the String in which is defined the path to the file)); Check the uses of BufferedReader here

Upvotes: 0

Related Questions