createNewFile无法创建包含中文名路径文件

欧阳小白 1年前 ⋅ 1760 阅读
package com.example.demo.util;
 
import com.example.demo.Controller.InfoController;
 
import java.io.BufferedWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.output.FileWriterWithEncoding;
import java.io.File;
import java.io.IOException;
/**
 * Created by bgy on 2018/1/24.
 */
public class FileUtil {
    public void builFile(String jsonfile){
        String rootPath = InfoController.class.getClassLoader().getResource("")
                .toString();
        rootPath = rootPath.replaceAll("%20", " ");
        rootPath = rootPath.replaceAll("file:/", "");
        Path path = Paths.get(rootPath).getParent().getParent();
        Path mypath = Paths.get(path.toString(), "src", "main", "resources","static","json");
        String realPath=mypath.toString();
 
        String filename = "maindata.json";
        String files = realPath + "\\" + filename;
        System.out.println(files);
        FileWriterWithEncoding fw = null;
        File f = new File(files);//files注意不包含中文路径名
        try
        {
            if (!f.exists()) {
                f.createNewFile();
            }
            fw = new FileWriterWithEncoding(f, "UTF-8");
            BufferedWriter out = new BufferedWriter(fw);
            out.write(jsonfile);
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

全部评论: 0

    我有话说:

    咨询问题

    点击这里给我发消息

    淘宝咨询

    博主承接代做业务

    真·送云服务器

    咨询博主