2010-06-25 3 views
2

장치 메모리의 내부 저장소 개인 데이터에 저장된 데이터를 읽고 표시하는 방법을 설명하는 데 도움이 될 수 있습니까?내부 저장소 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()); //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*/); 

답변

3

openFileInputFileInputStream 개체를 반환합니다. 그런 다음 제공하는 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); 

자세한 내용은 FileInputStream을 참조하십시오. 텍스트 파일의 경우에는 정상적으로 작동합니다 ... 바이너리 파일 인 경우 위젯에 이상한 데이터가 저장됩니다.

3

텍스트를 읽는 방법은 많이 있지만 스캐너 개체를 사용하는 것이 가장 쉬운 방법 중 하나입니다.

이 작업을 수행하려면 import java.util.Scanner;이 필요합니다. 파일에서 더 자세한 정보를 얻으려면 Scanner 객체에 nextInt()과 같은 다른 메서드가 있습니다.