마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

서비스에서 txt파일 읽기 오류

0 추천

제가 액티비티에서 블루투스 맥주소를 저장하고 서비스에서 맥주소를 읽어서 자동으로 연결되게 하려고 하고 있습니다.

    public String fileopen() {
        String dirPath = getFilesDir().getAbsolutePath();
        File file = new File(dirPath);

// 파일이 1개 이상이면 파일 이름 출력
        if (file.listFiles().length > 0) {
            for (File f : file.listFiles()) {
                String str = f.getName();
                Log.v(null, "connecting with : " + 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);
                    return content;
                } catch (Exception e) {
                    return null;
                }
            }
        }

        return null;
    }

함수는 이런데 파일을 읽는데 리턴으로 블루투스 주소를 넣었습니다.

이 함수를 태블릿으로 작업을 할때는 이상이 없었습니다.(4.4킷캣)

if (mChatService.getState() == BluetoothChatService.STATE_LISTEN && filecheck() == true) {
    android.util.Log.d(TAG, "파일 있음" + fileopen());
    connectDevice();
}

로그캣은 아래와 같은 방식으로 사용되게 됩니다.

 

다만 제폰(롤리팝)으로는 요런식으로 파일을 읽어오네요... 문제점을 잘 모르겠습니다...

이ㅡ글 (240 포인트) 님이 2016년 5월 6일 질문

1개의 답변

0 추천
폰의 VM과 PC의 인코딩이 달라 발생하는 이슈로 보입니다.

http://egloos.zum.com/psg9/v/1131327

http://forum.falinux.com/zbxe/index.php?document_srl=562562&mid=lecture_tip

아래코드를 확인 하셔서 파일의 인코딩을 맞춰 보세요.

아마 UTF-8, UTF-16, EUC-KR, ISO-8859-1 중 한개로 변경하시면 될 것으로 보입니다.

http://stackoverflow.com/questions/14918188/reading-text-file-with-utf-8-encoding-using-java
익명사용자 님이 2016년 5월 13일 답변
2016년 5월 13일 수정
...