2011-10-28 6 views
2

이 코드를 사용하고 있습니다.안드로이드에서 한 줄씩 읽는 방법?

try{ 
      // Open the file that is the first 
      // command line parameter 
      FileInputStream fstream = new FileInputStream("config.txt"); 
      // Get the object of DataInputStream 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      while ((br.readLine()) != null) { 
       temp1 = br.readLine(); 
       temp2 = br.readLine(); 

      } 

      in.close(); 
    }catch (Exception e){//Catch exception if any 
    Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show(); 
    } 
    Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show(); 

그러나 이것은 예외를 나타내며 temp1 및 temp2를 업데이트하지 않습니다.

+1

당신이 2 개 개의 다른 변수 같은 줄을 저장 하시겠습니까? 아니면 그냥 temp1과 temp2의 마지막 두 줄을 저장하고 싶습니까? – SERPRO

+0

두 개의 다른 변수에 파일의 처음 두 줄을 저장하고 싶습니다. – xkrlaix

답변

7

- 내가 강력하게 추천 할 것이라고는) 예를 들어, 특정 유형으로 잡기 IOException, b) 메시지 또는 스택 추적으로 로그 또는 표시 c) 적어도 Eclipse에서 프로그래밍하는 경우 DDC 관점에서 LogCat에서 확인 - Android가 config.txt 파일을 찾지 않아 발생합니다. 열려고합니다. 일반적으로 귀하의 경우와 같이 가장 간단한 경우 응용 프로그램에 전용 인 파일은 openFileInput - see the documentation을 사용하여 열립니다.

예외적으로 읽기 루프에 결함이 있습니다. 입력하기 전에 빈 문자열을 초기화하고 while 상태로 채워야합니다.

String line = ""; 
while ((line = br.readLine()) != null) { 
    // do something with the line you just read, e.g. 
    temp1 = line; 
    temp2 = line; 
} 

그러나 처음 두 줄을 다른 변수에 저장하려면 루프가 필요하지 않습니다. 당신의 config.txt 파일이 코드가 while 상태에 소비 한 줄, 다음 temp1temp2을 포함, 그래서 만약 readLine를 호출하는 다른 사람이 이미 지적했듯이

String line = ""; 
if ((line = br.readLine()) != null) 
    temp1 = line; 
if ((line = br.readLine()) != null) 
    temp2 = line; 

는 선을 소비 얻을 null 더 이상 텍스트가 없기 때문에 할당 읽다.

+0

나는 이것을 사용하고있다 FileInputStream fstream = 새로운 openFileInput ("config.txt"); 하지만 작동하지 않습니다. 그것은 openFileInput 것을 해결할 수 없습니다. – xkrlaix

+0

내 대답에 링크되어있는 문서에서 알 수 있듯이'openFileInput'은'Context'에서 파생 된'Context'의 메소드입니다. 따라서 활동에서 스 니펫을 사용하는 경우 'openFileInput'을 이미 사용할 수 있어야합니다. 그렇지 않으면 파일을 읽을 메소드 나 클래스에'Context' 인스턴스를 전달해야합니다. 덕분에 –

+0

. 그것은 실제로 일했습니다. – xkrlaix

1
try{ 
     // Open the file that is the first 
     // command line parameter 
     FileInputStream fstream = new FileInputStream("config.txt"); 
     // Get the object of DataInputStream 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String line = ""; 
     while ((line = br.readLine()) != null) { 
      temp1 = line; 
      temp2 = line; 

     } 

     in.close(); 
}catch (Exception e){//Catch exception if any 
Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show(); 
} 
Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show(); 
1

br.readLine() in은 (는) 이미 선을 소비합니다.

보십시오 당신이 볼 예외이

LineNumberReader reader = new LineNumberReader(new FileReader("config.txt"))); 
    String line; 
    while ((line = reader.readLine()) != null) { 
     //doProcessLine 
    } 
0

당신은 당신이해야 할 첫 두 줄을 저장하려면 :

try 
{ 
    // Open the file that is the first 
    // command line parameter 
    FileInputStream fstream = new FileInputStream("config.txt"); 
    // Get the object of DataInputStream 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String line = ""; 
    if((line = br.readLine()) != null) 
     temp1 = line; 
    if((line = br.readLine()) != null) 
     temp2 = line; 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
+0

실제로 "config.txt"를 찾을 수 없기 때문에 예외가 표시됩니다. 하지만 만약 내가 사용 "FileInputStream fstream = 새로운 openFileInput ("config.txt ");" 그때 그것을 인식하지 못합니다. – xkrlaix

+0

확인. 그것은 또 다른 문제입니다. 어쨌든,이 코드는 파일의 처음 2 줄을 가져 오는 데 필요합니다 (존재할 때). :) – SERPRO

+0

예, 실제로 존재합니다. 나는 그들을 따로 보았다. – xkrlaix

관련 문제