Java Read Line Feed Characters

1.9k views Asked by At

I'm trying to read data from a text file that was written from strings containing \n. As a result, these characters have been written as line feeds. When I'm trying to read it back,

I'm using a FileInputStream, InputStreamReader and a BufferedReader. However, when I read in each line, The reader notices the line feed and counts it as the end of the line. What I really want is to read to the end of the line, preserving the line breaks. I tried to see if I could actually write the characters \ and n when creating the file, but no matter what I tried, they wouldn't write without turning into normal line breaks.

How can I read in this file while preserving the line feeds?

Edit: For clarification, here is a sample of the data I'm reading. As is visible, each line that says Args is on its own line. This is the line break I want to ignore. I want each command (e.g. dt, e, b, c, o, etc.) to have it's own line. That would ideally look like this:

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.

I've replaced the line-feed characters I wish to ignore with a # symbol. However, when I read the file back into my program, I want it to be so that if I System.out.println(); the line, the line-breaks that have been replaced with # will display correctly.

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
2

There are 2 answers

2
user207421 On

I tried to see if I could actually write the characters \ and n

You can, but that doesn't constitute a line break. A line break is one of CR (0x0d), LF (0x0a), or CRLF(0x0d0a). \r is the Java escape for CR in character and string literals. It doesn't occur in output files. Similarly \n is the Java escape for LF in character and string literals.

You just have to read your file, not using readLine(), until you encounter '\r', '\n', or '\r\n', taking those as Java escape sequences, not as literal characters.

But it seems to me that what you really want to do is just read lines, and append lines starting with spaces to the previous line rather than treat them as a fresh line.

0
Globmont On

I didn't end up reading in the line break characters. Rather, I ended up writing my HashMap object to file.