2016-12-23 1 views
-3

열 단지 단어를 얻을 수서식 문자열 내가 텍스트가

c 
MyMP3s 
4 
Non 
Blindes 
Bigger 
Faster 
More 
Train 
mp3 

그리고 이것 모두를 파일에 써라. 다음은 내가 한 일입니다.

public static void formatText() throws IOException{ 

    Writer writer = null; 
    BufferedReader br = new BufferedReader(new FileReader(new File("File.txt"))); 

    String line = ""; 
    while(br.readLine()!=null){ 
     System.out.println("Into the loop"); 

     line = br.readLine(); 
     line = line.replaceAll(":", " "); 
     line = line.replaceAll(".", " "); 
     line = line.replaceAll("_", " "); 

     line = System.lineSeparator(); 
     System.out.println(line); 
     writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Write.txt"))); 
     writer.write(line); 
    } 

그리고 작동하지 않습니다!

예외 : 코드의 끝에

Into the loop 
Exception in thread "main" java.lang.NullPointerException 
    at Application.formatText(Application.java:25) 
    at Application.main(Application.java:41) 
+1

라인 + = System.lineSeparator()'과 같은 의미입니까?;'? –

+0

프로그램의 출력물을 게시 할 수 있습니까? – SteelToe

+0

@ PM77-1 출력을 쓰겠습니다 –

답변

1

, 당신은이 : 이것은 당신의 교체를 재설정

line = System.lineSeperator()

. 주목할 또 다른 점은 String#replaceAll이 첫 번째 매개 변수의 정규식을 사용한다는 것입니다. 그래서 당신은 .

String line = "c:\\MyMP3s\\4 Non Blondes\\Bigger!\\Faster, More!_Train.mp3"; 
System.out.println("Into the loop"); 

line = line.replaceAll(":\\\\", " "); 
line = line.replaceAll("\\.", " "); 
line = line.replaceAll("_", " "); 
line = line.replaceAll("\\\\", " "); 

line = line.replaceAll(" ", System.lineSeparator()); 

System.out.println(line); 

등, 어떤 순서를 탈출해야 출력은 다음과 같습니다

Into the loop 
c 
MyMP3s 
4 
Non 
Blondes 
Bigger! 
Faster, 
More! 
Train 
mp3