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

发布于 2024-12-27 04:57:57 字数 1593 浏览 3 评论 0原文

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

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

}

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.
Problems is when I'm loading text from file - I don't get string \n;(br) but \\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.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-01-03 04:57:57

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

如何在 Java 中对 Java 字符串进行转义?

It sounds like you want the "\n" in your text file to represent a newline character rather than a backslash followed by an n. Have a look at this question:

How to unescape a Java string literal in Java?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文