Java reads how the contents of the file are displayed on one line

reads its contents from a .txt, with similar information in the text as follows:
Info 1
Info 2
Info 3
.
the code I wrote is
public class FileMessage {

public static void main(String[] args) throws Exception {
    File file  = new File("D:"+File.separator+"test.txt");
    if(!file.exists()){
        throw new Exception("");
    }
    InputStream is = new FileInputStream(file);
    byte[] byt = new byte[(int)file.length()]; 
    is.read(byt);
    is.close();
    String result = new String(byt);
    result.replace("\r", "");
    result.replace("\n", "");
    System.out.println(":"+result);
}

}
the data read is always divided into multiple lines, but the requirements need to be displayed on one line. I would like to ask you why the replace () method does not work and remove the newline characters in the string. Or is there any other more elegant way to implement it?

Mar.16,2021

java.nio.file.Files this utility class has API, requirements for you that provide streaming text files:

Files.lines(Paths.get("D:", "test.txt")).collect(Collectors.joining());

java8


public class FileMessage2 {

public static void main(String[] args) throws Exception {
    File file  = new File("D:"+File.separator+"test.txt");
    if(!file.exists()){
        throw new Exception("");
    }

/ / InputStream is = new FileInputStream (file);
/ / byte [] byt = new byte [(int) file.length ()];
/ / is.read (byt);
/ / is.close ();
/ / String result = new String (byt);
/ / result.replace ("rn", ");

    
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    StringBuffer sb = new StringBuffer();
    String str;
    while((str=br.readLine())!=null){
        sb.append(str);
    }
    br.close();
    fr.close();
    System.out.println(":"+sb);
}

}
uses a way to read by row, but I still don't know why the replace () method is invalid in the first writing, sad:-(

Menu