账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Java 读文件内容如何显示在一行上
    20
    0

    从一个.txt中读取其内容,文本中有类似信息如下:
    信息1
    信息2
    信息3
    。。。
    我写的代码是:
    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);
    }

    }
    读到的数据总是分成了多行,但需求需要显示在一行,想请教一下大家为什么replace()方法没有起作用,去掉字符串里面的换行符呢?或者有没有其他更优雅一些的实现方法呢?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部