Hello forum, vaironl here.
I have a project which will be due at the end of the year. I'm taking in the user input from 1- 40 ingredients with, name, amount, an unit. Additionally I'm taking a description which is a paragraph, and I store these values in a text file. The problem is that after a certain amount of characters, everything is being writting to the next line. I would like everything to be on one line, and I'm really confuse on how to do this because the paragraphs have a large amount of whites paces(Enter).

Does anyone recommend another way to write this? Perhaps an XML document, or word, but what of my user does not have these programs?

Thanks

Recommended Answers

The problem is that after a certain amount of characters, everything is being writting to the next line

How do you define a line. Normal definition is a line contains all the characters up to the line end character ('\n'). A very large file can contain no line end chars …

Jump to Post

All 4 Replies

The problem is that after a certain amount of characters, everything is being writting to the next line

How do you define a line. Normal definition is a line contains all the characters up to the line end character ('\n'). A very large file can contain no line end chars and be all on one line.
If you do not add a line end char to the output, the line can be very long.

Are you certain that the file is being broken into long lines, or is it possible that whatever viewer/editor you are using to look at the file is displaying it like that?
If you read the file back into your Java program one line at a time, are the descriptions split?

Is the file supposed to be readable, or just store the data for efficient processing? In the latter case, what difference does it make if you have new lines or how the file looks at all?

It sound to me like you want to make a file that efficiently stores a lot of data for fast processing. Have you looked into random access files?

http://docs.oracle.com/javase/tutorial/essential/io/rafs.html

Reading your post again, I now wonder if you are asking the right question. You have one or more recipes with multiple ingredients and a multi-paragraph description, yes?
The natural way to d o this in Java is to create a very simple Recipe class, with the ingredients and description as instance variables. You can then write or read a whole Recipe to/from an ObjectOutputStream/ObjectInputStream with a single method call. Alternatively, to use a text file, use the java.beans.XMLEncoder class to write your Recipe to an XML encoded text file, and java.beans.XMLDecoder to read the file back into a Recipe object, also with a single line of code.
If you follow either of these routes you can also create an ArrayList<Recipe> to hold a number of Recipes, and write/read the whole ArrayList with a single line of code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.