기술/beginning games2011.05.25 10:09
download download
외부 저장 장치를 사용하기 위하여 uses-permission을 추가
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

public class ExternalStorageTest extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   TextView textView = new TextView(this); 
   setContentView(textView); 
   
   String state = Environment.getExternalStorageState();
   if(!state.equals(Environment.MEDIA_MOUNTED)){
    textView.setText("외부 저장 장치가 없습니다.");
   } else {
    File externalDir = Environment.getExternalStorageDirectory();
    File textFile = new File(externalDir.getAbsolutePath()+File.separator+"text.txt");
    try{
    writeTextFile(textFile,"테스트 입니다.");
    String text = readTextFile(textFile);
    textView.setText(text); 
    if(!textFile.delete()){
    textView.setText("임시파일을 삭제 할 수 없습니다.");
    }
    } catch(IOException e){
    textView.setText("에러:"+e.getMessage());
    }
   }
   
}
private void writeTextFile(File file, String text)throws IOException{
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
}
private String readTextFile(File file)throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder text = new StringBuilder();
String line;
while((line=reader.readLine())!= null){
text.append(line);
text.append("\n");
}
reader.close();
return text.toString();
}

}
저작자 표시
신고
download download download
Posted by [lying dragon] Ed Kim

티스토리 툴바