找不到文件

时间:2011-10-04 12:47:54

标签: java file csv filenotfoundexception

private void readRandomContacts() throws IOException 
{

BufferedReader bufRdr;
contacts = new ArrayList<Contacts>();

File randomContactsFile = new File("C:\randomContacts.csv");
try {
     bufRdr = new BufferedReader(new FileReader(randomContactsFile));
     String line = null;
     String[] a = new String[2];

        while ((line = bufRdr.readLine()) != null)
                {
                    a = line.split(",");
                    Contacts c = new Contacts(a[0], a[1], a[1], a[1], a[2]);
                    contacts.add(c);
                }

    } catch (FileNotFoundException e) {
        Log.d("file not found", "check");
        e.printStackTrace();
    }

我似乎无法找到该文件,并且randomContacts.csv确实存在于C目录中。有什么帮助吗?

3 个答案:

答案 0 :(得分:3)

尝试:

File randomContactsFile = new File("C:\\randomContacts.csv");
                                      ^^  these two are important

有关字符和转义序列的一些信息,请参阅here

答案 1 :(得分:1)

File randomContactsFile = new File("C:\randomContacts.csv"); 

应该是

File randomContactsFile = new File("C:\\randomContacts.csv"); 

您需要转义\,否则,java会将\r作为回车符读取。

答案 2 :(得分:0)

你也可以这样做(前言):

new File("C:/randomContacts.csv")