如何获取保存的文本文件的内容 (Java)
How to get the contents of a saved text file (Java)
有什么方法可以将文本文件的内容作为字符串获取吗?
例如我的文件内容为:
Name: John, Age: 34, Email: someone@example.com
我看到过类似的问题,但他们对我毫无帮助。
编辑:我的代码 -
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();
}
});
感谢您的帮助。
你可以试试这样想
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("C:\Users\Hasindu\Desktop\javaSample\src\sample\sample.txt");//Text file location
BufferedReader br = new BufferedReader(new FileReader(file));
String currentLine;
String content = null;
StringBuilder stringBuilder = new StringBuilder();//Using the StringBuilder class of java for the concatenation.
while ((currentLine = br.readLine()) != null) {
stringBuilder.append(currentLine); //Read all the lines in the text file and concatenate them for a single stringBulder object.
}
content = stringBuilder.toString();//Coverting the StringBuilder object to a String
System.out.println(content);
}
}
有什么方法可以将文本文件的内容作为字符串获取吗?
例如我的文件内容为:
Name: John, Age: 34, Email: someone@example.com
我看到过类似的问题,但他们对我毫无帮助。
编辑:我的代码 -
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();
}
});
感谢您的帮助。
你可以试试这样想
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("C:\Users\Hasindu\Desktop\javaSample\src\sample\sample.txt");//Text file location
BufferedReader br = new BufferedReader(new FileReader(file));
String currentLine;
String content = null;
StringBuilder stringBuilder = new StringBuilder();//Using the StringBuilder class of java for the concatenation.
while ((currentLine = br.readLine()) != null) {
stringBuilder.append(currentLine); //Read all the lines in the text file and concatenate them for a single stringBulder object.
}
content = stringBuilder.toString();//Coverting the StringBuilder object to a String
System.out.println(content);
}
}