從一個(gè).txt中讀取其內(nèi)容,文本中有類似信息如下:信息1信息2信息3。。。我寫的代碼是:publicclassFileMessage{publicstaticvoidmain(String[]args)throwsException{Filefile=newFile("D:"+File.separator+"test.txt");if(!file.exists()){thrownewException("抱歉,您請求的文件路徑不存在!");}InputStreamis=newFileInputStream(file);byte[]byt=newbyte[(int)file.length()];is.read(byt);is.close();Stringresult=newString(byt);result.replace("\r","");result.replace("\n","");System.out.println("從文件中讀到的結(jié)果:"+result);}}讀到的數(shù)據(jù)總是分成了多行,但需求需要顯示在一行,想請教一下大家為什么replace()方法沒有起作用,去掉字符串里面的換行符呢?或者有沒有其他更優(yōu)雅一些的實(shí)現(xiàn)方法呢?
Java 讀文件內(nèi)容如何顯示在一行上
三國紛爭
2019-05-21 09:29:24