当前位置: 移动技术网 > IT编程>开发语言>Java > java在linux上创建文件,写入内容

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

2020年07月27日  | 移动技术网IT编程  | 我要评论
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())

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”结尾换行,导出的文件名动态生成,目录存放在数据库字典中。

本文地址:https://blog.csdn.net/kang1011/article/details/107590016

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网