2012-06-11 3 views
0

알아낼 수 없습니다. 내 프로그램에서 텍스트를 쓰려고합니다. 그것은 단어 카운트 프로그램을 가정합니다. 나에게 줄 수, 문자 수, 단어 수를 보여주십시오. 그런 다음 사용자가 검색하는 단어와 그 행과 함께 결과를 표시합니다.버퍼링 된 출력이 잘못된 파일에 쓰기

(즉, 검색 자바)

라인 5 : 자바 섬 자바

라인 (9)을 포함 : 나는 자바 그것은 텍스트를 표시 아니에요

에게 술을 좋아한다. heiroglyphics와 같은 그것의 표시.

라인 2 : DN {c < \ $ H Uz X h4 [ bA. D Ja 8 ^) | k ˠ < ΤQJP˒nI "(vcBi"&/| qIW6 {PA0의 [[M ! FU } 4 배 {- (Vk을 우리는 T"을 ֭ @ 3 호선 : NU Ӣ ͇ ? 4 호선 : Ӻ 鬵 PD <} L> oVExQ |) 'gIB3b ("3 T 7 : : : : : : : : : : : : : : : : PK! N rels/.rels (

public void readFromFile(String filename) 
{ 
    LineNumberReader lineNumberReader = null; 
    try { 
     lineNumberReader = new LineNumberReader(new FileReader(filename)); 
     String line = null; 
     BufferedWriter output = new BufferedWriter(new FileWriter("output.txt")); 
     String ask = "Enter Word"; 
     String find = JOptionPane.showInputDialog(ask);  
     Scanner scan = new Scanner(new File("test.txt")); 
     while ((line = lineNumberReader.readLine()) != null) 
     { 
      line = scan.nextLine(); 
      if(line.indexOf(find) >= -1) 
      { 
      output.write("Line " + lineNumberReader.getLineNumber() + 
        ": " + line); 
      output.newLine(); 
      } 
     }// end of while 
     output.close(); 
     } // end of try 
     catch (FileNotFoundException ex) 
     { 
      ex.printStackTrace(); 
     } 
     catch (IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
    finally {  
     try { 
      if (lineNumberReader != null) 
       { 
       lineNumberReader.close(); 
       } 
      } // end of try 
     catch (IOException ex) 
      { 
      ex.printStackTrace(); 
      } 
     }// end of finally 
} // end of function 
+0

에서 오는 파일에서 읽는 이유를 이해하지 않습니다. 훨씬 더 읽기 쉬울 것입니다! – Maccesch

+1

while() 및 while 루프 내에서 루프 검사에서 줄을 겹쳐 쓰는 이유는 무엇입니까? 나는 당신의 프로그램이 무엇을하고 있는지조차 모르겠다 ... –

+0

당신은 어떤 텍스트 편집기를 통해 당신의 파일을 읽으려고 하는가? – mawia

답변

0

기본 OS 인코딩은 System.getProperty("file.encoding")에 설정된대로 사용됩니다. 명시 적으로 선택할 수 있습니다.

final String encoding = "UTF-16LE"; // Or "Cp1252" or "UTF-8" 

    lineNumberReader = new LineNumberReader(new InputStreamReader(new FileInputStream(filename), encoding)); 
    ... 
    BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.txt"), encoding)); 
    ... 
    Scanner scan = new Scanner(new File("test.txt", encoding)); 
+0

에 인코딩 된 경우가 나타나면을 FileReader는 인코딩 매개 변수를 고려하지 않습니다, 당신은 알렉스가 말한대로 FileInputStream에와 InputStreamReader는 – Alex

+0

@Joop Eggen이 작동하지 않는 사용해야합니다. 컴파일러는 유효하지 않은 인수를 말하고 있으며 해당 consturctor는 적용 할 수 없습니다. – user1444775

+0

@Alex thanks; 여전히 (멀티) 바이트 cpde가 무엇을 제공 할 수 있는지 궁금해하고있었습니다. 아마도 기존 파일은 UTF16LE이었을 것입니다. –

1

나는 당신이이 일을 왜하지 않는다 : 대신의

while ((line = lineNumberReader.readLine()) != null) 
     { 
      line = scan.nextLine(); 
      if(line.indexOf(find) >= -1) 
      { 
      output.write("Line " + lineNumberReader.getLineNumber() + 
        ": " + line); 
      output.newLine(); 
      } 
     }// end of while 

을 :

while ((line = lineNumberReader.readLine()) != null) 
     { 
      if(line.indexOf(find) >= -1) 
      { 
      output.write("Line " + lineNumberReader.getLineNumber() + 
        ": " + line); 
      output.newLine(); 
      } 
     }// end of while 

이 2 개 독자가 필요하지 않습니다. 그리고 독자 중 하나가 최종 파일에 읽고 다른 하나는 이름이 제대로 소스 코드를 들여하시기 바랍니다 인수