节省内部文件在我的Android自己的内部文件夹 [英] Save internal file in my own internal folder in Android
问题描述
我尝试保存一个txt文件在我的文件夹,在内部存储,但每次我面临同样的问题:
源未找到
我写我的code在这里pssed如下不同的方式前$ P $,但在各方面我有同样的问题。
值得一说,我甚至补充
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
在的Manifest.xml这是没有必要的内部存储
这是不用说,我没有任何问题,来保存的 /数据/数据/包/文件的文件的路径,但是当我加入我的文件夹中的文件的根目录,例如 /data/data/package/files/myforlder/myfile.txt 的我面对找不到源代码的问题。
你能指出我朝着正确的方向来解决这个?
第二个问题是,用于保存文件在外部存储的外部文件夹。 (例如:SD卡或USB存储)?是该方案的不同或者是相同的。
第一种方式:
OutputStreamWriter出来;
尝试 {
文件路径=新的文件(getFilesDir(),MyFolder的);
文件mypath中=新的文件(路径,myfile.txt的);
如果(!mypath.exists()){
OUT =新OutputStreamWriter(openFileOutput(mypath.getAbsolutePath(),MODE_PRIVATE));
out.write(测试);
out.close();
}
}
方式二:
OutputStreamWriter出来;
尝试 {
ContextWrapper CW =新ContextWrapper(本);
文件路径= cw.getDir(MyFolder的,Context.MODE_PRIVATE);
如果(!path.exists()){
path.createNewFile();
path.mkdir();
}
文件mypath中=新的文件(路径,myfile.txt的);
如果(!mypath.exists()){
OUT =新OutputStreamWriter(openFileOutput(mypath.getAbsolutePath(),MODE_PRIVATE));
out.write(测试);
out.close();
}
}
第三条道路:
文件路径= getFilesDir();
字符串mypath中= path.toString()+/ MyFolder的;
OutputStreamWriter出来;
尝试 {
文件F =新的文件(mypath中,/myfile.txt);
OUT =新OutputStreamWriter(openFileOutput(f.getPath(),MODE_PRIVATE));
out.write(测试);
out.close();
}
第四道:
文件路径= getFilesDir();
OutputStreamWriter出来;
尝试 {
文件F =新的文件(path.getPath()+/myfolder/myfile.txt);
OUT =新OutputStreamWriter(openFileOutput(f.getPath(),MODE_PRIVATE));
out.write(测试);
out.close();
}
第五方式:
文件路径= getFilesDir();
OutputStreamWriter出来;
尝试 {
文件F =新的文件(path.getCanonicalPath()+/myfile.txt);
OUT =新OutputStreamWriter(openFileOutput(f.getPath(),MODE_PRIVATE));
out.write(测试);
out.close();
}
第一种方式:
您没有创建目录。此外,要传递的绝对路径 openFileOutput()
,这是不对的。
方式二:
您创建了所需的名称,然后$ P $从创建目录pvented你一个空文件。此外,要传递的绝对路径 openFileOutput()
,这是不对的。
第三条道路:
您没有创建目录。此外,要传递的绝对路径 openFileOutput()
,这是不对的。
第四道:
您没有创建目录。此外,要传递的绝对路径 openFileOutput()
,这是不对的。
第五方式:
您没有创建目录。此外,要传递的绝对路径 openFileOutput()
,这是不对的。
正确方法:
- 创建一个
文件
您所需的目录(例如,文件路径=新的文件(getFilesDir(),MyFolder的);
) - 呼叫
mkdirs()
上文件
以创建目录,如果它不存在 - 创建一个
文件
输出文件(例如,文件mypath中=新的文件(路径,myfile.txt的);
) - 使用标准的Java I / O 写了
文件
(例如,使用新的BufferedWriter(新的FileWriter(mypath中))
)
I try to save a txt file in my folder, in internal storage, but I face the same problem every time:
"Source Not Found"
I write my code in different ways expressed here as follows but in all ways I have the same problem.
It is worth saying that I even add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in Manifest.xml that is not necessary for internal storage.
It is needless to say that I don't have any problem to save files in /data/data/package/files path, but when I add my folder in the files' root for example /data/data/package/files/myforlder/myfile.txt I face the "Source Not Found" problem.
Could you point me in right direction to solve this ?
The second question is that, for saving a file in an external folder in external storage.
(for example: sdCard or USB storage) is the scenario different or is it same ?
First Way:
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();
}
}
Second way:
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();
}
}
Third way:
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();
}
Fourth Way:
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();
}
Fifth way:
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();
}
First Way:
You didn't create the directory. Also, you are passing an absolute path to openFileOutput()
, which is wrong.
Second way:
You created an empty file with the desired name, which then prevented you from creating the directory. Also, you are passing an absolute path to openFileOutput()
, which is wrong.
Third way:
You didn't create the directory. Also, you are passing an absolute path to openFileOutput()
, which is wrong.
Fourth Way:
You didn't create the directory. Also, you are passing an absolute path to openFileOutput()
, which is wrong.
Fifth way:
You didn't create the directory. Also, you are passing an absolute path to openFileOutput()
, which is wrong.
Correct way:
- Create a
File
for your desired directory (e.g.,File path=new File(getFilesDir(),"myfolder");
) - Call
mkdirs()
on thatFile
to create the directory if it does not exist - Create a
File
for the output file (e.g.,File mypath=new File(path,"myfile.txt");
) - Use standard Java I/O to write to that
File
(e.g., usingnew BufferedWriter(new FileWriter(mypath))
)
这篇关于节省内部文件在我的Android自己的内部文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!