为什么我在 Windows 中将 \r\r\n 作为换行符而不是 \r\n 作为换行符 [英] Why I get \r\r\n as newline instead of \r\n as newline char in Windows

查看:101
本文介绍了为什么我在 Windows 中将 \r\r\n 作为换行符而不是 \r\n 作为换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的 readfile() java 函数来读取 .htm 文件

I have below readfile() java function to read .htm files

private String readfile(String inputDoc) throws IOException {
    FileInputStream fis = null;
    InputStreamReader isr = null;
    String text = null;
    //open input stream to file
    fis = new FileInputStream(inputDoc);
    isr = new InputStreamReader(fis, "UTF-8");
    StringBuffer buffer = new StringBuffer();
    int c;
    while( (c = isr.read()) != -1 ) {
        buffer.append((char)c);
    }
    text = buffer.toString();
    isr.close();
    return text;
}

这是输入文档的示例片段

Here is example snippet of input doc

<?xml version="1.0" encoding="utf-8"?><html>

<head>

出于某种原因,从 readfile() 返回的文本字符串是 <?xml version="1.0" encoding="utf-8"?><html>\r\r\n<head>

For some reason text string returned from readfile() is <?xml version="1.0" encoding="utf-8"?><html>\r\r\n<head>

但我希望它是 <?xml version="1.0" encoding="utf-8"?><html>\r\n<head>

正如其概述 此处 Windows 中的换行符 \r\n

as it is outlined here newline char in windows \r\n

我在 Windows 7 上的 IntelliJ Idea 中运行了上述函数.(IDEA 默认编码设置为 UTF-8)

I ran above function in IntelliJ Idea on Windows 7. (IDEA default encoding is set to UTF-8)

有谁知道为什么我从换行的 readfile(String inputDoc) 函数中得到这个奇怪的结果

Does anyone know why I get this weird result from readfile(String inputDoc) function for newline

推荐答案

你得到这个是因为它在输入文件中是这样的.尝试在十六进制编辑器中打开输入文件进行验证.

You get this because it is like this in the input file. Try to open the input file in a hex editor to verify.

这篇关于为什么我在 Windows 中将 \r\r\n 作为换行符而不是 \r\n 作为换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆