[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 결과
반응형
'Android Java' 카테고리의 다른 글
[Java] - Java 접근제어순서와 정의 (0) | 2012.08.13 |
---|---|
[Android] - 화면 켜짐 소스 (0) | 2012.05.07 |
[Android Java] - 상단 Title bar 제거 (파란색 바) (0) | 2012.02.07 |
[Android] - 안드로이드 페이지전환 로딩창 띄우는 소스 예제 (0) | 2011.12.26 |
[Android] - Splash 로딩 이미지 띄우는 소스 예제 (0) | 2011.12.26 |