开发者社区> 问答> 正文

在JAVA中,当字符串末尾包含\ r \ n时,哪种方法等效于strlen()c方法?

File file = new File(Path); reader = new BufferedReader(new FileReader(file )); String readLine = "";

while ((readLine = reader.readLine()=) != null) {

}

观察:索引1996:ascii值32索引1997:ascii值32索引1998:ascii值13/r索引1999:ascii值10/n索引2000:ascii值0空索引2001:ascii值50索引2002:ascii值50索引2002:ascii值50

readline.length在java中是1998,因为readline方法认为13是行尾。当从C中读取同一个文件时,其字符串长度为2000。

展开
收起
小六码奴 2019-10-14 18:37:17 6731 0
2 条回答
写回答
取消 提交回答
  • 精于基础,广于工具,熟于业务。

    readLine方法是按行读取的。方法的官方解释。Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.当读取到\r\n时,会把换行符自己过滤掉的。

    2019-10-15 09:32:28
    赞同 展开评论 打赏
  • 尝试运行以下代码:

    class Scratch { public static void main(String[] args) { System.out.println("Hello\r\n".length()); } } 看不到关于“ Hello \ r \ n”长度为5的假设是错误的。

    2019-10-14 18:38:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载