Windows和Linux之间的行结尾有何不同? [英] Do line endings differ between Windows and Linux?

查看:158
本文介绍了Windows和Linux之间的行结尾有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java解析Linux / etc / passwd 文件。我目前正在通过 java.util.Scanner class然后使用 java.lang.String.split(String) 分隔每个行。

I am trying to parse the Linux /etc/passwd file in Java. I'm currently reading each line through the java.util.Scanner class and then using java.lang.String.split(String) to delimit each line.

问题在于行:

list:x:38:38:Mailing List Manager:/var/list:/bin/sh" 

是由扫描仪处理为3个不同的行:

is treated by the scanner as 3 different lines:


  1. list:x:38:38:Mailing

  2. 列表

  3. 经理......

  1. list:x:38:38:Mailing
  2. List
  3. Manager...

当我输入一个我没有从Linux获得的新文件时, Scanner 正确解析它。

When I type this out into a new file that I didn't get from Linux, Scanner parses it properly.

我是否对Linux中的新行没有了解?

Is there something I'm not understanding about new lines in Linux?

显然是一个解决它是在不使用扫描仪的情况下解析它,但它不会很优雅。有没有人知道这样做的优雅方法?

Obviously a work around is to parse it without using scanner, but it wouldn't be elegant. Does anyone know of an elegant way to do it?

有没有办法将文件转换为可与扫描仪

Is there a way to convert the file into one that would work with Scanner?

两天前还没有:不同行间不同平台背后的历史原因

编辑

原作者注意:


我发现我有一个不同的错误导致问题。无视问题


推荐答案

来自维基百科



  • LF:Multics,Unix和类Unix系统(GNU / Linux ,AIX,Xenix, Mac OS
    X
    ,FreeBSD等),BeOS,Amiga,RI SC
    OS和其他

  • CR + LF:DEC RT-11和大多数其他早期非Unix,非IBM操作系统,CP / M,
    MP / M, DOS ,OS / 2,Microsoft Windows
    Symbian OS

  • CR:Commodore机器,Apple II家庭, Mac OS高达版本9
    OS-9

  • LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
  • CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
  • CR: Commodore machines, Apple II family, Mac OS up to version 9 and OS-9

我将此翻译成这些行结尾一般

I translate this into these line endings in general:


  • Windows:'\\\\ n'

  • Mac(OS 9-):'\ r'

  • Mac(OS 10) +):'\ n'

  • Unix / Linux:'\ n'

  • Windows: '\r\n'
  • Mac (OS 9-): '\r'
  • Mac (OS 10+): '\n'
  • Unix/Linux: '\n'

您还需要让您的扫描仪/解析器处理unix版本。

You need to make your scanner/parser handle the unix version, too.

这篇关于Windows和Linux之间的行结尾有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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