alex wilhelm
alex wilhelm

Reputation: 233

where to write a text file and how to set it up for an sd card

I know this is a popular question but I have looked at all of the other responses and none of them seem to work. What I want to do is write some code to a text file. My first question: is there a way to view that text file without writing code to the console? Second, I dont know where in my phone it goes and I want to see it to help trouble shoot, so if you know how to do that too, it would be great. So now I will give you an overview of what is happening. When I start my program it checks to see if the file exists, if it doesn't it reads a file out of my assets folder and copies that info and sends it to a file into the sd card. If it exits it reads the info from the sd card. Next if I press a button and change numbers and print it to my sd card again then I close it using task managers then when I come back the original information is here. I don't feel like it is being able to find my sd card location. So far I have used.

File outfilepath = Environment.getExternalStorageDirectory();
String FileName = "ExSettings.txt" ;
File outfile = new File(outfilepath.getAbsolutePath()+"/TimeLeft/"+FileName);

File outfilepath = Environment.getExternalStorageDirectory();
String FileName = "ExSettings.txt" ;
File outfile = new File(outfilepath, FileName);

Any Ideas?

Upvotes: 1

Views: 427

Answers (1)

Matt Clark
Matt Clark

Reputation: 28589

This is a function that I wrote which will take in a list array and write to a file line by line.

It will use the path in fileName to make a new folder called myfolder in the root of the SDCard, an inside will be your file of newtextfile.txt.

List<String> File_Contents = new ArrayList<String>();

File_Contents.add("This is line one");
File_Contents.add("This is line two");

File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfolder/newtextfile.txt");
f.mkDirs();

try {
    BufferedWriter out = new BufferedWriter(new FileWriter(f));
    for (int x = 0; x < File_Contents.size(); x++) {
        out.write(File_Contents.get(x));
        out.write(System.getProperty("line.separator"));
    }
    out.close();
    return true;
} catch (Exception e) {
    return false;
}

Upvotes: 2

Related Questions