gpt4 book ai didi

android - 如何使用输入流读取器从android内部存储器加载文件

转载 作者:行者123 更新时间:2023-11-29 20:56:23 25 4
gpt4 key购买 nike

我的应用程序要求将文本保存(用户选择)到内部存储器中,然后我想要一些类似于最喜欢的功能,用户点击一个按钮,最喜欢的 Activity 开始加载保存在内存中的文件(内部)。在我的程序中有多个文本,我使用随机生成器将文件保存为“fav1”“fav2”等。其中整数部分是随机生成的。问题是现在我不知道如何提供我的文件名,以便检索文件并将其显示在 TextView 中。

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();

}

所以你建议我如何检索文件,任何人的帮助都会让我感到沮丧。

最佳答案

您可以通过以下代码轻松完成;正如您所说,它从内部存储的文件中读取。

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();

}
}

编辑从路径中读取包含路径分隔符

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();

编辑 2

列出特定目录中的文件并按名称选择所需的文件。

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);



}

}

最后是loadFile()方法的定义:

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();
}

}

关于android - 如何使用输入流读取器从android内部存储器加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27578022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com