2016-12-23 6 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 
関連する問題