2011-04-23 36 views
29

我嘗試保存一個txt文件,我的文件夾中,在內部存儲,但每次我面臨同樣的問題:節省內部文件中的Android

「源未找到」

我以不同的方式編寫我的代碼,如下所示,但在所有方面我都有同樣的問題。

值得一說,我甚至在的Manifest.xml這是沒有必要的內部存儲添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

這是不用說,我沒有任何問題,保存在/數據/數據/包/文件路徑的文件,但是當我加入我的文件夾中的文件的根例如/數據/ data/package/files/myforlder/myfile.txt我面對「未找到資源」問題。

你能指出我正確的方向來解決這個問題嗎?

第二個問題是,將文件保存在外部存儲的外部文件夾中。
(例如:SD卡或USB存儲器)是不同的還是相同的?

第一種方式:

OutputStreamWriter out; 
try { 
    File path=new File(getFilesDir(),"myfolder"); 
    File mypath=new File(path,"myfile.txt"); 
    if (!mypath.exists()) { 
     out = new OutputStreamWriter(openFileOutput(mypath.getAbsolutePath() , MODE_PRIVATE)); 
     out.write("test"); 
     out.close(); 
    }       
} 

方式二:

OutputStreamWriter out; 
try { 
    ContextWrapper cw = new ContextWrapper(this); 
    File path = cw.getDir("myfolder", Context.MODE_PRIVATE); 
    if (!path.exists()) { 
     path.createNewFile(); 
     path.mkdir(); 
    } 
    File mypath=new File(path,"myfile.txt"); 
    if (!mypath.exists()) { 
     out = new OutputStreamWriter(openFileOutput(mypath.getAbsolutePath() , MODE_PRIVATE)); 
     out.write("test"); 
     out.close(); 
    } 
} 

第三條道路:

File path=getFilesDir(); 
String mypath=path.toString() + "/myfolder"; 
OutputStreamWriter out; 
try { 
    File f = new File(mypath , "/myfile.txt" ); 
out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE)); 
    out.write("test"); 
    out.close();     
    } 

第四道:

File path=getFilesDir(); 

OutputStreamWriter out; 
    try { 
    File f = new File(path.getPath() + "/myfolder/myfile.txt" ); 
    out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE)); 
    out.write("test"); 
    out.close();      
    } 

第五方式:

File path=getFilesDir();     
OutputStreamWriter out; 
try { 
    File f = new File(path.getCanonicalPath() + "/myfile.txt"); 
    out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE)); 
    out.write("test"); 
    out.close();      
    } 
+1

在編寫問題時選擇代碼並按ctrl + k,這會自動縮進和着色代碼,這樣更具可讀性。沒有人會想要閱讀那些沒有縮進和無色的代碼。 – 2011-04-23 20:16:13

回答

55

第一種方式:

你沒有創建目錄。此外,您傳遞的絕對路徑爲openFileOutput(),這是錯誤的。

方式二:

您創建了所需的名稱,然後阻止你創建目錄的空文件。此外,您傳遞的絕對路徑爲openFileOutput(),這是錯誤的。

第三條道路:

你沒有創建目錄。此外,您傳遞的絕對路徑爲openFileOutput(),這是錯誤的。

第四道:

你沒有創建目錄。此外,您傳遞的絕對路徑爲openFileOutput(),這是錯誤的。

第五方式:

你沒有創建目錄。此外,您傳遞的絕對路徑爲openFileOutput(),這是錯誤的。

正確方法:

  1. 創建所需目錄File(例如,File path=new File(getFilesDir(),"myfolder");
  2. 呼叫mkdirs()File創建目錄,如果它不存在
  3. 創建了一個File輸出文件(例如,File mypath=new File(path,"myfile.txt");
  4. 使用standard Java I/O來寫入該File(例如,使用new BufferedWriter(new FileWriter(mypath))
+0

是在內部存儲器或SD卡中創建的文件和文件夾? – Shoshi 2012-12-28 15:31:13

+0

@Shoshi:'getFilesDir()'表示內部存儲。 – CommonsWare 2012-12-28 15:33:55

+0

thankx。 @CommonsWare – Shoshi 2012-12-28 17:41:05

10

節省:

public boolean saveFile(Context context, String mytext){ 
    Log.i("TESTE", "SAVE"); 
    try { 
     FileOutputStream fos = context.openFileOutput("file_name"+".txt",Context.MODE_PRIVATE); 
     Writer out = new OutputStreamWriter(fos); 
     out.write(mytext); 
     out.close(); 
     return true; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

負載:

public String load(Context context){ 
    Log.i("TESTE", "FILE"); 
    try { 
     FileInputStream fis = context.openFileInput("file_name"+".txt"); 
     BufferedReader r = new BufferedReader(new InputStreamReader(fis)); 
     String line= r.readLine(); 
     r.close(); 
     return line; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.i("TESTE", "FILE - false"); 
     return null; 
    } 
} 
1

Mintir4的答案是很好,我會在負載執行下列操作(以讀取所有行)

FileInputStream fis = myContext.openFileInput(fn); 
BufferedReader r = new BufferedReader(new InputStreamReader(fis)); 
String s = ""; 
while ((s = r.readLine()) != null) { 
    txt += s; 
} 
r.close(); 
+1

請編輯您的答案並將代碼格式化以使其可讀 – kleopatra 2012-11-07 09:43:20