I am using Java to generate an file, but I can't get the formatting right when opened in different Viewers. The programs that give the different output are WordPad
and NotePad
.
The output of the program in the console is all well formatted. Also when I view the file in WordPad
it is also well formated. But when I open the file in NotePad
, it is all jumbled up.
It looks extremely bad in NotePad and I would hate for the end user to open it up in NotePad seeing a jumbled mess.
Is there a way to properly format text in Java so any text reader would render it like I intended it to be?
I am using String.format()
to format my strings.
Edit: Here is some of the core code I am using to write out the information.
private void retrieveOrderInfo(ArrayList<String> order) {
int row = orderTable.getRowCount();
String descr[] = new String[row];
double price[] = new double[row];
String info;
readOrderTable(descr, price);
for (int i = 0; i < row; i++) {
info = String.format(" %-45s %-20.1f ", descr[i], price[i]);
order.add(info);
}
}
private void writeString(String path, String info, boolean line) {
DataRW write = new DataRW();
write.writeFileExternal(path, info, line);
}
This is in my DataRW class.
public void writeFileExternal(String fileName, String info, boolean line) {
BufferedWriter bwriter = null;
try {
FileWriter fwriter = new FileWriter(fileName, true);
bwriter = new BufferedWriter(fwriter);
bwriter.write(info);
if (line) {
bwriter.newLine();
}
bwriter.flush();
} catch (IOException e) {
//e.printStackTrace();
//System.out.println("Could note write to " + fileName);
errorMessageNotify("writeexternalfile", fileName);
} finally {
if (bwriter != null) {
try {
bwriter.close();
} catch (IOException e) {
//Just ignore if couldnt close
}
}
}
}
So I get the information and just write it out with these method calls.
Edit: Links to images http://i1248.photobucket.com/albums/hh489/EberronBruce/NotepadDemo_zpsangaprji.jpg
http://i1248.photobucket.com/albums/hh489/EberronBruce/wordpad_zpscbu0adkb.jpg
Here are the links to the images to describe what I mean. I do not have enough reputation points to posts the images directly.
I really do appreciate all the help. I been banging my head on this for a long time. I tried using \t and adjusting the formatting to get it to look right, but then it looks off in Wordpad, MS Word, and FireFox once I get it to look right under NotePad.
I am guessing that the generated code uses UNIX newlines. Wordpad breaks at UNIX linebreaks. Notepad does not.
If you want correct breaks for your OS, use System.getProperty("line.separator")
Edit: Now that we have screenshots, I can say that your problem is with notepad. Change font to a fixed-width font such as Courier, and your data will be properly aligned.