Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();                    
    }
question from:https://stackoverflow.com/questions/5766609/save-internal-file-in-my-own-internal-folder-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
155 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

  1. Create a File for your desired directory (e.g., File path=new File(getFilesDir(),"myfolder");)
  2. Call mkdirs() on that File to create the directory if it does not exist
  3. Create a File for the output file (e.g., File mypath=new File(path,"myfile.txt");)
  4. Use standard Java I/O to write to that File (e.g., using new BufferedWriter(new FileWriter(mypath)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...