Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created February 28, 2016 15:28
Show Gist options
  • Save AnnaBoro/5821b71203959df48bb4 to your computer and use it in GitHub Desktop.
Save AnnaBoro/5821b71203959df48bb4 to your computer and use it in GitHub Desktop.
changeEncoding
package lesson8.chars;
import java.io.*;
public class DemoChar {
public static void main(String[] args) {
File file = new File("/Users/anna/Documents/directorytest/testtest.txt");
String currentEncoding = "ISO_8859_1";
String neededEncoding = "UTF_16BE";
changeEncoding(file, currentEncoding, neededEncoding);
}
public static void changeEncoding(File file, String currentEncoding, String neededEncoding) {
StringBuilder builder = new StringBuilder();
String fileName = file.getName();
File fileNew = new File("newcoding" + fileName);
try (FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, currentEncoding);
BufferedReader reader = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream(fileNew);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, neededEncoding);
BufferedWriter writer = new BufferedWriter(outputStreamWriter);)
{
String str;
while ((str = reader.readLine()) != null) {
builder.append(str);
writer.write(str + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment