2011-03-04 7 views
1

나는 파일에서 읽으려고하고, 각 줄의 길이를 취하고, 길이를 다른 파일에 쓰고, 두 번째 파일을 출력하여 글 쓰기 결과를 보여 주지만, 내가 열 때 두 번째 파일은 결과가 내가 원하는 것과 정확히 일치하지 않습니다. 이 코드를 실행 한 후 파일에 숫자가 많이 있습니다.자바에서 파일을 읽고 쓰자.

String line = null; 

    boolean flag = false; 

    BufferedReader bf = new BufferedReader(new FileReader("c:\\lm_giga_5k_nvp_2gram.arpa")); 

    BufferedWriter index = new BufferedWriter(new FileWriter("c:\\index.txt")); 
    int l; 

    int counter=0; 

    while ((line = bf.readLine()) != null) 

    { 

     l=line.length(); 

     index.write(l + "\n"); 

    } 

    BufferedReader bf1 = new BufferedReader(new FileReader("c:\\index.txt")); 

    String line1=null; 

    while ((line1 = bf1.readLine()) != null) 


    { 
     System.out.println(line1); 


    } 

    bf.close(); 

    bf1.close(); 

이 예제를 사용하여 도움을주십시오. 색인을 닫지 만 여전히 동일한 문제가 있습니다.

알림 : arpa 파일에주의하지 마십시오. 대신 txt 파일을 이미지 할 수 있습니다.

+0

숙제인가? –

+0

파일을 읽고 쓰고 나면 파일을 닫지 않았습니까? '당신이 원하는 정확한 것'은 무엇입니까? 첫 번째 파일에서 두 번째 파일까지 각 행의 길이를 인쇄하려고한다고 가정합니다. 그게 네가 가진거야. –

+0

출력은 어떻게됩니까? – CloudyMarble

답변

2

다른 장소에서 열기 전에 index.txt를 닫아야합니다. 또는 적어도 flush it :

... 
index.close(); 

BufferedReader bf1 = new BufferedReader(new FileReader("c:\\index.txt")); 

String line1=null; 
... 
+0

이제는 색인을 닫지 만 여전히 같은 문제가 있습니다 – user642564

+1

출력 결과를 표시 할 수 있습니까? – trojanfoe

+0

나는 내 문제가있어, 나는 index.newLine()를 사용해야한다; "\ n"을 사용하는 대신 다음 행으로 이동하십시오. – user642564

0

다음은 쓰기 및 읽기 방법입니다. 필요에 맞게 사용자 정의 할 수 있습니다.

public boolean writePublic(String strWrittenString, String writeFileName, String writeEncoding, boolean appendString) { 

    try { 
     //System.out.println("Writing to file named " + writeFileName + " ..."); 
     Writer out = new OutputStreamWriter(new FileOutputStream(writeFileName, appendString), writeEncoding); 
     try { 
      out.write(strWrittenString); 

     } finally { 
      out.close(); 
     } 

     //System.out.println("Writing to file named " + writeFileName + "- success."); 
     return true; 
    } catch (IOException ioe) { 
     System.out.println("file named " + writeFileName + "-Failed. cause: " + ioe.getMessage()); 
     return false; 
    } catch (Exception e23) { 
     System.out.println("file named " + writeFileName + "-Failed. cause: " + e23.getMessage()); 
     return false; 
    } 

} 

및 읽기 방법 :

또한
public static String readWithoutEncoding(String readFileName) { 
    StringBuilder text = new StringBuilder(); 
    try { 
     //System.out.println("Reading from file named " + readFileName + " ..."); 

     String NL = System.getProperty("line.separator"); 
     Scanner scanner = new Scanner(new FileInputStream(readFileName)); 
     try { 
      while (scanner.hasNextLine()) { 
       text.append(scanner.nextLine() + NL); 
      } 
     } finally { 
      scanner.close(); 
     } 
    // System.out.println("Text read in: " + text); 
    //System.out.println("Reading from file named " + readFileName + "- success."); 
    } catch (IOException ioe) { 
     System.out.println("file named " + readFileName + "-Failed. cause: " + ioe.getMessage()); 
    } catch (Exception e23) { 
     System.out.println("file named " + readFileName + "-Failed. cause: " + e23.getMessage()); 


    } finally { 
     return text.toString(); 
    } 

} 

방법 위에 포함 된 자바 클래스의 시작 부분에 필요한 가져 오기 문을 배치하는 것을 잊지 마세요 :

import java.io.*; 
import java.util.Scanner; 
관련 문제