@Jon Skeet's answer works very well.
We can remove the specification when converting from one encoding to another.
Here I converted from "UTF-8" to "SHIFT-JIS", the output of the SHIFT-JIS file without specification.
File file =new File("input.txt"); InputStreamReader ireader = new InputStreamReader(new FileInputStream(file), "UTF-8"); BufferedReader br = new BufferedReader(ireader); File file2 =new File("removeBom.txt"); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file2), "SHIFT-JIS"); PrintWriter pw = new PrintWriter(out); try { String line = br.readLine(); while(line!=null) { pw.println(line.replace("\uFEFF", "")); line=br.readLine(); } System.out.println("file copied!"); pw.close(); br.close(); } catch (IOException e) { System.out.println("error"+e); }
source share