Java读取换行符

Java读取换行符,java,file,file-io,linefeed,Java,File,File Io,Linefeed,我正在尝试从包含\n的字符串编写的文本文件中读取数据。因此,这些字符被写为换行符。当我试着读回它的时候 我使用的是FileInputStream,InputStreamReader和BufferedReader。但是,当我阅读每一行时,读者会注意到换行符,并将其计为行尾。 我真正想要的是读到行尾,保留换行符。我试着看看在创建文件时,我是否真的能写出字符\和n,但不管我怎么做,它们都会变成正常的换行符 如何在保留换行符的同时读取此文件 编辑:为了澄清,这里是我正在阅读的数据样本。如图所示,表示Ar

我正在尝试从包含
\n
的字符串编写的文本文件中读取数据。因此,这些字符被写为换行符。当我试着读回它的时候

我使用的是
FileInputStream
InputStreamReader
BufferedReader
。但是,当我阅读每一行时,读者会注意到换行符,并将其计为行尾。 我真正想要的是读到行尾,保留换行符。我试着看看在创建文件时,我是否真的能写出字符
\
n
,但不管我怎么做,它们都会变成正常的换行符

如何在保留换行符的同时读取此文件

编辑:为了澄清,这里是我正在阅读的数据样本。如图所示,表示
Args
的每一行都在自己的行上。这是我想忽略的换行符。我希望每个命令(例如dt、e、b、c、o等)都有自己的行。理想情况下是这样的:

o Desc: Opens a file or folder#Args: [<handle or path>] -- the path of the file or folder#(<file name>) -- the name of the file to open. Omit if you are opening a folder.
我试着看看我是否能真正写出这些字符\n

可以,但这并不构成换行。换行符是CR(0x0d)、LF(0x0a)或CRLF(0x0d0a)中的一种
\r
是字符和字符串文本中CR的Java转义。它不会出现在输出文件中。类似地,
\n
是LF在字符和字符串文本中的Java转义

您只需读取文件,而不必使用
readLine()
,直到遇到“\r”、“\n”或“\r\n”,将它们作为Java转义序列,而不是文字字符


但在我看来,你真正想做的是读几行,在前一行后面加上以空格开头的行,而不是把它们当作新的行。

我没有读到换行符。相反,我最终将HashMap对象写入文件。

是的。当我读入每一行时,我希望结果字符串包含\n。但是,BufferedReader将文件中的换行符视为行尾,因此它不会读取它应该读取的所有数据。您是将文件的内容视为文本还是二进制数据?正如@Chrylis指出的,您需要将内容作为二进制数据来读取,即
byte[]
数组并将其转换为
字符串
@SotiriosDelimanolis OP不一定需要将其作为二进制数据读取,但如果它确实是二进制数据,则读取为
字符串
是错误的方法。以这种方式将文本读入
字符串
仍然是明智的。@Chrylis内容是文本,但所有以字符串形式读取流的类都会将内容拆分为新行字符,因此您不能使用它们。我希望这样,输出文件中不会显示换行符。无论如何,我都在将每一行读回Java,因此如果我将该行存储在字符串中,则作为文本打印到文件中的任何\n都应解释为转义序列。我对它的理解正确吗?不,我已经回答过了。“它不会出现在输出文件中。”任何作为文件文本打印的
\n
都将成为文件中的LF。不是一个转义序列。编译器将
\n
转换为
\u000a
。它甚至在运行时都不存在,更不用说写入文件了。
dt Desc: Toggles the state of debug mode.
e Desc: Exits QuiConsole
b Desc: Goes to the specified webpages.
    Args: [<URLs>] -- the URLs of the webpages to visit, separated by spaces
c Desc: Clears the console window.
o Desc: Opens a file or folder
    Args: [<handle or path>] -- the path of the file or folder
          (<file name>) -- the name of the file to open. Omit if you are opening a folder
l Desc: Searches the web
    Args: [<query>] -- the query to search for
h Desc: Displays a list of all the current handles.
    Args: (<handle names>) -- displays information for only the specified handles.
info Desc: Displays information about QuiConsole
help Desc: Displays a list of all commands, their descriptions and their arguments, if any exist.
    Args: (<command names>) -- Displays information for the specified command only. Ignores any invalid command names.
s Desc: Shuts down, restarts, hibernates or logs off the computer.
    Args: [s,r,h,l] -- shuts down, restarts, hibernates or logs off the computer respectively.
          (<integer value>) -- the number of seconds to delay the action.
dh Desc: Deletes all handles
    Args: (<handle names>) -- deletes only the specified handles
r Desc: Runs the specified program
    Args: [<names>] -- the names of the programs to run. Separate programs by a space.
wa Desc: Displays the answer from Wolfram|Alpha
    Args: [<query>] -- the query to send to Wolfram|Alpha
ch Desc: Modifies a handle
    Args: [<handle names>] -- the handle to change
          [<new value>] -- the new value to assign to the handle
ah Desc: Adds the specified handles.
    Args: [handle] -- name of the handle.
          [value] -- value for the handle
ds Desc: Displays the state of debug mode.
dhist Desc: Deletes command history