2016-04-29 2 views
1

注釈付きファイルをコピーして、新しいコピー内のそれらの注釈を置き換えたいとします。しかし、私は交換を行う方法に苦労しています。私は現在の文字列にファイル全体を読み込み、新しいファイルに文字列を保存する前に注釈を交換しています:ファイル内の特定の式を置換するJava

String file = null; 

void openAnnotatedSource(String path){ 

    byte[] encoded = null; 
    try { 
     encoded = Files.readAllBytes(Paths.get(anotatedpath + "/" + path)); 
    } catch(Exception e) { 
     System.out.println("Error opening annotated source."); 
    } 

    file = new String(encoded, StandardCharsets.UTF_8); 

} 

void replaceAnotation(String anotation, String config){ 
    file = file.replace(anotation, config); 
} 

void replaceAnotation(String anotation, int config){ 
    file = file.replace(anotation, String.valueOf(config)); 
} 

void createFinalSource(String path){ 
    try{ 
     Files.write(Paths.get(targetpath + "/" + path), file.getBytes()); 
    } catch(Exception e) { 
     System.out.println("Couldnt create " + targetpath + "/" + path); 
    } 
} 

私はファイルを持っ全体の時間理由としてこれを正しくやっている場合、私は知りません文字列が私にとって正しいとは思わない。

答えて

0

すべてのまともなテキストエディタには、正規表現をサポートする代替検索機能&があります。

しかし、あなたはJavaでホイールを再発明する必要がある場合は、あなたが従ったアプローチがまともです。変更された内容を別の新しいファイルに書き込むので、ソースファイルから内容を文字列に読み込み、 stringの内容と更新された文字列を含む新しいファイルを作成しても問題は発生しません。

関連する問題