2011-10-04 161 views
0
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目錄中。請幫忙嗎?找不到文件

+0

是它拋出一個異常? – smp7d

+0

是一個filenotfound異常! :/ – michelle

回答

3

嘗試:

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

有關人物的一些信息,請參閱here和轉義序列。

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

應該

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

你需要逃避\,否則,JAVA會讀\r爲回車。

+0

我試過..沒有:/ – michelle

+0

10-04 14:54:47.009:WARN/System.err(333):java.io.FileNotFoundException:/C:/test/randomcontacts.txt(沒有這樣的文件或目錄) – michelle

0

,你也可以做到這一點(FRONTSLASH):

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