Created
February 28, 2016 15:28
-
-
Save AnnaBoro/5821b71203959df48bb4 to your computer and use it in GitHub Desktop.
changeEncoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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