java在linux上创建文件,写入内容

1:首先创建全路径,

public void mkTemporaryDirs(){
       
       String filePath = "temporaryDir/file/temp.txt";
File file = new File(filePath);
if (!file .getParentFile().exists()) {
   file .getParentFile().mkdirs()
}
if(!file .exists()) {
   file .createNewFile();
}
    }

2:把需要写入的内容与文件名一块传入

public static Boolean inputStreamToFile(String context, File file) {
    
           Boolean flag=false;
           if(!file .exists()) {
             file .createNewFile();
               }
           file.setExecutable(true);
           file.setReadable(true);
           file.setWritable(true);
        try {          
            OutputStream os = new FileOutputStream(file);
           /* int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }*/
            os.write(context.getBytes("GBK"));
            if(os!=null){
             os.close();
            }      
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
            if(os!=null){
             os.close();
            }
            return flag;
        }
        return flag;
    }

总结

1:文件名确保是全路径以及有权限执行,
2:写入的内容从数据库查询出并对查询结果进行处理,
遍历每行数据,获取到每个元素用‘|’凭接字符串并以“\br”结尾换行,导出的文件名动态生成,目录存放在数据库字典中。