안드로이드 개발 질문/답변
(글 수 45,052)
sd card를 마운트 시키고
읽는 부분은 성공했습니다^^;
근데 쓰는 부분이 안되더군요...
자꾸 file not found 에러가 뜨네요.
파일 이름 이나 경로 문제일까요?? 조언부탁드립니다.
코드는 다음과 같습니다.
public void saveFile()
{
try{
FileOutputStream fos = new FileOutputStream("/sdcard/note.txt");
fos.write(editor.getText().toString().getBytes());
fos.close();
}
catch(java.io.FileNotFoundException e){
Toast.makeText(this, "FileNotFound", 2000).show();
}
catch(Throwable t)
{
Toast.makeText(this, "error : " + t.toString(), 2000).show();
}
}
읽는 부분은 성공했습니다^^;
근데 쓰는 부분이 안되더군요...
자꾸 file not found 에러가 뜨네요.
파일 이름 이나 경로 문제일까요?? 조언부탁드립니다.
코드는 다음과 같습니다.
public void saveFile()
{
try{
FileOutputStream fos = new FileOutputStream("/sdcard/note.txt");
fos.write(editor.getText().toString().getBytes());
fos.close();
}
catch(java.io.FileNotFoundException e){
Toast.makeText(this, "FileNotFound", 2000).show();
}
catch(Throwable t)
{
Toast.makeText(this, "error : " + t.toString(), 2000).show();
}
}
2010.10.19 11:56:13
1. 외부 저장 공간(sdcard)에 파일을 쓰고자 할때
안드로이드 1.6버전부터 권한 설정이 필요
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
2. 버퍼를 이용 권장
File file = new File(Environment.getExternalStorageDirectory(),"note.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fw);
String writeStr = et.getText().toString();
out.write(writeStr);
out.flush();
out.close();
3. 파일이 없을시 지정된 영역에 지정된 파일명으로 파일 생성 후 쓰기 실행