set encoding as UTF-8 for a FileWriter
Java has extensive, highly informative documentation. Keep it bookmarked. Refer to it first, whenever you have difficulty. You'll find it's frequently helpful.
In this case, the documentation for FileWriter says:
The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
If you want to be sure your file will be written as UTF-8, replace this:
FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(mergedFile, false);
with this:
Writer fstream = null;
BufferedWriter out = null;
try {
fstream = new OutputStreamWriter(new FileOutputStream(mergedFile), StandardCharsets.UTF_8);
How to write a UTF-8 file with Java?
Instead of using FileWriter
, create a FileOutputStream
. You can then wrap this in an OutputStreamWriter
, which allows you to pass an encoding in the constructor. Then you can write your data to that inside a try-with-resources Statement:
try (OutputStreamWriter writer =
new OutputStreamWriter(new FileOutputStream(PROPERTIES_FILE), StandardCharsets.UTF_8))
// do stuff
}
Java UTF-8 Encoding with FileWriter
The write method writes a single character, so you're writing characters with the Unicode codepoint of blockID, blockX, blockY which is not what you want.
Whether your Writer is encoded as UTF-8 is not so relevant here, although it is always good to be explicit about encoding if you want your files to be portable across machines, so try new OutputStreamWriter(new FileOutputStream("res/save/" + filePath+ fileName, true), "UTF-8");
instead of creating a FileWriter directly. Creating it directly doesn't allow you to specify the encoding.
Instead do something like this:
writer.write(String.format("%d %d %d\n", blockID, blockX, blockY));
This will format your three numbers as one string on one line before sending it to the file.
Note that you shouldn't create a new Writer/BufferedWriter every time that you want to write a line. You should keep them in a class field and re-use the same writer. You also need to close the file after you are done with it, since the operating system has a limit to the number of files that you have open at the same time, and you will run out of that number quickly with your current code.
Appending to file in utf8
You forgot to add the true
paramter to the FileOutputStream
constructor:
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("file.txt", true), // true to append
StandardCharsets.UTF_8 // Set encoding
)
);
Write to a file with a specific encoding in Java
Now first the worrisome. FileWriter and FileReader are old utility classes, that use the default platform settings on that computer. Run elsewhere that code will give a different file, will not be able to read a file from another spot.
ISO-8859-15 is a single byte encoding. But java holds text in Unicode, so it
can combine all scripts. And char
is UTF-16. In general a char index will not be a byte index, but in your case it probably works. But the line break might be one \n
or two \r\n
chars/bytes - platform dependently.
Re
Personally I think UTF-8 is well established, and it is easier to use:
byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
string = new String(bytes, StandardCharsets.UTF_8);
That way all special quotes, euro, and so on will always be available.
At least specify the encoding:
Files.newBufferedWriter(file.toPath(), "ISO-8859-15");
How to force content from a file to be utf-8 in java spring?
Instead of using FileWriter
, create a FileOutputStream
. You can then wrap this in an OutputStreamWriter
, which allows you to pass an encoding in the constructor. Then you can write your data to that inside a try-with-resources Statement:
try (OutputStreamWriter writer =
new OutputStreamWriter(new FileOutputStream("your_file_name"), StandardCharsets.UTF_8))
// do stuff
}
Related Topics
How to Create an 2D Arraylist in Java
How to Get the Current Date and Time
Transactional Saves Without Calling Update Method
How to Modify JSONnode in Java
Java Httpservletrequest Get Url in Browsers Url Bar
What Is an "Internal Address" in Java
How to Pass a JSON Array as a Parameter in Url
Exception Without Stack Trace in Java
Make Hibernate Ignore Instance Variables That Are Not Mapped
How Does Java Makes Use of Multiple Cores
Sessiontimeout: Web.Xml VS Session.Maxinactiveinterval()
What Is the Equivalent of Java Static Final Fields in Kotlin
Matrix Multiplication in Apache Spark