我尝试保存一个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();
}
在编写问题时选择代码并按ctrl + k,这会自动缩进和着色代码,这样更具可读性。没有人会想要阅读那些没有缩进和无色的代码。 – 2011-04-23 20:16:13