Wie lösen Sie die java.nio.Datei.NoSuchFileException?

Habe ich eine Datei namens "Ergebnis.csv" aus, die Datei die ich Lesen möchte bestimmte Daten und zeigt Sie an. Ich habe die Datei in meinem eclipse-Projekt-Ordner selbst. Noch bin ich nicht in der Lage, die Datei zu Lesen.

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

AUSGABE:

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)

Kann mir jemand zeigen wo genau ich verpasst? und wie kann ich das umgehen oder Alternative Methoden für diese Methode?

  • Arbeiten Sie mit einem Maven-Projekt? Das problem ist, dass die Datei nicht auf Ihrem classpath, also, wenn Sie führen Sie Ihre Programm Java kann, nicht die Datei finden.
  • Eine Abhilfe wäre, nur verwenden Sie den vollständig qualifizierten Pfad zur Datei, z.B. C:\your_folder\project\result.csv. Die alternative ist, laden Sie es aus dem classpath.
InformationsquelleAutor Snkini | 2018-01-03
Schreibe einen Kommentar