I am using the first snippet to write a file.
String fileName = "Test6.txt";
String outputString="Text for File";
try {
FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(outputString.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
And the second to read it.
try{
FileInputStream InputStream = openFileInput("Text6.txt");
InputStreamReader inputStreamReader = new InputStreamReader(InputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineData = bufferedReader.readLine();
}catch(FileNotFoundException ex)
{
Log.d(TAG, ex.getMessage());
}
catch(IOException ex) {
Log.d(TAG, ex.getMessage());
}
But I can't read it, I get:
java.io.FileNotFoundException: /data/user/0/com.example.android.buildingmarque2/files/Text6.txt (No such file or directory)
I can also get a list of the files and Test6.txt is in the list.
Also, Android Studio Device File Explorer shows it.
It could be a problem with the path.
Device Explorer, "Copy Path" gives me
/data/data/com.example.android.buildingmarque2/files/Test6.txt
But the Log says:
/data/user/0/com.example.android.buildingmarque2/files/Text6.txt
I'm confused?
Typo. One is "Text6" the other is "Test6". Use a constant for both names to avoid this in the future
Related
developer data-storage-files
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();
}
private String readfile(){
String content="";
try{
InputStream inputStream=openFileInput(myfile);
if(inputStream!=null){
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String receiveString="";
StringBuilder stringBuilder=new StringBuilder();
while((receiveString=bufferedReader.readLine())!=null){
stringBuilder.append(receiveString);
}
inputStream.close() ;
content=stringBuilder.toString();
}
}
catch (FileNotFoundException e){
Log.e(FILENAME1, "File Not Found"+e.toString());
}catch (IOException e){
Log.e(FILENAME1,"Cannot read file"+e.toString());
}
return content;
}
I try to write a txt file according to this website, say i want to save the data like this way: ever day i save one number after the date
line 1 2014-12-23 3
line 2 2014-12-24 6
line 3 2014-12-25 10
.
.
.
.
But for every time I write data into this file, it seems that the file is overwritten and every time i read the file, this returns me the most update number. I want to save the date and get the data on one specific line. Any suggestions? Thanks a lot!!
Try changing this
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
to
outputStream = openFileOutput(filename, Context.MODE_APPEND);
Also you might have to check if files exists. If it doesn't exist use your original statement and if it does exist then use the statement I have mentioned.
I have an application that runs on glass. I want my application to create files at run time and be able to write/read data to/from those files. Can anyone show me a way to do this? Does Android's openFileOutput() work in glass?
(In case if anyone else has the same question)
Okay I figured out a way to do this. It looks like Java i/o libraries works fine with android and also for glass. The following works fine in glass.
String filename = "sensorData";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_APPEND);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter (outputStream);
outputStreamWriter.write(content);
outputStreamWriter.close();
}
catch (Exception e) {
e.printStackTrace();
}
//Printing the file in logcat just to verify the contents
FileInputStream inputStream;
try
{
inputStream = openFileInput(filename);
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
char[] buffer = new char[content.length()];
inputStreamReader.read(buffer);
String input = buffer.toString();
Log.i(input);
}
catch (Exception e)
{
e.printStackTrace();
}
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();
}
I copy+paste a *.txt file into the folder /frameworks/opt/telephony/src/java/android/telephony. Now I want to read it from SmsManager, but when I run the "emulator + adb logcat" it show me that it cant find the file.
Both files SmsManager.java and textfile.txt are in the same folder.
My code inside SmsManager is:
try {
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
// read every line of the file into the line-variable, on line at the time
while (( line = br.readLine()) != null) {
Log.i(TAG, line+"##########################");
}
in.close();
} catch (java.io.FileNotFoundException e) {
Log.i(TAG, "File didnt found");
} catch (java.io.IOException e) {
Log.i(TAG, "File didnt found");
}
What is wrong, or where must I save the file to find it?
You will have to modify device.mk like below to copy from your source code to Android file system
PRODUCT_COPY_FILES := frameworks/abc.txt:system/abc.txt
In your code you will have to access /system/abc.txt
Code first:
AssetManager mgr = DeviceListActivity.this.getApplicationContext().getAssets();
try {
Log.e("Glenn:", address);
FileOutputStream fout = mgr.openFd("device/device_address.txt").createOutputStream();
PrintWriter _fout = new PrintWriter(fout);
_fout.println(address);
Log.e("Glenn", address);
_fout.close();
fout.close();
InputStream fin = mgr.open("device/device_address.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
address = br.readLine();
try {
Log.e("Glenn:", address);
} catch (NullPointerException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
Log.e("Glenn", "error with OutputStream");
}
The value of address printed by the first two Log.e() calls is the right value, which actually is a device MAC address. However, when I was trying to test the value of address read from the file, which had just been written, NullPointerException has been caught within the Log.e() call. This means the value read from the file is NULL. Can anyone point out what's wrong with the code?
You cannot write in your app's asset file. You have only read but not write permissions. AssetManager only provides methods to read the files from your app's asset folder.