我尝试在我的文件夹中,在内部存储中保存txt文件,但每次都遇到同样的问题:
"未找到来源"
我用不同的方式编写我的代码,如下所示,但在各方面我都有同样的问题.
值得一提的是,我甚至补充道
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在Manifest.xml中,内部存储不需要.
不用说我在/ data/data/package/files路径中保存文件没有任何问题,但是当我在文件的根目录中添加我的文件夹时,例如/ data/data/package/files/myforlder /myfile.txt我面临"未找到来源"问题.
你能指出我正确的方向来解决这个问题吗?
第二个问题是,用于将文件保存在外部存储器中的外部文件夹中.
(例如:sdCard或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();
}
}
Run Code Online (Sandbox Code Playgroud)
第二种方式:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
第三种方式:
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();
}
Run Code Online (Sandbox Code Playgroud)
第四种方式:
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();
}
Run Code Online (Sandbox Code Playgroud)
第五种方式:
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();
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 62
第一种方式:
您没有创建目录.此外,您正在传递绝对路径openFileOutput(),这是错误的.
第二种方式:
您创建了一个具有所需名称的空文件,这会阻止您创建目录.此外,您正在传递绝对路径openFileOutput(),这是错误的.
第三种方式:
您没有创建目录.此外,您正在传递绝对路径openFileOutput(),这是错误的.
第四种方式:
您没有创建目录.此外,您正在传递绝对路径openFileOutput(),这是错误的.
第五种方式:
您没有创建目录.此外,您正在传递绝对路径openFileOutput(),这是错误的.
正确的方式:
File为您想要的目录创建一个(例如File path=new File(getFilesDir(),"myfolder");)mkdirs()它File来创建目录(如果它不存在)File为输出文件创建一个(例如File mypath=new File(path,"myfile.txt");)File(例如,使用new BufferedWriter(new FileWriter(mypath)))Bet*_*das 12
保存:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
加载:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92092 次 |
| 最近记录: |