Writing to File in Java: output displays differently in NotePad vs. Word/WordPad/NotePad++
Below is the Java code for reading an existing text file called Sales.txt and outputting a bar chart to a text file called storeReport.txt
Each asterisk represents $100 in sales.开发者_开发知识库import java.io.*;
import java.util.*;
public class BarChart
{ public static void main(String[] args) throws FileNotFoundException, IOException
{
int[] s = new int[5];
int[] b = new int[5];
FileWriter fstream = new FileWriter("storeReport.txt");
BufferedWriter out = new BufferedWriter(fstream);
Scanner scanner = new Scanner(new File("Sales.txt"));
int i = 0;
while(scanner.hasNext())
s[i++] = scanner.nextInt();
System.out.println("SALES BAR CHART\n");
out.write("SALES BAR CHART\n");
for (i=0; i<s.length; i++)
{
b[i] = s[i]/100;
out.write("store "+ (i+1) +" : ");
System.out.print("store "+ (i+1) +" : ");
for(int j = 1; j <= b[i]; j++) {
System.out.print("*");
out.write("*"); }
out.write("\n");
System.out.println();
}
out.close(); //Close the output stream
}
}When I open storeReport.txt in Word, or WordPad, or NotePad++ the output is displayed as was intended:
store 1 : ***
store 2 : ***** store 3 : ******** store 4 : ** store 5 : *********When I open the file in NotePad, the outputs are displayed all on one line. (I'm using Windows 7 and the latest jGrasp to compile and run the program.)SALES BAR CHART
The input file Sales.txt reads as such:
1000
1200 1800 800 1900Anybody have an idea on why NotePad shows the output as:
SALES BAR CHARTstore 1 : ***store 2 : ****store 3 : ********store 4 : **store 5 : **********
I suspect this is a newline issue. Try replacing your
out.write("\n");
with
out.newLine();
Notepad does not interpret a simple Newline '\n'
character as a newline. It uses the "Windows" format of Carriage Return + Newline "\r\n"
to designate a newline.
You'll basically need to do one of the following:
- Stop using Notepad
- Use some program to convert the line-end characters to CR+LF (unix2dos)
- Change your program to output
\r\n
I think most people would agree that #1 is your best choice, but I don't really know a lot about how far the \r\n
problem extends on windows platforms.
Edit: As srgerg noted in his answer, your best option is to stop specifying \n
on your own and use JAVA's out.newLine() function to produce line ends. These will be correct for whatever platform you use.
See Wikipedia for more information: http://en.wikipedia.org/wiki/Newline#Common_problems
Notepad only understands DOS/Windows line endings, which is \r\n, while Notepad++ and Wordpad handle Linux/Unix/new MacOS (\n) and older MacOS (\r) line endings as well.
You can try to get the right line end for your system:
System.getProperty ("line.separator");
Or try to use println ()
or printf/format
in combination with "%n"
, and the program will do what is appropriate on the running platform.
Installing a more elaborated editor than notepad isn't a bad idea, though.
I agree with the other answers provided above. Just a little design pointer which might enhance the overall thinking.
What I understood is you are creating some data and then trying to generate some graph out of it....
- How about using a explicit field separator and record separator.
- Use some encoding. This will provide you better control if this is run in different environments.
精彩评论