0

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
Globmont
  • 881
  • 2
  • 8
  • 20
  • Yes. When I read in each line, I want the resulting String to contain \n. But, the BufferedReader treats the line-feed in the file as the end of the line so it doesn't read all of the data that it is supposed to. – Globmont Dec 11 '13 at 03:59
  • 1
    Are you treating the file's content as text or as binary data? – chrylis -cautiouslyoptimistic- Dec 11 '13 at 03:59
  • As @Chrylis points out, you will need to read the contents as binary data, ie. a `byte[]` array and convert it to a `String`. – Sotirios Delimanolis Dec 11 '13 at 04:00
  • @SotiriosDelimanolis The OP won't necessarily *need* to read it as binary data, but if it really is binary, then reading into a `String` is the wrong approach. Reading text in this fashion into a `String` could still be sensible. – chrylis -cautiouslyoptimistic- Dec 11 '13 at 04:01
  • @Chrylis The content is text, but all the classes that read Streams as Strings split the content on new line characters, so you can't use them. – Sotirios Delimanolis Dec 11 '13 at 04:02
  • Also, @Globmont, you're unclear in your phrasing. You say you want "to read to the end of the line, preserving the line breaks." You need to specify what you mean by "line", because a "line" is by definition something that ends at a line break. – chrylis -cautiouslyoptimistic- Dec 11 '13 at 04:02
  • @chrylis as of now, I'm treating the data as text. Will reading the lines in as binary data ignore the line-feed and read in each line as they display in the .txt file when opened in notepad? Would this involve me writing the file in binary as well? Or just reading it? Sorry, I'm not very proficient in File I/O. – Globmont Dec 11 '13 at 04:03
  • @SotiriosDelimanolis The JRE classes do, but I think there are some Commons ones that might be helpful. (Been a while since I've poked around there and never needed the functionality myself, so I'm not certain on the point.) – chrylis -cautiouslyoptimistic- Dec 11 '13 at 04:04
  • @chrylis by line-feed I mean a character that has been written to file as \n and by end-of-line I mean that when I open the file in a text editor like notepad, the visual end of the line i.e. there is no more text to the right when word-wrap is off. – Globmont Dec 11 '13 at 04:04
  • @Globmont In Real Operating Systems (and as far as I know, even Java on Windows), there's no inherent difference between text and binary files. You need to specify what "line feed" you want to "preserve", and what sort of input in the file you want to treat as a line break. Right now, you keep saying you're wanting to both split lines somehow and ignore line-break characters. – chrylis -cautiouslyoptimistic- Dec 11 '13 at 04:05
  • @Neha Nonsense. That will write a backslash followed by an 'n'. Not a newline. – user207421 Dec 11 '13 at 04:09
  • I've edited the original post to try and make it a little clearer what I'm looking for. – Globmont Dec 11 '13 at 04:10
  • Sorry for wrong comments ... i misunderstood to save \n as \n char in string. – Neha Dec 11 '13 at 04:40

2 Answers2

0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I would like it to be so that the line-break didn't display in the output file. I'm reading each line back into Java anyways, so if I stored the line in a String, any \n I printed as a literal to the file should be interpreted as an escape sequence. Am I correct in my understanding of how this works? – Globmont Dec 11 '13 at 04:15
  • No. I already answered that. "It doesn't occur in output files." Any `\n` you print as a literal to the file will become an LF in the file. Not an escape sequence. The `\n` is turned into `\u000a` *by the compiler.* It isn't even present at runtime, let alone written to the file. – user207421 Dec 11 '13 at 04:19
0

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

Globmont
  • 881
  • 2
  • 8
  • 20