繁体   English   中英

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

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

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

\n;(br)

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

有人知道该怎么做吗?

我的代码-我知道我在方法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);

}

听起来您想让文本文件中的“ \\ n”代表换行符,而不是后跟n的反斜杠。 看一下这个问题:

如何在Java中取消对Java字符串文字的转义?

暂无
暂无

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

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