向指定文件夹创建文件,并且写入信息

java 复制代码
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\mytemp";
        File file = new File(filePath);
        String filePath2 = "E:\\mytemp\\hello.txt";
        File file1 = new File(filePath2);
        if (!(file.exists() && file.isDirectory() && file1.exists())) {
            System.out.println("目录不存在,正在创建...");
            file.mkdir();
        } else {
            System.out.println("该文件已经存在,就不要再重复创建了");
        }
        //以下代码正在写入文本内容
        {
            System.out.println("文件、目录已存在,正在输入文本~");
            String context = "hehdhdfh~";
            BufferedWriter bw = new BufferedWriter(new FileWriter(filePath2));
            bw.write(context);
            bw.close();
        }
    }
java 复制代码
public class Homework01 {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\mytemp";
        File file = new File(filePath);
        if(!file.exists()){
            if(file.mkdir()){
                System.out.println("目录创建成功");
            }else {
                System.out.println("创建目录失败");
            }
        }
        String filePath2 = filePath+"\\hello.txt";
        file = new File(filePath2);
        if(!file.exists()){
            //创建文件
            if(file.createNewFile()){
                System.out.println(filePath2+"文件创建成功~");
            }else{
                System.out.println(filePath2+"文件创建失败~");
            }
        }else{
            System.out.println(filePath2+"文件已经存在,无需继续创建~");
        }
        //以下代码正在写入文本内容
        {
            System.out.println("正在输入文本~");
            String context = "hello,worldl!~";
            BufferedWriter bw = new BufferedWriter(new FileWriter(filePath2));
            bw.write(context);
            System.out.println("文本输入成功!");
            bw.close();
        }
    }
}
相关推荐
q***062915 分钟前
PHP进阶-在Ubuntu上搭建LAMP环境教程
开发语言·ubuntu·php
u***420724 分钟前
Ubuntu介绍、与centos的区别、基于VMware安装Ubuntu Server 22.04、配置远程连接、安装jdk+Tomcat
java·ubuntu·centos
charlie1145141911 小时前
从 0 开始:在 WSL + VSCode 上利用 Maven 构建 Java Spring Boot 工程
java·笔记·vscode·后端·学习·maven·springboot
笑醉踏歌行1 小时前
NVM 在安装老版本 Node环境时,无法安装 NPM的问题
前端·npm·node.js
YUJIANYUE1 小时前
Gemini一次成型龙跟随鼠标html5+canvas特效
前端·计算机外设·html5
tuokuac1 小时前
Maven中的属性占位符的用法
java·maven
abiao19811 小时前
npm WARN ERESOLVE overriding peer dependency
前端·npm·node.js
芒克芒克1 小时前
Maven 项目管理从入门到进阶:基础与高级实战全解析
java·maven
TechExplorer3651 小时前
禁用 npm 更新检查
前端·npm·node.js
郝学胜-神的一滴3 小时前
Qt的QSlider控件详解:从API到样式美化
开发语言·c++·qt·程序人生