Created
January 30, 2023 04:48
-
-
Save eyasuyuki/42a0d79a82ba1a78413df737490766e7 to your computer and use it in GitHub Desktop.
This file contains 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
import java.io.*; | |
import java.nio.charset.StandardCharsets; | |
public class RemoveBOM { | |
public static void main(String[] args) { | |
File directory = new File(args[0]); | |
removeBOM(directory); | |
} | |
private static void removeBOM(File file) { | |
if (file.isDirectory()) { | |
File[] files = file.listFiles(); | |
if (files != null) { | |
for (File f : files) { | |
removeBOM(f); | |
} | |
} | |
} else if (file.getName().endsWith(".java")) { | |
try (FileInputStream fis = new FileInputStream(file); | |
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) { | |
int bom = isr.read(); | |
if (bom != '\ufeff') { | |
return; | |
} | |
try (FileOutputStream fos = new FileOutputStream(file); | |
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) { | |
osw.write(bom); | |
int c; | |
while ((c = isr.read()) != -1) { | |
osw.write(c); | |
} | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment