2013-08-27 4 views
0

두 파일에서 데이터를 읽어야합니다. 이를 위해이 두 파일을 while을 사용하여 반복해야합니다. A,있는 File2에서 B, C, D // 데이터가 A, B가 여기 내 코드 ...을 File1에서외부 while 루프 반복 계속

// 데이터는 루프 동안 내가 사용하고 C

Scanner scanFile = new Scanner(new DataInputStream(fOne)); 
       while (scanFile.hasNextLine()) 
       { 
        countTwo = 0; 
        if(scanFile.nextLine()!=null) 
        { 
         count++; 
         Toast.makeText(getBaseContext(), "Count : " + count, 500).show(); 
        } 
        else 
         scanFile.close(); 

        Scanner scanFileT = new Scanner(new DataInputStream(fTwo)); 
        while(scanFileT.hasNextLine()) 
        { 
         if(scanFileT.nextLine()!=null) 
         { 

          countTwo++; 
          Toast.makeText(getBaseContext(), "CountTwo : " + countTwo, 500).show();     
         } 

         else 
          scanFileT.close(); 
        } 


       } 

되어있다. 여기서 내가 처음으로 count = 1 및 countTwo 변수를 1, 2 및 3으로하고 다시 변수를 2, 3, 4로 계산합니다 (file1의 데이터가 4이고 file2의 데이터가 3 임). 이제 count = 2 및 countTwo = 1, 2, 3으로 카운트 값을 얻는 것과 같이 outer while 반복문을 반복해야합니다. 다시 count = 3 및 countTwo = 1, 2, 3 다시 count = 4 및 countTwo = 1, 2 3. 무엇을해야합니까? 완전히 가지고 루프, 또는 방금 scanFile의 첫 번째 반복 후에 루프

+0

정확하게 이해하면 countTwo가 매회 증가 할 때마다 1부터 시작하기를 원합니다. 그렇다면 루프 내부에 들어가기 전에 countTwo = 1을 지정해야한다고 생각합니다! – Ashish

+0

사실, 여기 파일의 내용을 비교해야합니다. 첫 번째 파일의 첫 번째 내용은 두 번째 파일의 모든 내용과 일치합니다. 다시 첫 번째 파일의 두 번째 내용은 두 번째 파일의 모든 내용과 일치하며 첫 번째 파일의 마지막 내용까지 일치합니다. –

+0

예제가 컴파일되지 않습니다. 자신이하는 일의 실행 가능한 예를 제공해주십시오. –

답변

1
try 
      { 
      FileInputStream fOne = openFileInput("File1"); 

      } 
      catch (FileNotFoundException e) 
      { 
       e.printStackTrace(); 
      } 
      Scanner scanFile = new Scanner(new DataInputStream(fOne)); 
      while (scanFile.hasNextLine()) 
      { 
       countTwo = 0; 
       if(scanFile.nextLine()!=null) 
       { 
        count++; 
        Toast.makeText(getBaseContext(), "Count : " + count, 500).show(); 
       } 
       FileInputStream fTwo = openFileInput("File2"); 
       Scanner scanFileT = new Scanner(new DataInputStream(fTwo)); 
       while(scanFileT.hasNextLine()) 
       { 
        if(scanFileT.nextLine()!=null) 
        { 

         countTwo++; 
         Toast.makeText(getBaseContext(), "CountTwo : " + countTwo, 500).show();     
        } 
       } 
       fTwo.close(); 
      } 

는 외부 내부 사용자의 SECON 파일을 읽기 첫 번째 파일의 모든 단일 라인, 즉 대신 외부의 루프, 두 번째 파일을 읽을 수 있습니다 동안 countTwo가 1에서 다시 시작됩니다

+0

동일한 출력을 얻기 ... 아무것도 작동하지 않습니다 ... –

+0

@Anil 편집 코드, 초기화하여 countTwo 변수를 편집 한 바깥 쪽에서 0으로 루프 While – Siva

+0

같은 출력 .... –

0

는 외부의 시작에서 countTwo = 1을 할당 할 수 scanFileT가 소진 될 그것을 반복했다. 다시 초기화해야합니다. 당신의 업데이트 후

Scanner scanFileT = new Scanner(new DataInputStream(fTwo)); 
while(scanFileT.hasNextLine()) 
{ 
. 
. 
} 

:

Scanner scanFileT = new Scanner(new DataInputStream(fTwo)); 
while(scanFileT.hasNextLine()) 
{ 

} 
scanFileT.close(); 
1

하면서 내부에 들어가기 전에이 값을 할당 할 수 있지만

+0

동일한 출력 얻기 ... 아무 것도 작동하지 않습니다 ... –

+0

죄송합니다. 제 설명은 정확하지만 재 초기화하기 전에 스캐너를 닫고 싶을 수 있습니다. –

+0

이봐, 너 무슨 소리하는거야? 다시 초기화하기 전에 루프 –

0
try 
        { 
         fOne = openFileInput("File1"); 
         //fTwo = openFileInput("File2"); 
        } 
        catch (FileNotFoundException e) 
        { 
         e.printStackTrace(); 
        } 

        Scanner scanFile = new Scanner(new DataInputStream(fOne)); 
        while (scanFile.hasNextLine()) 
        { 
         countTwo = 0; 
         if(scanFile.nextLine()!=null) 
         { 
          count++; 

         } 

         try 
         { 
          fTwo = openFileInput("File2"); 
         } 

         catch (FileNotFoundException e) 
         { 
          e.printStackTrace(); 
         } 


         Scanner scanFileT = new Scanner(new DataInputStream(fTwo)); 
         while(scanFileT.hasNextLine()) 
         { 
          if(scanFileT.nextLine()!=null) 
          {       countTwo++; 

          } 
         } 
         scanFileT.close(); 


        } 
        count = 0;