I am not having luck with this method, which is basically a hello world for printing file contents to the Android cat log.
try {
InputStream instream = openFileInput("inputFile.txt");
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
while (( line = buffreader.readLine()) != null) {
System.out.println(line);
}
instream.close();
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
}
Using a default generated project with the Android Eclipse plugin, under what directory should this file exist? Any other considerations?
This will try to read the file inputFile.txt from your internal application data directory /data/data/your.application.package/ (and will fail if it doesn't exist):
openFileInput("inputFile.txt");
To read a file from SD card you would do something like this:
new FileInputStream(Environement.getExternalStorageDirectory()
.getAbsolutePath() + "/inputFile.txt")
Don't forget to set the SD card permission in your manifest then:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This file should exist in the apps local 'data' directory. Does this file already exist? Have you taken a look at the documentation for file IO on Android?
Related
i am trying to read file from internal storage. the file is available in internal storage at given location and the code is able to access it.. but when it tries to open the file , it throws FileNotFound Exception which is due to the app can't open the file to read. I came to know about the app can't open the file by using file.canRead() method which is returning false. can anybody help me to figure the situation out? Here is my code
path = MainActivity.this.getFilesDir().getAbsolutePath();
try {
File file = new File(path, "jamshaid.txt");
Context ctx = getApplicationContext();
FileInputStream fileInputStream = ctx.openFileInput(file.getName());
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineData = bufferedReader.readLine();
view1.setText(lineData);
} catch (Exception e) {
e.printStackTrace();
}
Permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
API Version of my device is 24
What i wanna do?
i am trying to parsethe file/set text file data to textView
Update1
after changing
File directory = MainActivity.this.getFilesDir();
File file = new File(directory, "jamshaid.txt");
i am getting the following error
`09-25 14:45:16.413 21144-21144/com.example.root.testingvolley W/System.err: java.io.FileNotFoundException: /data/user/0/com.example.root.testingvolley/files/jamshaid.txt (No such file or directory)`
Try this for directory path
File directory = context.getFilesDir();
File file = new File(directory, filename);
Or
public String getTextFileData(String fileName) {
StringBuilder text = new StringBuilder();
try {
FileInputStream fIS = getApplicationContext().openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fIS, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
text.append(line + '\n');
}
br.close();
} catch (IOException e) {
Log.e("Error!", "Error occured while reading text file from Internal Storage!");
}
return text.toString();
}
How are you verifying that the file exists? Perhaps the file does not exist, in which case openFileInput() will throw FileNotFoundException.
You can try opening the file in write-mode which will create a file if no such file exists.
FileOutputStream fileOutputStream = ctx.openFileOutput(file.getName(), ctx.MODE_PRIVATE);
I am trying to open a file to use for my android application, but I am not sure where to save this file so I can use it?
Also how do I use this file once it is saved in the correct place?
It is a .txt file.
How to do this?
To add a file to your application and then read from it at runtime, put it into your res/raw directory and then you can read it like this (where my_file is the name of your file):
try {
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.my_file);
BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));
String line = reader.readLine();
while (line != null) { ... }
} catch (Exception e) {
e.printStackTrace();
}
i have been experiencing problem reading data from text files which resides in my internal storage of my mobile phone. My code is as below:
public void recommend(View view) {
Context context = getApplicationContext();
String filename = "SendLog.txt";
TextView tv = (TextView)findViewById(R.id.textView134);
try {
FileInputStream fis = context.openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
tv.getText();
tv.setText(line);
}
} catch (IOException e) {
e.getStackTrace();
}
I'm not getting any data input to my mobile phone. My "SendLog.txt" file reside in /storage/emulated/0/SendLog.txt directory. Anyone can assist me? I want to read everything from the text file and display in textView134. Anyone can assist?
Thank you in advance!
"/storage/emulated/0/SendLog.txt" is located on the External Storage, not the internal.
Instead of context.openFileInput(filename) you should be using Environment.getExtenalStorageDirectory() for example,
FileInputStream fis = new FileInputStream(new File(Environment.getExtenalStorageDirectory(), filename));
Though you will need to handle possibilities such as the External Storage being unavailable / unmounted.
You will also need the manifest permission to read external storage, or alternately to write it if you plan to do that as well (you only need one of the two, as write implies read).
You are building a string using stringbuilder but instead are settexting line. The result is last line in file, which may be an empty line
I have an application that make me choose a file through a file explorer(the file is stored on the sd), and then reads it.
I want to modify it, so it has the file directly into the app and reads the file from "inside". Where I have to put the file into the project? How can I access it?
You can save a file inside your project by using the following code:
File cDir = getApplication().getExternalFilesDir(null);
File saveFilePath = new File(cDir.getPath() + "/" + "yourfilename");
You can see the saved file inside "files" folder of your application package name in your device.
Try the following path in your device:
File manager >> Android >> data >> "your package name" >> files >> new file.
Yes, you can put your file into /assets folder, and retrieve as follows:
AssetManager assetManager = getAssets();
InputStream instream = assetManager.open("file.txt");
or res/raw folder:
InputStream raw = getResources().openRawResource(R.raw.file);
If you want to modify it, you'll be able only to write a file into External storage (e.g. sdcard),
or into Internal storage (under your application folder data/data/package_name/).
If you store your file into External storage it will persist until user manually or programmatically deletes the file. But if you store this file into Internal storage, it will be deleted if user deletes an app, or clear an application cache.
Demo
File myExternalFile;
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveToExternalStorage.setEnabled(false);
} else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
save External Storage (FileOutputStream )
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(myInputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText("");
responseText.setText("Saved to External Storage.(StorageFile.txt)");
Get External Storage (FileInputStream )
try {
FileInputStream fis = new FileInputStream(myExternalFile);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText(myData);
responseText
.setText("Data retrieved from Internal Storage.(StorageFile.txt)");
I am new to android and I want to read a binary file extension .AMF file.
I really need your help this is really urgent.
Thanks in advance.
I made a folder in RES named raw. Here is the code I tried, it says file not found.
FileInputStream in = new FileInputStream("R.raw.hello.txt");
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
inLine.append("\n");
}
in.close();
return inLine.toString();
From the Andoid SDK:
To read a file from internal storage:
Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
Read bytes from the file with read().
Then close the stream with close().
Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
InputStream input = getResources().openRawResource(R.raw.hello.txt);