2011-04-23 98 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