简体   繁体   English

从文件加载换行符“ \\ n”

[英]Loading break line sign “\n” from file

I need to load from text file text which I will be replacing in other string. 我需要从文本文件中加载要替换为其他字符串的文本。 For example I have text file: 例如我有文本文件:

\n;(br)

After loading this file I need to change all break lines to (br) so I will receive one line string. 加载此文件后,我需要将所有换行更改为(br)这样我将收到一个行字符串。 Problems is when I'm loading text from file - I don't get string \\n;(br) but \\\\n;(br) 问题是当我从文件加载文本时-我没有得到字符串\\n;(br)但是\\\\n;(br)

Anyone know how to do that? 有人知道该怎么做吗?

My code - I know that I'm adding '\\n' in method applyFilters but it is because that there can be situation when I don't whant to change that. 我的代码-我知道我在方法applyFilters中添加了'\\ n',但这是因为在某些情况下我不想更改它。

    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);

}

It sounds like you want the "\\n" in your text file to represent a newline character rather than a backslash followed by an n. 听起来您想让文本文件中的“ \\ n”代表换行符,而不是后跟n的反斜杠。 Have a look at this question: 看一下这个问题:

How to unescape a Java string literal in Java? 如何在Java中取消对Java字符串文字的转义?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM