有编码方式的文件创建

来源:互联网 发布:86-87赛季乔丹每场数据 编辑:程序博客网 时间:2024/04/29 04:07
     /**
         * 有编码方式的文件创建
         * @param filePathAndName 文本文件完整绝对路径及文件名
         * @param fileContent 文本文件内容
         * @param encoding 编码方式 例如 GBK 或者 UTF-8
         * @return
         */
        public void createFile(String filePathAndName, String fileContent, String encoding) {
        
            try {
                String filePath = filePathAndName;
                filePath = filePath.toString();
                File myFilePath = new File(filePath);
                if (!myFilePath.exists()) {
                    myFilePath.createNewFile();
                }
                PrintWriter myFile = new PrintWriter(myFilePath,encoding);
                String strContent = fileContent;
                myFile.println(strContent);
                myFile.close();
            }
            catch (Exception e) {
                message = "创建文件操作出错";
            }
        }
原创粉丝点击