java创建中文文件名文件

 java在linux环境创建中文文件名文件,文件UTF-8编码,代码如下:

    private static void createCnFile() {
        try {
            String fileName = "中文名文件.txt";
            String newName = new String(fileName.getBytes(), "UTF-8");
            File f1 = new File(newName);
            if (!f1.exists()) {
                f1.createNewFile();
            }
        }catch (Exception e) {

        }
    }

 

 windows中创建中文文件名文件,文件GBK编码,代码如下:

    private static void createCnFile() {
        try {
            String fileName = "中文名文件11.txt";
            File f1 = new File(fileName);
            if (!f1.exists()) {
                f1.createNewFile();
            }
        }catch (Exception e) {

        }
    }

 

posted @ 2022-09-13 14:18  SaraMorning  阅读(575)  评论(0编辑  收藏  举报