Skip to main content
added 418 characters in body
Source Link
Jon Skeet
  • 1.5m
  • 893
  • 9.3k
  • 9.3k

It's very easy to write code to convert encodings - although I'd expect there are tools to do it anyway. Simply:

  • Create one FileInputStream to the existing file, and wrap it in an InputStreamReader with the appropriate encoding
  • Create one FileOutputStream to the new file, and wrap it in an OutputStreamWriter with the appropriate encoding
  • Loop over the reader, reading characters into a buffer and writing out the contents of that buffer (just as many characters as you read) until you've read the whole file
  • Close all resources (automatic with a try-with-resources block)

The first two steps are simpler with Files.newBufferedReader and Files.newBufferedWriter, too.

It's very easy to write code to convert encodings - although I'd expect there are tools to do it anyway. Simply:

  • Create one FileInputStream to the existing file, and wrap it in an InputStreamReader with the appropriate encoding
  • Create one FileOutputStream to the new file, and wrap it in an OutputStreamWriter with the appropriate encoding
  • Loop over the reader, reading characters into a buffer and writing out the contents of that buffer (just as many characters as you read) until you've read the whole file
  • Close all resources (automatic with a try-with-resources block)

It's very easy to write code to convert encodings - although I'd expect there are tools to do it anyway. Simply:

  • Create one FileInputStream to the existing file, and wrap it in an InputStreamReader with the appropriate encoding
  • Create one FileOutputStream to the new file, and wrap it in an OutputStreamWriter with the appropriate encoding
  • Loop over the reader, reading characters into a buffer and writing out the contents of that buffer (just as many characters as you read) until you've read the whole file
  • Close all resources (automatic with a try-with-resources block)

The first two steps are simpler with Files.newBufferedReader and Files.newBufferedWriter, too.

Source Link
Jon Skeet
  • 1.5m
  • 893
  • 9.3k
  • 9.3k

It's very easy to write code to convert encodings - although I'd expect there are tools to do it anyway. Simply:

  • Create one FileInputStream to the existing file, and wrap it in an InputStreamReader with the appropriate encoding
  • Create one FileOutputStream to the new file, and wrap it in an OutputStreamWriter with the appropriate encoding
  • Loop over the reader, reading characters into a buffer and writing out the contents of that buffer (just as many characters as you read) until you've read the whole file
  • Close all resources (automatic with a try-with-resources block)