2015-12-29 96 views
0

因此,我在本地存儲中有一個.txt文件,它是一個簡單的文本文件。文本基本上只是一系列的線條。Android/java無法從本地存儲中的.txt文件讀取

我正在使用下面的代碼來嘗試讀取文本文件(我在調用此方法之前驗證文件是否存在)。

public static String GetLocalMasterFileStream(String Operation) throws Exception { 

//Get the text file 
    File file = new File("sdcard/CM3/advices/advice_master.txt"); 
    if (file.canRead() == true) {System.out.println("-----Determined that file is readable");} 

    //Read text from file 
    StringBuilder text = new StringBuilder(); 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     text.append(line); 
     System.out.println("-----" + line); //for producing test output 
     text.append('\n'); 

    } 
    br.close(); 
    System.out.print(text.toString()); 
    return text.toString(); 

} 

的代碼產生日誌

在----確定文件是可讀 但是,這是唯一的輸出的文件數據不被寫入到日誌

我也試過在while循環之前插入以下內容以嘗試只讀第一行

line = br.readLine(); 
System.out.println("-----" + line); 

產生以下的輸出:

-----空

回答

0

檢查了這一點getExternalStorage

 File path = Environment.getExternalStorageDirectory(); 
    File file = new File(path, "textfile.txt"); 
//text file is copied in sdcard for example 
0

嘗試在文件路徑/ SD卡添加鉛斜線/ CM3/advices/advice_master.txt

File file = new File("/sdcard/CM3/advices/advice_master.txt"); 
+0

添加的斜槓字符,它並沒有區別 –

0

試試這個。只需傳遞txt文件名作爲參數...

public void readFromFile(String fileName){ 
    /* 
    InputStream ips; 
    ips = getClass().getResourceAsStream(fileName); 

    //reading 
    try{ 
     InputStreamReader ipsr = new InputStreamReader(ips); 
     BufferedReader br = new BufferedReader(ipsr); 
     String line; 

     while ((line = br.readLine())!=null){ 
      //reading goes here ;) 
     } 
     br.close(); 
    } 
    catch (Exception e){ 
     System.out.println(e.toString()); 
    } 
    */ 
// or try this 
File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"file.txt"); 

//Read text from file 
StringBuilder text = new StringBuilder(); 

try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     text.append(line); 
     text.append('\n'); 
    } 
    br.close(); 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
    } 


} 
+0

嘗試這樣做,沒有我的 –

+0

的作品,但你可以嘗試另一種變體,我已經加入其他變體還 –

+1

我想你的變體光盤,並沒有奏效。但我看到你的評論對你有用。所以我創建了一個名爲test.txt的不同文件,其中包含3行代碼,將它放在sd卡和本地存儲的根目錄下,然後運行。 我做了一些測試,發現Environment.getExternalStorageDirectory();實際上指向設備的內部存儲而不是SD卡。 無論如何,我然後用舊文件替換新文件,它不起作用。然後我回到我的ORIGONAL代碼,將它指向新的txt文件,它工作。問題是自己的.txt文件 –

0

讓我改進我的答案。你可以嘗試另一種方式來閱讀advice_master.txt中的所有行,看看會發生什麼。它確保可以讀取所有文件內容。

Charset charset = Charset.forName("ISO-8859-1"); 
try { 
    List<String> lines = Files.readAllLines(Paths.get(YOUR_PATH), charset); 

    for (String line : lines) { 
    System.out.println(line); 
    } 
} catch (IOException e) { 
    System.out.println(e); 
} 
+0

好吧,我試過你的建議,我有以下java.io.FileNotFoundException:/系統/存儲/ SD卡/ CM3 /建議/ advice_master。txt:打開失敗:ENOENT(沒有這樣的文件或目錄) 文件路徑是正確的,因爲當我將其更改爲別的東西時,它確實會拋出找不到文件的錯誤, –