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) { }
}
Related
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"/>
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.
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.
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();
}
Anyone knows why Logcat doesn't show full message?
I try to print xml response from my site but I only can get some of it..
Because your logcat has reached its max size, see What is the size limit for Logcat and how to change its capacity?
Try using this code to write your result into a text file in sdcard, then use DDMS to get it.
public static void appendLog(String text)
{
File logFile = new File("sdcard/log.txt");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
} catch (IOException e)
{
e.printStackTrace();
}
}
try
{
// BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
put your cursor on the error ,you will see the full log-cat info.