window 下檔案傳給linux 亂碼問題 解決方案

來源:互聯網
上載者:User

裝載cygwin可以打成tar 包。然後如果檔案有問題

Java用

iconv -f GBK -t utf-8 2010-10-30.txt –o 2010-10-30.txt

c用

iconv -f GBK -t utf-8 2010-10-30.txt > 2010-10-30.txt

 

如果檔案有多個問題 可以用遞迴實現

具體如下:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class ChangeFile4ZH {
    private static int total=0;
   
   
   
    public static void main(String args[]){
        System.out.println("請輸入聊天根資料夾:");
        Scanner sc = new Scanner(System.in);
        String filePath = sc.nextLine();
        if(filePath!=null && filePath.length()>0){
            changeFileName(filePath);
        }
    }
   
    public static void changeFileName(String filePath){
        if(total %50==0){System.out.println("total:"+total);}
        File file= new File(filePath);
        if(file.exists()){
            File[] filesList = file.listFiles();
            for (File f : filesList) {
                if(f.isFile()){
                    //重新命名檔案
                    renameFile( f.getAbsolutePath(),f.getParent() , f.getName());
                    total++;
                }else{
                    changeFileName(f.getAbsolutePath());
                }
            }
        }
    }
    public static void encode(String filePath){
        if(total %50==0){System.out.println("total:"+total);}
        File file= new File(filePath);
        if(file.exists()){
            File[] filesList = file.listFiles();
            for (File f : filesList) {
                if(f.isFile()){
                    //重編譯檔案內容
                    enConderFile(f);
                    total++;
                }else{
                    encode(f.getAbsolutePath());
                }
            }
        }
    }
    public static void delete(String filePath){
        if(total %50==0){System.out.println("total:"+total);}
        File file= new File(filePath);
        if(file.exists()){
            File[] filesList = file.listFiles();
            for (File f : filesList) {
                if(f.isFile()){
                    //刪除原來檔案
                    deleteFile(f.getAbsolutePath() ,f.getName());
                    total++;
                }else{
                    delete(f.getAbsolutePath());
                }
            }
        }
    }
   
    public static void enConderFile(File file){
        Runtime runTime=Runtime.getRuntime();
        try {
            runTime.exec("iconv -f gbk -t utf-8  "+file.getPath() +"  -o " +file.getParent()+File.separator+"bak"+file.getName());
        } catch (Exception e) {
           
            System.out.println("iconv -f gbk -t utf-8  "+file.getPath() +"  -o " +file.getParent()+File.separator+"bak"+file.getName());
        }
    }
   
    public static void renameFile(String realPath,String path,String fileName){
        if(fileName.startsWith("bak")){
            Runtime runTime=Runtime.getRuntime();
            try {
                runTime.exec(" mv "+realPath +" "+path+File.separator+ fileName.replaceFirst("bak",""));
            } catch (IOException e) {
                System.out.println(" mv "+realPath +" "+path+File.separator+ fileName.replaceFirst("bak",""));

            }
           
        }
    }
    //刪除檔案
    public static void deleteFile(String realPath,String fileName){
        String regPath = "^[//d]{4}-[//d]{2}-[//d]{2}.txt$";
        Pattern  pa=Pattern.compile(regPath);
        if(pa.matcher(fileName).find()){
            new File(realPath).delete();
        }
    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.