Reputation: 306
my app requires that texts be saved(user choice) into the internal memory and then i want something like a favorite feature where user clicks on a button and a favorite activity starts to load the files saved in the memory(internal). in my program there are multiple texts and i have used a random generator to save the files as "fav1" "fav2" etc.. where the integer part is generated randomly. the problem is that now i don't know how to give my file name so that the files are retrieved and shown in a text View.
public void load(String name){
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput(filename)));
String inputString;
StringBuffer stringBuffer = new StringBuffer();
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString + "\n");
}
show.setText(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
so how do you suggest i retrieve the files, its getting frustrating anyone help.
Upvotes: 1
Views: 3123
Reputation: 2177
you can do it easily by the following code; as you said it reads from file from internal storage.
private String readFromFile(String fname) {
String ret = "";
try {
InputStream inputStream = openFileInput(fname+".txt");
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);
}
show.setText(stringBuilder.toString());
inputStream.close();
}
}
EDIT to read from the path contains path seperators
File myFile = new File(fname+".txt"); // path contains full path to the file including file name
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = myReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
show.setText(stringBuilder.toString());
myReader.close();
EDIT 2
to list files from specific directory and to choose the required file by its name.
File directory = new File(Environment.getExternalStorageDirectory()+"/sample");
// check the existance of the parent directory
if (directory.exists()) {
// get the list of files from the directory and keep it in an array of type File.
File[] fileList = directory.listFiles();
for (File file : fileList) {
//compares with filename: you can change this to your required file!
if (file.getName().equals("sam2.txt")) {
// method to read and show the text in text view
loadFile(file);
}
}
finally the definition of loadFile() method:
private void loadFile(File file) {
// TODO Auto-generated method stub
FileInputStream fIn;
try {
fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = myReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
show.setText(stringBuilder.toString());
myReader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Upvotes: 3