Java 内部存储-设备内存
有人能帮我解释一下如何读取和显示存储在设备内存内部存储器中的数据吗Java 内部存储-设备内存,java,android,Java,Android,有人能帮我解释一下如何读取和显示存储在设备内存内部存储器中的数据吗 String input=(inputBox.getText().toString()); String FILENAME = "hello_file"; //this is my file name FileOutputStream fos; try { fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(input.getBytes(
String input=(inputBox.getText().toString());
String FILENAME = "hello_file"; //this is my file name
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(input.getBytes()); //input is got from on click button
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos1= openFileInput (FILENAME);
} catch (FileNotFoundException e) {}
outputView.setText(fos1./*I don't know what goes here*/);
openFileInput
返回FileInputStream
对象。然后,您必须使用它提供的read
方法从中读取数据
// missing part...
int len = 0, ch;
StringBuffer string = new StringBuffer();
// read the file char by char
while( (ch = fin.read()) != -1)
string.append((char)ch);
fos1.close();
outputView.setText(string);
请查看以供进一步参考。请记住,这将适用于文本文件。。。如果它是一个二进制文件,它会将奇怪的数据转储到你的小部件中。有很多方法可以读取文本,但使用scanner对象是我最简单的方法之一
String input=(inputBox.getText().toString());
String FILENAME = "hello_file"; //this is my file name
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(input.getBytes()); //input is got from on click button
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String result = "";
try {
fos1= openFileInput (FILENAME);
Scanner sc = new Scanner(fos1);
while(sc.hasNextLine()) {
result += sc.nextLine();
}
} catch (FileNotFoundException e) {}
outputView.setText(result);
您需要导入java.util.Scanner代码>以使其工作。如果要从文件中获取更多特定信息,Scanner对象还有其他方法,如nextInt()