2014-07-24 2 views
0

Android 프로젝트의 루트 폴더에 KARIN.txt라는 txt 파일이 있습니다. 내가 FileReader하여 도달하려고하지만 다음과 같은 오류 데 :텍스트 파일의 경우 java.io.FileNotFoundException

BufferedReader br = new BufferedReader(new FileReader("KARIN.txt")); 

내가 그렇지 않은 이유 혼란 스러워요 :

java.io.FileNotFoundException: /KARIN.txt: open failed: ENOENT (No such file or directory) 

내가 그 파일에 도달하려고하는 방법은 다음과 같습니다 내가 파일을 읽을 수있게 해줬 어.

+0

아마도 정확한 위치에 파일이 없기 때문에 이미 확인 했습니까? – 1337

+1

당신은'/'에 쓸 수 없습니다. root 만'/'에 쓸 수 있습니다. – Blackbelt

+0

절대 경로를 지정하십시오. – AlexS

답변

2

KARIN.txt가 프로젝트에 빌드 된 경우 자산 폴더에 넣고 AssetManager를 통해 액세스해야합니다.

public String loadFile(String file){ 
     AssetManager am = getActivity().getAssets(); 
     InputStreamReader ims = null; 
     BufferedReader reader = null; 
     String data = "File not available!"; 
     try { 
      ims = new InputStreamReader(am.open(file), "UTF-8"); 
      reader = new BufferedReader(ims); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if(ims != null){ 
      try { 
       String mLine = reader.readLine(); 
       data = ""; 
       while(mLine != null){ 
        data+= mLine; 
        mLine = reader.readLine(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally{ 
       if (reader != null) { 
        try { 
         reader.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 
     return data; 
} 
+0

Eclipse는 getActivity를 빨간색으로 강조 표시합니다. – birdcage

+0

아, 죄송합니다. 이것은 다른 코드에서 가져온 코드입니다. 이 경우 getActivity()를 무시하고 그냥 getAssets()를 사용하십시오. – TheJavaCoder16

+0

감사합니다. – birdcage

관련 문제