Android/File
단말기 내부에 폴더 및 txt파일 생성하기
berabue
2011. 3. 2. 13:24
String dirPath = getFilesDir().getAbsolutePath(); File file = new File(dirPath); // 일치하는 폴더가 없으면 생성 if( !file.exists() ) { file.mkdirs(); Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show(); } // txt 파일 생성 String testStr = "ABCDEFGHIJK..."; File savefile = new File(dirPath+"/test.txt"); try{ FileOutputStream fos = new FileOutputStream(savefile); fos.write(testStr.getBytes()); fos.close(); Toast.makeText(this, "Save Success", Toast.LENGTH_SHORT).show(); } catch(IOException e){} // 파일이 1개 이상이면 파일 이름 출력 if ( file.listFiles().length > 0 ) for ( File f : file.listFiles() ) { String str = f.getName(); Log.v(null,"fileName : "+str); // 파일 내용 읽어오기 String loadPath = dirPath+"/"+str; try { FileInputStream fis = new FileInputStream(loadPath); BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis)); String content="", temp=""; while( (temp = bufferReader.readLine()) != null ) { content += temp; } Log.v(null,""+content); } catch (Exception e) {} }
LogCat 결과