Is there any way to get the contents of a text file as a string?
For example, I have the contents of the file as:
Name: John, Age: 34, Email: someone@example.com
I have seen similar questions, but they don't get me anywhere.
Edit: My code -
String fileName = "Sample" + ".txt";
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS) + File.separator + fileName);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String currentLine;
String content;
StringBuilder stringBuilder = new StringBuilder();
while ((currentLine = br.readLine()) != null) {
stringBuilder.append(currentLine);
}
content = stringBuilder.toString();
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
// somewhere else in the code
final Button viewData = findViewById(R.id.viewData); // Defining the viewData button
viewData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewFileData();
}
});
Thanks for your Help.