2012-04-22 2 views
3

나는 파일을 읽는 메소드가있는 프로젝트에서, 전달 된 문자열을 찾고 temp라는 새 파일에 쓴다. 그 다음에 원래 이름을 삭제하고 임시 이름을 임시 이름으로 변경합니다. 오류없이 실행할 수 있지만 새 파일에 아무것도 출력하지 않습니다. 나는 보통 디버깅을했는데 오류가 라인에 새로운 파일을 작성하는 라인에 있다는 것을 발견했다. 뭔가 잘못된 것을 부르는 작은 오류를 만든 것처럼 느껴집니다. 그것은 좋은 것입니다 해결에 어떤 도움을 ... 여기자바가 파일을 읽고 줄을 제거한다.

코드

public static void LineDelete(String Filename, String Content) throws IOException { 
    try { 
     File flights = new File("AppData/" + Filename); 
     File temp; 
     temp = new File("AppData/temp.txt"); 
     FileWriter fstream; 
     BufferedWriter out; 
     try (Scanner sc = new Scanner(flights)) { 

      fstream = new FileWriter("AppData/temp.txt", true); 
      out = new BufferedWriter(fstream); 
      boolean exis = temp.exists(); 
      if (exis) { 
       temp.delete(); 
       temp = new File("AppData/temp.txt"); 
       boolean createNewFile = temp.createNewFile(); 
      } else { 
       boolean creatNewFile = temp.createNewFile(); 
      } 
      String f; 
      while (sc.hasNextLine()) { 
       f = sc.nextLine(); 
       if (!f.equals(Content)) { 

        out.newLine(); 
        out.write(f); 

       } 

      } 
     } 
     fstream.close(); 
     //out.close(); 
     flights.delete(); 
     File flightsn = new File("AppData/" + Filename); 
     temp.renameTo(flightsn); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

}

답변

4

당신은 out (BufferedReader의)에 가까운 호출해야에게 있습니다.

또한 try-catch-finally의 finally 절에서이를 닫아야합니다.

이 코드는해야 더 많거나 적은

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class FileWrite { 
    public static void LineDelete(String Filename, String Content) 
      throws IOException { 
     BufferedWriter out = null; 
     File flights = new File("AppData/" + Filename); 
     File temp = new File("AppData/temp.txt"); 
     FileWriter fstream = null; 

     try { 
      Scanner sc = new Scanner(flights); 
      fstream = new FileWriter("AppData/temp.txt", true); 
      out = new BufferedWriter(fstream); 
      boolean exis = temp.exists(); 
      if (exis) { 
       temp.delete(); 
       temp = new File("AppData/temp.txt"); 
       boolean createNewFile = temp.createNewFile(); 
      } else { 
       boolean creatNewFile = temp.createNewFile(); 
      } 
      String f; 
      while (sc.hasNextLine()) { 
       f = sc.nextLine(); 
       if (!f.equals(Content)) { 
        out.newLine(); 
        out.write(f); 
       } 

      } 
     } catch (IOException ex) { 
      Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      try { 
       out.close(); 
      } catch (Exception e) { 
       Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, e); 
      } 
     } 

     if(flights.exists()){   
      flights.delete(); 
      File flightsn = new File("AppData/" + Filename); 
      temp.renameTo(flightsn); 
     } 
    } 
} 
+0

이것은 단지 내가 작은 문제를 파악 – Charlie

+0

확인 파일 flights.txt을 ... 삭제로 확인 내가 뭔가를 놓친 거지, 다른 하나 개의 질문, 때를 빈 줄을 지우면 모든 비어있는 줄을 지울 수있는 방법이 있습니까? – Charlie

+0

여기서 끝내기 전에 삭제하고 이름을 바꿉니다! –

관련 문제