외부 저장소에 파일을 쓰고 읽는 간단한 프로그램을 작성하려고합니다. 다음은 문제에 대한 설명입니다.
1. 텍스트 "hello world"가 포함 된 파일 이름 = "hello_files.txt"는 /sdcard
에 저장됩니다.
2. 파일에 대한 데이터가 작성되면 파일은 adb
으로 볼 수 있습니다.
3. 그러나 응용 프로그램을 휴대 전화에로드하고 타사 파일 탐색기 응용 프로그램을 사용하여 보았 으면 hello_files.txt 파일에 텍스트가 기록되지 않은 것으로 보입니다. 내 프로그램의 일부를 쓰지 만, 나는 무엇을 알아낼 수 없다. 나는 코드의 두 부분 아래에 글쓰기와 읽기 모두를 주었다.외부 저장소에 파일을 쓰거나 읽을 수 없습니다. - Android
파일에서 읽기위한 코드 :
public void readFromFile() {
//Step 1. Check if the storage is available.
boolean mStorageAvailable;
boolean mStorageReadWrite;
String state = Environment.MEDIA_MOUNTED;
Log.v(this.toString(), "State of media = " + state);
if(Environment.MEDIA_MOUNTED.equals(state)) {
Log.v(this.toString(), "Media is mounted.");
mStorageAvailable = true;
mStorageReadWrite = true;
File dir = Environment.getExternalStorageDirectory();
String fileName = dir + "/" + file;
Log.v(this.toString(), "Reading from file = " + fileName);
File f = new File(dir, file);
FileReader fr = null;
try {
fr = new FileReader(f);
Log.v(this.toString(), "Wrapping a buffered reader around file reader.");
BufferedReader bufRead = new BufferedReader(fr, 8192);
//Log.v(this.toString(), bufRead.toString());
String line;
try {
line = bufRead.readLine();
Log.v(this.toString(), "Line read = " + line);
Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v(this.toString(), "IOException found in reading line from file.");
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Log.v(this.toString(), "File not found for reading.");
}
}
}
파일로 작성하기위한 코드는 다음과 같습니다
public void writeToFile() {
//Step 2. Check if external storage is mounted.
boolean mStorageExists;
boolean mStorageReadWrite;
String state = Environment.getExternalStorageState();
Log.v(this.toString(), "External media mounted state = " + state);
if(Environment.MEDIA_MOUNTED.equals(state)) {
//media is mounted.
//Mounted storage is read/write.
//Write to the file.
mStorageExists = true;
mStorageReadWrite = true;
Log.v(this.toString(), "Going to write to a file.");
//get directory.
File dir = Environment.getExternalStorageDirectory();
Log.v(this.toString(), "Root directory = " + dir.getAbsolutePath());
String fileName = dir.getAbsolutePath() + "/" + file;
Log.v(this.toString(), "File to write to is present at " + fileName);
File f = new File(dir, file);
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Log.v(this.toString(), "File not found.");
}
try {
fos = openFileOutput(file, MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
Log.v(this.toString(), "Bytes of the string = " + str.getBytes().toString());
fos.write(str.getBytes());
//Log.v(this.toString(), "Local path = ");
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.v(this.toString(), "File not found!!");
e.printStackTrace();
} catch (IOException e) {
Log.v(this.toString(), "IO Exception.");
e.printStackTrace();
}
} else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
//media is mounted but read only.
mStorageExists = true;
mStorageReadWrite = false;
Log.v(this.toString(), "The media is read-only mounted.");
} else if((Environment.MEDIA_NOFS.equals(state)) || (Environment.MEDIA_REMOVED.equals(state)) || (Environment.MEDIA_UNMOUNTED.equals(state))) {
mStorageExists = false;
mStorageReadWrite = false;
Log.v(this.toString(), "There is some problem with the media.");
}
}
합니다 (Log
자세한 태그에서) 로그 :
10-23 10:26:19.444: V/[email protected](466): Read from file button clicked.
10-23 10:26:19.444: V/[email protected](466): State of media = mounted
10-23 10:26:19.444: V/[email protected](466): Media is mounted.
10-23 10:26:19.444: V/[email protected](466): Reading from file = /sdcard/hello_files.txt
10-23 10:26:19.453: V/[email protected](466): Wrapping a buffered reader around file reader.
10-23 10:26:19.453: V/[email protected](466): Line read = null
로그 –
게시 게시하시기 바랍니다. hello_files.txt 파일에는 한 줄만 있습니다. hello world – Sriram
@SardorDushamov : 어떤 도움이라도 환영합니다 ... – Sriram