Store Multiple values in a file in table format - android

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

Related

File isn't getting created in the phone's external storage

I have used this code for creating a file in my Phone's external storage. Please note that I've set the permissions for Read and Write in my Manifest file.
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Date now = new Date();
String fileName = formatter.format(now) + ".txt"; //like 20170602.txt
File directory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(directory, fileName);
String bodyOfFile = "Body of file!";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(bodyOfFile.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
My LogCat is showing the below. I can't see the file 20170602.txt in that particular location. In my Download folder, there isn't any file with that name. Can anyone tell me where I went wrong.
D/tag: Directory: /storage/emulated/0/Android/data/com.pc.tab/files/Download
D/tag: File: /storage/emulated/0/Android/data/com.pc.tab/files/Download/20170605.txt
UPDATE:
I'm using MOTO G4 for running this app. I found the 20170602.txt file on Internal Storage.
File Manager --> `LOCAL` tab ( Upper right ) --> Internal Storage --> Android --> data --> com.pc.tab --> files --> Download --> 20170602.txt
It is important to separate the directory and the file itself
File directory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(directory, fileName);
In your code you call mkdirs on the file you are want to write on, its an error because mkdirs makes your file be an a directory. You should call mkdirs for the directory only so its will be created if its not exist, and the file will be created automatically when you create new FileOutputStream object for this file.
Your code should look like this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Date now = new Date();
String fileName = formatter.format(now) + ".txt"; //like 20170602.txt
File directory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(directory, fileName);
String bodyOfFile = "Body of file!";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(bodyOfFile.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}

Appending data to a text file in Android

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.

Getting lastModified date for item in Asset folder?

My goal is very simple: I have a zip file in my assets folder and wish to get the last modified date of it.
I am able to access the file via the Assetmanager and create a file from it, but the modified date is the moment that file is written
AssetManager manager;
File checkFile = new File(source + "/" + "test.zip");
try
{
InputStream stream = manager.open(fileToCheck);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
FileOutputStream fos = new FileOutputStream(checkFile);
fos.write(buffer);
fos.close();
}
catch(IOException ioExc)
{
//stuff
}
long lastMod = checkFile.lastModified();
Date lastMod = new Date(lastMod ); //This is returning the current time,
//NOT the modified date of the file
Can anyone think of a way I could access the actual last modified date of the file in the assets folder? Any help would be greatly appreciated!
The easiest way is you can include the last modified date into that zip file (e.g, lastmod.txt). You can then unzip and read that file.

Android : CSV not storing values in normal format

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

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

Categories

Resources