Android : CSV not storing values in normal format - android

I am trying to store values in csv file. But it is storing all those values in exponential format.
FileOutputStream fos;
try {
File dir = new File("/sdcard/");
boolean b = dir.mkdir();
File myFile = new File(dir, filename);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
for (String s : arrayListDGId) {
myOutWriter.append(s + "\n");
}
myOutWriter.close();
fOut.close();
These are the example values of arraylist 10989808,8768762,76876787. But it stores/converts those values in exponential form like 1E+9, 8E+8 & so on format. I want to store those in there normal/original format. Plz help me

If your array contains ints like:
ArrayList<Integer> arrayListDGId
You can write it like this:
for (Integer i : arrayListDGId) {
myOutWriter.append(String.format("%d%n", i));
}
For more Information on how to format numbers look here: http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Related

OutputStreamWriter does not append

Original code and its working saving the data to the SD Card
// Writing data to internal storage
btnSaveData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isSDCardWritable()) {
String dataToSave = etData.getText().toString();
try {
// SD Card Storage
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()+"/MyFiles");
directory.mkdirs();
File file = new File(directory, "text.txt");
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
// write the string to the file
osw.write(dataToSave);
osw.flush();
osw.close();
. . .
And then I changed the code to append a new values as it should be according to what I need:
osw.append(dataToSave);
osw.flush();
osw.close();
Problem is: It overwrites the text file instead of appending. What did I miss? Thanks for helping
Constructor FileOutputStream( File file ) always overwrites file. If you want to append to file you have to use more generic constructor FileOutputStream( File file, boolean append ). If you set parameter 'append' to true file is not overwritten.

Store Multiple values in a file in table format

I would like to store my values as a textfile and hence i used
try
{
File Mydir = new File("/sdcard/app/");
Mydir.mkdirs();
File outputFile = new File(Mydir, "helloworld");
FileOutputStream fos = new FileOutputStream(outputFile);
fos.write(item1.getBytes());
fos.write(item2.getBytes());
// Close output stream
fos.flush();
fos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
This is working extremely well and the values are storing properly. But now my question is i would like to store large number of values in a single file and i would like to store those values in the table format. Here the values are getting over writted one above and the last used values is only displaying. So instead of this i would like to store all the used values in a table format.
I didn't understood the requirement of writing in tabular format but if you simply want that the new text don't overwrite the existing text in file , or simple append the new text in file , you should use
FileOutputStream fos = new FileOutputStream(outputFile,true);
instead of
FileOutputStream fos = new FileOutputStream(outputFile);
i.e.
try
{
File Mydir = new File("/sdcard/app/");
Mydir.mkdirs();
File outputFile = new File(Mydir, "helloworld");
FileOutputStream fos = new FileOutputStream(outputFile, true);
fos.write(item1.getBytes());
fos.write(item2.getBytes());
// Close output stream
fos.flush();
fos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
Edit
For current system Date and time
SimpleDateFormat ft =new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss:S a zzz");
Date date= new Date();
String nowTime = ft.format(date);
concatenate nowTime with item1 and item2 before writing it on the file

How to append data to text files in the new line, ( \n does not work)

I am using this code as a part to write data to a text file, but I am not sure how I can append the data to the new line. "\n" does not seem to be working. Here, variable "data" has this format:
t=1, x=-3.1, y=19.0, z=-8.6
this part of the code iterates and the variable "data" is written each time; the current result is something like:
t=1, x=-6.9, y=-9.6, z=-6.9t=2, x=-1.4, y=6.2, z=7.0t=3, x=-1.4, y=6.1, z=6.9t=4, and so on, but what I would like is:
t=1, x=-6.9, y=-9.6, z=-6.9
t=2, x=-1.4, y=6.2, z=7.0
t=3, x=-1.4, y=6.1, z=6.9
Thanks in advance for your help.
String datatest = data.toString();
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/test");
directory.mkdirs();
String filename = "test.txt";
File file = new File(directory, filename);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(fOut);
try {
osw.write(datatest + "\n");
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
try "\r\n"
and to make this answer longer :)

saving a file which contains dates results in keeping only last values

I am using the following code to store in a file some data.
(mydata is the data the user enters (double list) and dates_Strings is a string list where i store dates)
public void savefunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy",Locale.US);
Date d=new Date();
String formattedDate=thedate.format(d);
Log.d("tag","format"+formattedDate);
dates_Strings.add(formattedDate);
double thedata=Double.parseDouble(value.getText().toString().trim());
mydata.add(thedata);
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "MyFiles");
directory.mkdirs();
File file = new File(directory, filename);
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=0;i<mydata.size();i++){
bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
}
value.setText("");
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
The problem is that if I enter some data in 06/05/13 and later some data in 07/05/13 , the file contains only the last data from the last date.I want to keep all the data.
Open the fileoutputstream in append mode
fos = new FileOutputStream(file, true);
Use fos = new FileOutputStream(file, true); to append data to the file instead of overwriting it.
FileOutputStream documentation

append text to the end of the file

I am using the below code segment to write text to the end o the file for each time it is called. But, it is erasing the old data and then writes the new data to the beginning of the file. How can I fix the below code so that it is append new data always end of the file ?
public boolean writeToFile(String directory, String filename, String data ){
File out;
OutputStreamWriter outStreamWriter = null;
FileOutputStream outStream = null;
out = new File(new File(directory), filename);
if ( out.exists() == false ){
out.createNewFile();
}
outStream = new FileOutputStream(out) ;
outStreamWriter = new OutputStreamWriter(outStream);
outStreamWriter.append(data);
outStreamWriter.flush();
}
Try to set append boolean value to true in FileOutputStream:
outStream = new FileOutputStream(out, true);
outStreamWriter = new OutputStreamWriter(outStream);

Categories

Resources