Write user readable file - android

I am writing values to my database and stop the time it needs for an evaluation.
Now I would like to write these times to an easy accessible file like a .txt but I can not write on the phone (some answers on the internet say because it is connected as "media source" but when I disconnect it I can not connect to eclipse anymore).
So the question is: How can I write a file which I can simply copy from my phone to my PC to get the data to analyze them.

you can use below function, text is your content:
public void backUp(String text,String namefile)
{
File file = new File(Environment.getExternalStorageDirectory()+"/yourdir/"+namefile+".txt");
if (!file.exists())
{
try
{
file.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
updated :
you should create your directory before use above function :
File yourdir = new File(Environment.getExternalStorageDirectory()+"/yourdir");
if(!yourdir.exists()){
yourdir.mkdir();
}
then :
backUp("hello world", "test");
don't forget this permission :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Related

file has been created but when opened nothing is in it

I have an android app and simply i need to store data in a file. I need to use external storage to make it available for other applications too.
im using this piece of code
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter("/storage/sdcard0/download/themagicfile.txt", true));
bw.write("hi");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
which i got from http://stackoverflow.com/questions/10041007/write-to-txt-file-but-not-overwrite
I can see the file in the directory but when i open it there is nothing in it. i need to use FileWriter to make it appendable.
any suggestion
you should also call
bw.flush();
bw.close();
rembember to add the WRITE_EXTERNAL_STORAGE permission to the AndroidManifest.xml file
try {
bw = new BufferedWriter(new FileWriter("/storage/sdcard0/download/themagicfile.txt", true));
bw.write("hi");
bw.flush();
} (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} (IOException e) { }
}

Checking where the file is stored in android?

I am writing a log data to the File in android. But I am unable to understand where this file is stored on my android device ( google nexsus 7 ). Where and How should I look for the contents of the file ?
Following is the piece of code I am using to write log data to the file.
public File AppendingLog(String LogData){
try {
File logFile = new File(Environment.getExternalStorageDirectory(),
"yourLog.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(LogData);
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return logFile;
}
android.util.Log.d("myapp", logFile.toString());
On most devices, it will be in /sdcard/. The exact path will be /sdcard/yourLog.txt.
As you are using Environment.getExternalStorageDirectory(), the actual path will differ between devices. Some devices return the external sd card path, but most will return the internal sd card's path, which usually is /sdcard/.
I think answers below are what you need.
You can also download Astro File Manager App which will also give you an idea where your files go on Android System.

Data saving management in text file using java

I wrote an android application which collect data from sensors and saves the collected data in a text file for plotting and for other purpose after that,
the problem is to save the collected data in specific format into text file
for example: I need to save the data inside text file as following format:
1,34
2,40
3,56
4,66
.
.
.
... and so on.
But the data is stored with me as the following:
1,342 403 564 66
which this is the problem.
The writing function looks like this:
public void writing_in_file_1(){
try{
//file_1.createNewFile();
fw = new FileWriter(file_1, true);
bw = new BufferedWriter(fw);
out = new PrintWriter(bw);
out.append(String.valueOf(time + "\t "+currentReading ) );
out.append("\n");
//out.append(String.valueOf(current_reading_list));
out.flush();
Toast.makeText(
this,
"Done writing SD 'specific text file'",
Toast.LENGTH_SHORT)
.show();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
You can use a BufferedWriter.newLine() method here:
BufferedWriter buf = new BufferedWriter(new FileWriter(Filename, true));
buf.append(text);
buf.newLine();
buf.close();
Also, my guess is you don't really need the PrinterWriter class there.

Working with files: read and write many times

I am newbie in Android development and I cant know that is going with files.
That's about audio information.
So, if I create new file and write in it, after it i read from this file.
And new iteration: I don't create new file (cuz i already got this file), and write in this file. Now I gonna read from file (after second write) that I can get from file?
I need get second written information, can I get it on this way?
yes, you have read again the file
let say you use a textfile to save your data.
Ex.
this is how i save it
File temf=new File(getCacheDir()+"/data/mytext.txt");
Writer out=null;
if(temf.exists()){
temf.delete();
}
try {
out = new BufferedWriter(new FileWriter(temf));
out.write("sample text") + "\r\n");
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
and this is how i retrieve it
FileInputStream fs;
try {
File temf=new File(getCacheDir()+"/data/mytext.txt");
if (temf.exists()) {
fs = new FileInputStream(temf);
DataInputStream dis = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(
dis));
String str=br.readLine();
while(str!=null)
{
if(str=="text you want to compare"){
break;
}else{
str=br.readLine();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Android filewrite nullpointer exception

In my app there are 3 EditTexts. I want to write the content of this EditTexts to a file, but the filewrite throws a nullpointer exception. Why?
OutputStream f1; is declared globally.
BtnSave = (Button)findViewById(R.id.Button01);
BtnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
intoarray = name + "|" + number + "|" + freq + "\n";
Toast.makeText(Main.this, "" + intoarray, Toast.LENGTH_SHORT).show();
//so far so good
byte buf[] = intoarray.getBytes();
try {
f1 = new FileOutputStream("file2.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f1.write(buf); //nullpointer exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Most likely
f1 = new FileOutputStream("file2.txt");
failed and since you caught the exception f1 remained null. In most cases in Android you can only create files either in your application data directory or external storage.
The way you are currently using this won't work, generally you are trying to write to internal storage, which is private to your app and must be contained within your applications directory.
The proper way to create the file stream is
fin = openFileOutput("file2.txt", Context.MODE_PRIVATE); // open for writing
fout = openFileInput("file2.txt", Context.MODE_PRIVATE); // open for reading
Which will locate the file in your storage area for your application, which is typically something like
/data/data/com.yourpackagename/files/...
You can still create directories within your applications area if you need a directory structure of course.
If you need to write to external storage that's a different process, for more information see Android Data Storage
Sorry for all you were trying help me, I asked the wrong question. I wanted to use internal storage (and it is now working). I don't know what the problem is, but the with the code below (that i have used a lot) filewrite is ok:
try {
File root = Environment.getExternalStorageDirectory();
File file = new File(root, "Data.txt");
if (root.canWrite()) {
FileWriter filewriter = new FileWriter(file, true);
BufferedWriter out = new BufferedWriterfilewriter);
out.write(intoarray);
out.close();
}
} catch (IOException e) {
Log.e("TAG", "Could not write file " + e.getMessage());
}
I would delete the topic if I could. I accept this answer to close the topic.
Thanks, anyway.

Categories

Resources