'안드로이드 폴더 생성'에 해당되는 글 1건

  1. 2011.12.16 [Android] - 안드로이드 내부 폴더 생성, txt 파일 생성 예제

[Android] - 안드로이드 내부 폴더 생성, txt 파일 생성 예제

Android Java 2011. 12. 16. 15:47
반응형
01.String dirPath = getFilesDir().getAbsolutePath();
02.File file = new File(dirPath);
03. 
04.// 일치하는 폴더가 없으면 생성
05.if( !file.exists() ) {
06.file.mkdirs();
07.Toast.makeText(this"Success", Toast.LENGTH_SHORT).show();
08.}
09. 
10.// txt 파일 생성
11.String testStr = "ABCDEFGHIJK...";
12.File savefile = new File(dirPath+"/test.txt");
13.try{
14.FileOutputStream fos = new FileOutputStream(savefile);
15.fos.write(testStr.getBytes());
16.fos.close();
17.Toast.makeText(this"Save Success", Toast.LENGTH_SHORT).show();
18.catch(IOException e){}
19. 
20.// 파일이 1개 이상이면 파일 이름 출력
21.if ( file.listFiles().length > 0 )
22.for ( File f : file.listFiles() ) {
23.String str = f.getName();
24.Log.v(null,"fileName : "+str);
25. 
26.// 파일 내용 읽어오기
27.String loadPath = dirPath+"/"+str;
28.try {
29.FileInputStream fis = new FileInputStream(loadPath);
30.BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis));
31. 
32.String content="", temp="";
33.while( (temp = bufferReader.readLine()) != null ) {
34.content += temp;
35.}
36.Log.v(null,""+content);
37.catch (Exception e) {}
38.}


LogCat 결과
반응형
: