开发者

No line found Exception in java with Scanner

I want to read in lines from a .txt file. However while the file is found, I can't access its content.

import java.io.*;
import java.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public static void main(String[] args) {
try {
    File myObj = new File("C:\\Daten\\workspaces\\my_file.txt");
    System.out.println(myObj);
    Scanner myReader = new Scanner(myObj);
    System.out.println("test1");
    
    System.out.println(myObj.getName());
            
    String insertInto = "INSERT INTO ";
    insertInto = insertInto.concat(myObj.getName() +" ( ");
                        
    //get header
    System.out.println("hast next ?");
    System.out.println(myReader.hasNextLine());
    String header = myReader.nextLine();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

and this causes the following output:

C:\Daten\workspaces\my_file.txt
test1
my_file.txt
hast next ?
false

... threw exception
java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
...

The file is a tab limited text file and I rechecked the path multiple times: it is at this location and is not empty.

At this point I really don't know anymore what the probl开发者_如何学Goem is and I feel like I'm searching in the wrong place, but I don't know where I'm supposed to search.

Thank you for your answers in advance.


I found the problem. Somehow the file was corrupted or so. I tried a new different dummy file and it could read the lines. So I then i simply copied the content of the my_file.txt into a new .txt and it worked. I still have no idea why though.

PS: Thanks for your help Wombat.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜