0

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

}
umb
  • 3
  • 2

1 Answers1

0

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?

Community
  • 1
  • 1
Russell Zahniser
  • 16,188
  • 39
  • 30