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
Related
I am trying to add data to a text file in android using the code below but it only overwrites the data with one line of data.
private void copyImageToMemory(File outFile , Float number) {
try {
BufferedOutputStream fos = new BufferedOutputStream(
new FileOutputStream(outFile));
PrintWriter pw = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(fos)));
pw.append("result"+number);
pw.close();
maxSpeed=0;
} catch (FileNotFoundException e) {
Log.e(TAGFile, "FileNotFoundException");
}
}
The FileOutputStream constructor allows to specify whether it should append to an already existing file or not:
new FileOutputStream(file, true);
will create a stream that appends to the given file.
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
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
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);
I made this code to write on a sdcard, now how can I transform it to write into the internal memory ?
//private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/liste.txt"; Path used to write on sdcard
try{
File file = new File(path);
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write("something");
bw.flush();
bw.close();
catch(Exception e) {...}
In android each application has its own folder under /data/data/package/
this dir is only accessable by your app and the root if the device is rooted
to access this dir and read/write to it, you can use:
getApplicationContext().openFileOutput(name, mode);
and
getApplicationContext().openFileInput(name);
more about this here : Docs
USe this code:
FileOutputStream fos;
fos = context.openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(data.getBytes()); //write to application top level directory for immediate sending
fos.close();
try{
File file = new File("/data/data/com.example.packagename/files/");
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write("something");
bw.flush();
bw.close();
catch(Exception e) {...}
or
//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext();
filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath);