import java.io.IOException; 02 import java.nio.file.FileSystems; 03 import java.nio.file.FileVisitResult; 04 import java.nio.file.Files; 05 import java.nio.file.Path; 06 import java.nio.file.PathMatcher; 07 import java.nio.file.Paths; 08 import java.nio.file.SimpleFileVisitor; 09 import java.nio.file.attribute.BasicFileAttributes; 10 11 public class ChangeGBK2UTF { 12 13 /** 14 * @param args 15 * @throws IOException 16 */ 17 public static void main(String[] args) throws IOException { 18 Path path = Paths.get("C:/src"); 19 String pattern = "**/*.java"; 20 final PathMatcher matcher = FileSystems.getDefault().getPathMatcher( 21 "glob:" + pattern); 22 SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { 23 @Override 24 public FileVisitResult visitFile(Path file, 25 BasicFileAttributes attrs) { 26 if (matcher.matches(file)) { 27 System.out.println("to process " + file); 28 try { 29 byte[] bytes = Files.readAllBytes(file); 30 String gbkContent = new String(bytes, "gb18030"); 31 32 Files.write(file, gbkContent.getBytes("UTF-8")); 33 } catch (IOException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 } 38 return FileVisitResult.CONTINUE; 39 } 40 }; 41 Files.walkFileTree(path, visitor); 42 } 43 }