gbk -> utf-8

來源:互聯網
上載者:User
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 } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.