게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
Android] String 을 파일 출력할 때 질문입니다!!
게시물ID : programmer_13270짧은주소 복사하기
작성자 : 뿌뿌빠뿌뿌
추천 : 0
조회수 : 321회
댓글수 : 3개
등록시간 : 2015/09/14 12:42:59
옵션
  • 본인삭제금지
1차시도
try {
String path;
BufferedWriter writer = new BufferedWriter( new FileWriter(".\\korea_food.txt"));
writer.write(results);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

이렇게 했더니 FileSystem이 Read Only 라며 거절당했습니다 ㅠㅠ 해결 방법을 도무지 찾지 못해서 다음 방법을 사용해보았습니다.

2차시도

File myfile = getDir("myFile", Activity.MODE_PRIVATE);
String path = myfile.getAbsolutePath();
//= myfile.getAbsolutePath();
System.out.println("path  : " + path);
FileOutputStream fos = null;
File file = new File(path+"/food_korea.txt");
try {
fos = new FileOutputStream(file, true);
System.out.println("fos 객체 생성 실패");
} catch (FileNotFoundException e){
e.printStackTrace();
}
try {
               fos.write(results.getBytes());       //byte[] java.lang.String.getBytes()
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               System.out.println("write실패");
           }
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
               System.out.println("close실패");
}

객체 생성부터 안됩니다. ㅠ^ㅠ Logcat에 fos 객체 생성 실패가 찍히더라구요...


대망의 3차 시도!!

OutputStreamWriter outputStreamWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(openFileOutput("Food_Korea.txt", Context.MODE_PRIVATE));

System.out.println("객체 생성 성공");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("OutputStreamWriter 객체 생성 실패");
}
           try {
outputStreamWriter.write(results);
System.out.println("write성공");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("write실패");
}
           try {
outputStreamWriter.close();
System.out.println("close성공");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("close실패");
}

LogCat 확인 결과 객체생성성공/write성공/close성공이 찍혔습니다. 
뭔가 드디어 성공한 기분?? 하지만 파일 위치를 찾을 수가 없습니다.

이 파일을 뽑아서 다른 분께 전달해드려야 하는데... 못찾겠어요..죽..여...줘....

절대경로 썼더니 빨간 줄 뜨면서 \b \n 머 이러면서 오류나서 그냥 저렇게 했는데 쀼...

어디로 가야 제가 만든 파일을 찾을 수 있을까요? (눈물을 훔치며)
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호