I'm using something like this in a java application to write to a file:
BufferedWriter out = new BufferedWriter(new FileWriter(out1, true)); //where out1 is a File.
When I run it from netBeans the output is good. When I try to run it from the windows command line (the intended use; using the jar) the accented characters go crazy. I think that is have something to do with the chars encoding.
e.g.
(the output file is a HTML one);
I want to write this:
"<p>Inclinação(1):</p>"
Using Win command line, appears this:
<p>Inclina褯(1):</p>
Use OutputStreamWriter with FileOutputStream so you can explicitly specify the Charset.
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out1, true), "UTF-8"));
I believe that you need to specify an encoding, unfortunately FileWriter does not provide any ways to set it, though there are other options such as:
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(out1, true),"UTF-8"));
I resolve this problem using the parameter 8859_1, you can learn more about here
http://www.cafeconleche.org/books/xmljava/chapters/ch03s03.html.
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(baos, "8859_1"));
Related
I have code that creates a PrintWriter and prints Unicode symbols
out = new PrintWriter(new FileWriter("test.txt"));
out.print("\u2588");
out.close();
It works perfectly while saved with UTF-8 encoding inside eclipse, but when I export it to a jar it just prints off question marks. How would I tell it to use UTF-8 when printing off strings?
Eclipse may be helping you to create UTF-8 encoded file but it is better to use the right encoding in your code as well.
FileWriter does not take any parameter for encoding. You can use OutputStreamWriter as it accepts the encoding also. You may change your PrintWriter initialization to:
PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("test.txt"),"UTF-8")));
Instead of FileWriter, create a FileOutputStream. then wrap this in an OutputStreamWriter, It allows you to pass an encoding in the constructor.
FileOutputStream fos = new FileOutputStream("test.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8")
PrintWriter out = new PrintWriter(osw);
I have got a file in Windows-1250.
I would like to print this file line by line but in Eclipse console I cannot see diacritic signs.
I was trying to make changes in Common tab in run configuration but it gives no results.
I use
BufferedReader reader = new BufferedReader(new FileReader(fileName));
Thank you in advance
Use InputStreamReader or anything that allows specifying the charset:
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "Windows-1250"));
may be try to set encoding like this:
PrintStream out = new PrintStream(System.out, true, "Windows-1250");
out.println(message);
may be this helps.
I haven't programmed in java for a while but maybe this class does what you need?
It allows to set charset
The doc of the class you use tells you how to use it.
I have an output file for a program I have written. It is written by a FileWriter and BufferedWriter.
FileWriter errout = new FileWriter(new File("_ErrorList.txt"));
BufferedWriter out = new BufferedWriter(errout);
Later I write to the file using lines similar to.
out.write("Product id:" + idin + " did not fetch any pictures.\n ");
When I simpily run the program in Eclipse, the output file is formatted correctly, with each message being written on a new line. However when I export to a .jar file, it no longer works and puts every message on a single line, as if the "\n" was not working.
Am I using the FileWriter/BufferedWriter incorrectly, or does it not work in a .jar file?
You should not use '\n' directly. Either use out.newLine() to introduce a line break, or wrap the BufferedWriter into a PrintWriter, and use out.println().
This has nothing to do with the .jar file, anyway. More likely is Eclipse being clever and showing you line breaks, while the operating system does not.
One, check that the line separator is valid. Use System.getProperty("line.separator") as provided by #Andrew Thompson.
Another option if you're doing a lot of this writing new lines is to wrap your BufferedWriter in a PrintWriter.
FileWriter errout = new FileWriter(new File("_ErrorList.txt"));
BufferedWriter out = new BufferedWriter(errout);
PrintWriter printWriter = new PrintWriter(out);
printWriter.println("Product id:" + idin + " did not fetch any pictures.");
I have a slight delema with learning FileWriter... The ultimate goal is writing a program that will "spawn" a .bat file that will be executed by the batch code that launched the .jar. The problem is, I have no clue how to make sure that every FileWriter.write(); will print on a new line... Any ideas??
To create new lines, simply append a newline character to the end of the string:
FileWriter writer = ...
writer.write("The line\n");
Also, the PrintWriter class provides methods which automatically append newline characters for you (edit: it will also automatically use the correct newline string for your OS):
PrintWriter writer = ...
writer.println("The line");
Use a BufferedWriter and use writer.newLine() after every write-operation that represents one line.
Or, use a PrintWriter and writer.println().
If you are using BufferedWriter then you can use an inbuilt method :
BufferedWriter writer = Files.newBufferedWriter(output, charset);
writer.newLine();
I got a folder called DIR and a subfolder called SUB.
the java file i am running is placed in the DIR folder, and now i want to save my small string in a .txt file in the SUB folder.
Could you help me?
PrintWriter out = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("SUB/myfile.txt")));
out.println("simpleString);
out.close();
This simple task seems very complicated in Java. The reason is, that you can configure everything on the way from the String to the file. You could e.g. specifiy the encoding of the output file, which defaults to platform encoding (e.g. "win1252" for Windows in most parts of the western hemisphere). To write UTF-8 files use:
PrintWriter out = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("SUB/myfile.txt"),"UTF-8"));
out.println("simpleString);
out.close();
The PrintWriter also has a "flush immediately" option, which is nice for logfiles you want to monitor in the background.
By chaining the IO constructors, you can also increase performance when using a BufferedWriter, because this flushes only to the disk, when a minimum size is reached, or when the stream is closed.
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("SUB/myfile.txt"),"UTF-8")));
out.println("simpleString);
out.close();
By having to create a FileOutputStream you specify the output should go to the disk, but you can also use any other streamable location, like e.g. an array:
byte[] bytes = new byte[4096];
OutputStream bout = new ByteArrayOutputStream(bout);
PrintWriter out = new PrintWriter(
new OutputStreamWriter(bout,"UTF-8"));
out.println("simpleString);
out.close();
You will love Javas versatility!
Apache org.apache.commons.io.FileUtils class is implemented to reduce amount of boilerplate code..
FileUtils.writeStringToFile(new File("SUB/textFile.txt"),stringData,"UTF-8");