Skip to content

Instantly share code, notes, and snippets.

@eyasuyuki
Created January 30, 2023 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyasuyuki/42a0d79a82ba1a78413df737490766e7 to your computer and use it in GitHub Desktop.
Save eyasuyuki/42a0d79a82ba1a78413df737490766e7 to your computer and use it in GitHub Desktop.
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