Android : How to write into internal memory - android

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

Related

Write data continuously to file in external storage - Android App

I am writing the data to the file in an external storage (SD card) on my android. The issue that I am facing is that it just makes one entry and does not go beyond that. I have looked up a number of Q/As here. Could someone please point me in the write direction? TIA!
FileOutputStream outputstream;
try {
file1 = new File(Environment.getExternalStorageDirectory(), "MyData.txt");
outputstream = new FileOutputStream(file1);
OutputStreamWriter oswriter = new OutputStreamWriter(outputstream);
BufferedWriter bwriter = new BufferedWriter (oswriter);
bwriter.append(entry);
bwriter.newLine();
bwriter.close();
outputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
You have to tell FileOutputStream to append the data. By default it just overwrites all contents there. For this you only need to use a different constructor FileOutputStream(File, boolean):
outputstream = new FileOutputStream(file1, true);

Internal Storage and Files?

I'm trying to create a file in the internal storage
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
But when i search in /data/data/ i don't find the app dir to see the file presence...why? I try with a context.getApplicationInfo().dataDir() but it don't work it give error. How can i do? What is/are my mistake(s)?
Try something like this,
File path = getFilesDir();
File file = new File(path, filename);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(string.getBytes());
outputStream.close();
you should probably do a google search before posting.
The file will be located in
data->data->package name->files->filename
File Explorers have no access to the /data directory.
With getFilesDir() you will know the directory where you put your file.
outputStream = context.getFilesDir().openFileOutput(filename, Context.MODE_PRIVATE);
getFilesDir() returns the Absolute path to the file .
Read here

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

Folder created instead of a file

I want to create a file in a defined directory, i tried this two codes but the first just creates folders and the other output an exception: no such file or directory:
First code:
File file = new File(Environment.getExternalStorageDirectory()
+File.separator
+"carbu"
+File.separator
+"install");
file.mkdir();
Then i added this code hopefully to create the file:
File file2 = new File("/carbu/install/","voitu");
file2.createNewFile();
Can anyone please try to help me ?
Thank you very much :).
Try this in your Activity:
FileOutputStream fos = openFileOutput(YOUR_FILE_NAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(5);
oos.flush();
This will create file if it isn't exist. Of course you should close oos and fos.
Here is the most simple solution, working at 100% ;)
File dir = new File (sdCard.getAbsolutePath() + "/jetpack/install");
dir.mkdirs();
File file = new File(dir, "wipe");
have you tried:
File f=new File("myfile.txt");
if(!f.exists())
{
f.createNewFile();
}
In you example you are only giving the pathname to the file but you are not defining the type and the name of the new file.
http://download.oracle.com/javase/6/docs/api/java/io/File.html
Also try following:
String FILENAME = "/carbu/install/test.txt";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close()
got the above example from: http://developer.android.com/guide/topics/data/data-storage.html
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File gpxfile = new File(root, "gpxfile.gpx");
FileWriter gpxwriter = new FileWriter(gpxfile);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write("Hello world");
out.close();
}
} catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
}
While you were careful in constructing the path correctly in the first segment, you just hard-coded the wrong path in the second part. Ensure you use the correct path, possibly as follows:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+File.separator
+"carbu"
+File.separator
+"install";
File file = new File(path);
file.mkdir();
File file2 = new File(path + File.separator + "voitu");
file2.createNewFile();

Android Emulator writes file but cannot find directory

I am trying to get a simple Hello World txt file to be written then read by my android application. When viewing the DDMS File Explorer it successfully creates the text file but i then get a FileNotFoundException when trying to read it.
try {
final String TESTSTRING = new String("Hello World");
FileOutputStream fOut = openFileOutput("test.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(TESTSTRING);
osw.flush();
osw.close();
FileInputStream fIn = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[TESTSTRING.length()];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
boolean isTheSame = TESTSTRING.equals(readString);
Log.i("File Reading Stuff", "success = " + isTheSame);
} catch (IOException e) {
e.printStackTrace();
}
also the error is java.io FileNotFoundException: /test.txt (No such file or directory)
Any Help Thanks.
I don't know where openFileOutput saves its files, but wouldn't you use it's input equivalent openFileInput to read such a file?
See my previous post with information about how to read/write to the external storage directory in Android:
Android how to use Environment.getExternalStorageDirectory()
-- Dan

Categories

Resources