Data saving management in text file using java - android

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.

Related

Write user readable file

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"/>

String read from file appears different than the text written to the file -Android

SOLVED: SEE BELOW
I'm trying to save some data to a file, so later I can access them.
However, when I write the string, and read it, then it looks good. But when I try to read from the file and display it on a Toast it doesn't show good.
The text is full of spaces.
Here is my code:
try {
fos = openFileOutput("1"+i, Context.MODE_PRIVATE);
DataOutputStream dos = new DataOutputStream(fos);
text = "{id"+ "1"+ i + "id}"+
"{amount" + Amount + "amount}"+
"{category"+ category + "category}"+
"{date"+ date.getText() + "date}"+
"{description"+ description.getText() + "description}";
dos.writeChars(text);
dos.close();
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
The second activity, when it opens fires this code:
try {
FileInputStream fis;
fis = openFileInput("11");
DataInputStream dis = new DataInputStream(fis);
Toast.makeText(getApplicationContext(),
dis.readUTF(), Toast.LENGTH_SHORT).show();
dis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
When the toast shows, the text looks something like this:
{ i d 1 1 i d } ...
EDIT: SOLVED
Instead of using
dos.writeChars(text);
I use: dos.writeUTF(text);
When you read in UTF you should make sure to also write in UTF

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) { }
}

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();
}

Why does LogCat not show the full message?

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.

Categories

Resources