从文件加载断行符号“\ n”

时间:2012-01-12 18:21:38

标签: java bufferedreader

我需要从文本文件加载,我将替换其他字符串。 例如,我有文本文件:

\n;(br)

加载此文件后,我需要将所有断行更改为(br),这样我才会收到一个行字符串。 问题是我从文件加载文字时 - 我没有收到字符串\n;(br)但是\\n;(br)

任何人都知道怎么做?

我的代码 - 我知道我正在添加' \ n'在方法applyFilters中,但是因为当我不想改变它时可能存在这种情况。

    void loadSource(){

    File file = new File(sourcePath);
    BufferedReader reader=null;
    String text;
    try{
        reader = new BufferedReader( new FileReader(file));         
    }catch(FileNotFoundException e){            
        e.printStackTrace();
    }   

    try{
        while((text = reader.readLine()) != null){
            sourceText.add(text);               
        }
    }catch(Exception e){
        e.printStackTrace();
    }       

}

void loadFilters(){

    File file = new File(filterPath);
    BufferedReader reader=null;
    String text;

    try{
        reader = new BufferedReader( new FileReader(file));         
    }catch(FileNotFoundException e){
        System.out.println("Błąd, brak pliku źródłowego");
        e.printStackTrace();
    }

    try{
        while((text = reader.readLine()) != null){
            filterText.add(text);               
        }
    }catch(Exception e){
        e.printStackTrace();
    }       

}

void applyFilters(){

    for (String s : sourceText){
        finalText = finalText+ s + "\n";            
    }
    for(String filter : filterText)
    {           
        finalText = finalText.replace(filter.split(";")[0],filter.split(";")[1]);
    }
    System.out.println(finalText);

}

1 个答案:

答案 0 :(得分:0)

听起来您希望文本文件中的“\ n”表示换行符而不是反斜杠后跟n。看看这个问题:

How to unescape a Java string literal in Java?

相关问题