6-Java中新建一个文件、目录、路径
文章目录
- 前言
- 1-文件、目录、路径
- 2-在当前路径下创建一个文件
- 3-在当前路径下创建一个文件夹(目录)
- 3.1 测试1-路径已经存在
- 3.2 测试2-路径不存在
- 3.2 创建不存在的路径并新建文件
- 3.3 删除已存在的文件并新建
- 4-总结
前言
学习Java中如何新建文件、目录、路径
1-文件、目录、路径
文件 | fileName,就如我们在电脑中看到的.txt、.java、.doc等 |
---|---|
目录 | dir,可以理解成文件夹,里面可以包含多个文件夹或文件 |
路径 | directoryPath,有绝对路径和相对路径,这个不需要多说,但需要注意的是,如果想把win11电脑上已经存在的路径用来创建File实例,需要注意加转义符 |
2-在当前路径下创建一个文件
Main.java
class MainActivity{public static void main(String[] args){System.out.println("Main thread is running...");FileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", null);}
}
FileTest1.java
import java.io.*;class FileTest1{// the path of the fileNameString directoryPath;// the name of the fileNameString fileName;FileTest1(){}FileTest1(String directoryPath, String fileName){this.directoryPath = directoryPath;this.fileName = fileName;}static void createAFileInCurrentPath(String fileName, String directoryPath){if (null == directoryPath){directoryPath = ".";}File tempFile = new File(directoryPath, fileName);try{tempFile.createNewFile();}catch (IOException e){System.out.println("文件创建异常!");}}}
上面的代码中,如果createAFileInCurrentPath方法传入的directoryPath为"."也是可以的,就表示当前路径
3-在当前路径下创建一个文件夹(目录)
3.1 测试1-路径已经存在
Main.java
import java.io.*;class MainActivity{public static void main(String[] args){System.out.println("Main thread is running...");String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1";String testFileName1 = "实习日志.java";//create a file in current pathFileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", ".");//create a file in certain pathFile testFile1 = new File(existedPath1, testFileName1);FileTest1.createAFileInCertainPath(testFile1);}
}
FileTest1.java
import java.io.*;class FileTest1{// the path of the fileNameString directoryPath;// the name of the fileNameString fileName;FileTest1(){}FileTest1(String directoryPath, String fileName){this.directoryPath = directoryPath;this.fileName = fileName;}static void createAFileInCurrentPath(String fileName, String directoryPath){if (null == directoryPath){directoryPath = ".";}File tempFile = new File(directoryPath, fileName);try{tempFile.createNewFile();}catch (IOException e){System.out.println("文件创建异常!");}}static void createAFileInCertainPath(File file){try{file.createNewFile();}catch(Exception e){System.out.println(e);}}}
测试结果:编译通过、解释运行正常,创建了新文件
3.2 测试2-路径不存在
Main.java
import java.io.*;class MainActivity{public static void main(String[] args){System.out.println("Main thread is running...");String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1";String testFileName1 = "实习日志.java";String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir1";String testFileName2 = "学习笔记.java";//create a file in current pathFileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", ".");//create a file in certain and existed pathFile testFile1 = new File(existedPath1, testFileName1);FileTest1.createAFileInCertainPath(testFile1);//create a file in certain but not existed pathFile testFile2 = new File(unExistedPath1, testFileName2);FileTest1.createAFileInCertainPath(testFile2);}
}
FileTest1.java
import java.io.*;class FileTest1{// the path of the fileNameString directoryPath;// the name of the fileNameString fileName;FileTest1(){}FileTest1(String directoryPath, String fileName){this.directoryPath = directoryPath;this.fileName = fileName;}static void createAFileInCurrentPath(String fileName, String directoryPath){if (null == directoryPath){directoryPath = ".";}File tempFile = new File(directoryPath, fileName);try{tempFile.createNewFile();}catch (IOException e){System.out.println("文件创建异常!");}}static void createAFileInCertainPath(File file){try{file.createNewFile();}catch(Exception e){System.out.println(e);}}}
测试结果如下
3.2 创建不存在的路径并新建文件
Main.java
import java.io.*;class MainActivity{public static void main(String[] args){System.out.println("Main thread is running...");String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1";String testFileName1 = "实习日志.java";String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir1";String testFileName2 = "学习笔记.java";//create a file in current pathFileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", ".");//create a file in certain and existed pathFile testFile1 = new File(existedPath1);FileTest1.createAFileInCertainPath(testFile1);//create a file in certain but not existed pathFileTest1.createAFileInCertainPath(testFileName2, unExistedPath1);}
}
FileTest1.java
import java.io.*;class FileTest1{// the path of the fileNameString directoryPath;// the name of the fileNameString fileName;FileTest1(){}FileTest1(String directoryPath, String fileName){this.directoryPath = directoryPath;this.fileName = fileName;}static void createAFileInCurrentPath(String fileName, String directoryPath){if (null == directoryPath){directoryPath = ".";}File tempFile = new File(directoryPath, fileName);try{tempFile.createNewFile();}catch (IOException e){System.out.println("文件创建异常!");}}static void createAFileInCertainPath(File file){try{if (file.exists()){file.createNewFile();}else{System.out.println("the path is not existed ! here are the information of the path:");System.out.println("Name :"+file.getName());System.out.println("AbsoluteFile :"+file.getAbsoluteFile());System.out.println("AbsolutePath :"+file.getAbsolutePath());}}catch(Exception e){System.out.println(e);}}static void createAFileInCertainPath(String fileName, String directoryPath){File tempFileName, tempDirectoryPath;if (null != directoryPath){tempDirectoryPath = new File(directoryPath);System.out.println("Is tempFileName a directory :"+tempDirectoryPath.isDirectory());tempDirectoryPath.mkdirs();}if (null != fileName){tempFileName = new File(directoryPath, fileName);System.out.println("Is tempFileName a file :"+tempFileName.isFile());try{tempFileName.createNewFile();}catch(Exception e){System.out.println("在未存在的路径下创建文件失败!");}}}}
测试结果:编译通过、解释运行,创建成功
3.3 删除已存在的文件并新建
Main.java
import java.io.*;class MainActivity{public static void main(String[] args){System.out.println("Main thread is running...");String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1";String testFileName1 = "实习日志.java";String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir2";String testFileName2 = "学习笔记.java";//create a file in current path//create a file in certain and existed pathFile testFile1 = new File(existedPath1);FileTest1.createAFileInCertainPath(testFile1);//create a file in certain but not existed pathFileTest1.createAFileInCertainPath(testFileName2, unExistedPath1);//delete a file in current pathFileTest1.deleteAFileInCurrentPath("DefaultJavaFile1.java");//delete a file in certain pathString deleteTestPath1 = "D:\\TestPath1\\TestDir1\\TestDir2\\TestDir3\\TestDir3_1\\测试.txt";FileTest1.deleteAFileInCeratainPath("D:\\TestPath1\\TestDir1\\TestDir2\\TestDir3\\TestDir3_1", "测试.txt");//delete a dir in certain pathFileTest1.deleteADirInCertainPath("D:\\TestPath1\\TestDir1\\TestDir2\\TestDir3\\TestDir3_2");}
}
FileTest1.java
import java.io.*;class FileTest1{// the path of the fileNameString directoryPath;// the name of the fileNameString fileName;FileTest1(){}FileTest1(String directoryPath, String fileName){this.directoryPath = directoryPath;this.fileName = fileName;}static void createAFileInCurrentPath(String fileName, String directoryPath){if (null == directoryPath){directoryPath = ".";}File tempFile = new File(directoryPath, fileName);try{tempFile.createNewFile();}catch (IOException e){System.out.println("文件创建异常!");}}static void createAFileInCertainPath(File file){try{if (!file.exists()){file.createNewFile();}else{}}catch(Exception e){System.out.println(e);}}static void createAFileInCertainPath(String fileName, String directoryPath){File tempFileName, tempDirectoryPath;if (null != directoryPath){tempDirectoryPath = new File(directoryPath);System.out.println("Is tempFileName a directory :"+tempDirectoryPath.isDirectory());tempDirectoryPath.mkdirs();}if (null != fileName){tempFileName = new File(directoryPath, fileName);System.out.println("Is tempFileName a file :"+tempFileName.isFile());try{tempFileName.createNewFile();}catch(Exception e){System.out.println("在未存在的路径下创建文件失败!");}}}static void deleteAFileInCurrentPath(String fileName){System.out.println("Function deleteAFileInCurrentPath is running---------");File tempFile = new File(fileName);try{if (tempFile.exists()){System.out.println(tempFile.getName()+" 文件存在");tempFile.delete();}else{System.out.println("文件不存在");}}catch(Exception e){System.out.println("删除文件失败!");}System.out.println("Function deleteAFileInCurrentPath is finished---------");}static void deleteAFileInCeratainPath(String directory, String fileName){System.out.println("Function deleteAFileInCeratainPath is running---------");File tempFile = new File(directory, fileName);try{if (tempFile.exists()){System.out.println(tempFile.getName()+" 文件存在");tempFile.delete();}else{System.out.println("文件不存在");}}catch(Exception e){System.out.println("删除文件失败!");}System.out.println("Function deleteAFileInCeratainPath is finished---------");}static void deleteADirInCertainPath(String directory){System.out.println("Function deleteADirInCertainPath is running---------");File tempFile = new File(directory);try{if (tempFile.exists()){System.out.println(tempFile.getName()+" 文件存在");tempFile.delete();}else{System.out.println("文件不存在");}}catch(Exception e){System.out.println("删除文件失败!");}System.out.println("Function deleteADirInCertainPath is finished---------");}}
4-总结
1.简要学习了Java中如何创建文件、目录
2.在调试过程中遇到了一些问题
(1)导包,本来想使用Nullable,但似乎没有相关包
(2)直接在java文件的目录下打开的Windows power Shell窗口中运行时,显示无法加载主类MainActivity,而打开的cmd窗口却可以运行
(3)如果实例化的File对象中的路径是磁盘里不存在的,则isFile、isDirectory全返回false
相关文章:

6-Java中新建一个文件、目录、路径
文章目录前言1-文件、目录、路径2-在当前路径下创建一个文件3-在当前路径下创建一个文件夹(目录)3.1 测试1-路径已经存在3.2 测试2-路径不存在3.2 创建不存在的路径并新建文件3.3 删除已存在的文件并新建4-总结前言 学习Java中如何新建文件、目录、路径…...

Bootstrap系列之Flex布局
文章目录Bootstrap中的Flexd-flex与d-inline-flex也存在响应式变化flex水平布局flex垂直布局flex水平与垂直也存在响应式变化内容排列(justify-content响应式变化也存在于这里sm,md,lg,xl)子元素对齐方式Align items&a…...

匈牙利算法与KM算法的区别
前记 在学习过程中,发现很多博客将匈牙利算法和KM算法混为一谈,当时只管用不管分析区别,所以现在来分析一下两个算法之间的区别。 匈牙利算法在二分图匹配的求解过程中共两个原则: 1.最大匹配数原则 2.先到先得原则 而KM算法求…...

You Only Need 90K Parameters to Adapt Light 论文阅读笔记
这是BMVC2022的论文,提出了一个轻量化的局部全局双支路的低光照图像质量增强网络,有监督。 思路是先用encoder f(⋅)f(\cdot)f(⋅)转到raw-RGB域,再用decoder gt(⋅)g_t(\cdot)gt(⋅)模拟ISP过程转到sRGB域。虽然文章好像没有明确指出&…...

【vue2小知识】实现axios的二次封装
🥳博 主:初映CY的前说(前端领域) 🌞个人信条:想要变成得到,中间还有做到! 🤘本文核心:在vue2中实现axios的二次封装 目录 一、平常axios的请求发送方式 二、axios的一次封装…...

走近php的数组:数组的定义与数组函数
数组是一种数据结构,它由一组元素组成,这些元素可以是相同类型或不同类型。数组是在程序运行时动态创建的,可以根据需要增加或删除元素,因此它们是非常灵活和实用的数据结构。在大多数编程语言中,数组都有一个索引&…...

Docker 应用实践-仓库篇
目前 Docker 官方维护了一个公共仓库 Docker Hub,用于查找和与团队共享容器镜像,界上最大的容器镜像存储库,拥有一系列内容源,包括容器社区开发人员、开放源代码项目和独立软件供应商(ISV)在容器中构建和分…...

python+django篮球NBA周边商城vue
目 录 第一章 绪 论 1 1.1背景及意义 1 1.2国内外研究概况 1 1.3 研究的内容 1 第二章 关键技术的研究 3 2.1 vue技术介绍 3 myproject/ <-- 高级别的文件夹 |-- myproject/ <-- Django项目文件夹 | |-- myproje…...

抽象类与接口的区别
抽象类什么是抽象类?抽象类是特殊的类,只是不能被实例化;除此以外,具有类的其他特性;重要的是抽象类可以包括抽象方法,这是普通类所不能的。抽象方法只能声明于抽象类中,且不包含任何实现&#…...

1904. 你完成的完整对局数
题目: 一款新的在线电子游戏在近期发布,在该电子游戏中,以 刻钟 为周期规划若干时长为 15 分钟 的游戏对局。这意味着,在 HH:00、HH:15、HH:30 和 HH:45 ,将会开始一个新的对局,其中 HH 用一个从 00 到 23…...

Vue3:自定义指令以及简单的后台管理权限封装
目录 前言: 自定义指令介绍: 局部的自定义指令: 全局自定义指令: 讲讲后台管理权限管理: 前言: 说起这个自定义指令的使用场景,我第一反应就是,后台管理的权限管理,要…...

剑指 Offer 12. 矩阵中的路径
摘要 剑指 Offer 12. 矩阵中的路径 一、回溯算法解析 本问题是典型的矩阵搜索问题,可使用 深度优先搜索(DFS) 剪枝解决。 深度优先搜索: 可以理解为暴力法遍历矩阵中所有字符串可能性。DFS 通过递归,先朝一个方向搜…...

springboot+jersey+tomcat实现跨域方式上传文件到服务器
前言 在服务器上,当我们启动了tomcat,就可以以 http://ip地址:8080/文件路径/文件名 的方式,进行访问到我们服务器上处于tomcat的webapps文件夹下的文件 于是为了可以往上面加文件,我们有两种方式,一种就是直接复制文…...

【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)
💌 所属专栏:【微信小程序开发教程】 😀 作 者:我是夜阑的狗🐶 🚀 个人简介:一个正在努力学技术的CV工程师,专注基础和实战分享 ,欢迎咨询! &#…...

猜数字游戏——C++
我们在有了一定的C基础了以后,简单的实现一个案例(其实只要会while循环结构就行了),我们本章内容会实现猜数字游戏,大家有什么语法疑问可以看看我写的:C快速入门_染柒_GRQ的博客-CSDN博客,该博客…...

整数对最小和
题目描述 给定两个整数数组 array1 array2。数组元素按升序排列,假设从array1 、array2中分别取出一个元素可构成一对元素,现在需要取出K个元素并对取出的所有元素求和,计算和的最小值 注意事项 两对元素如果对应于array1 array2中的两个下…...

2023-2-22 -javaagent
周三,天气晴,7度 Java Agent Java Agent也叫作java探针,可以实现动态修改java字节码,完成额外的功能。在java类编译成字节码,在jvm执行之前,它可以读取修改字节码,以来完成额外的功能。 使用…...

JavaScript BOM操作
目录 前言 window 对象 location 对象 navigator 对象 screen 对象 history 对象 前言 BOM(Browser Object Model)指的是浏览器对象模型,它是 JavaScript 和浏览器之间的接口。通过 BOM,JavaScript 可以与浏览器窗口交互&…...

【机器学习 | 强基计划】开山篇 | 机器学习介绍及其类别和概念阐述
🤵♂️ 个人主页: @计算机魔术师 👨💻 作者简介:CSDN内容合伙人,全栈领域优质创作者。 机器学习 | 强基计划系列 (一) 作者: 计算机魔术师 版本: 1.0 ( 2022.2.25) 注释:文章会不定时更新补充 文章目录 前言一、机器学习概览1.1 有监督学习和无监督学习1.1.…...

华为OD机试真题Java实现【合规数组】真题+解题思路+代码(20222023)
合规数组 题目 给定一个正整数数组 检查数组中是否存在满足规则的数组组合 规则: A = B + 2C 🔥🔥🔥🔥🔥👉👉👉👉👉👉 华为OD机试(Java)真题目录汇总 ## 输入 第一行输出数组的元素个数 接下来一行输出所有数组元素,用空格隔开 输出 如果存在满…...

BoostSearcher搜索引擎项目
BoostSearcher搜索引擎项目 1.BoostSearcher这个项目是什么? 答:一个为Boost文档建立索引的站内搜索引擎,简单的说就是一个类似于csdn站内文档搜索框。 项目展示: gitee:https://gitee.com/zxlfx/boost-search-engine-project …...

【模拟集成电路】频率综合器(Frequency Synthesizer,FS)设计
应用于无线局域网的频率综合器设计前言频率综合器简介各部分链接链接:前言 本文主要内容是对频率综合器或称为PLL 做出简单介绍,为课程设计部分章节内容,后需给出各部分的设计方案,以及测试结果。 频率综合器简介 无线收发系统中…...

实例8:机器人的空间描述和变换仿真
实例8:机器人的空间描述和变换仿真 实验目的 通过刚体与刚体的平动、转动基础知识的学习,熟悉位姿的描述通过Python编程实践,可视化学习坐标系的变换,熟悉空间变换 实验要求 通过python编程,输入一指定向量和对应的…...

网络 导航
目录内容链接网络网络参考文章:【网络】http请求 调试网络问题解决参考文章:【问题解决】网络 IP DNS解析网络问题解决参考文章:【问题解决】网络 TCP 规则 抓包网络问题解决参考文章:【问题解决】网络 Http请求 调试网络问题解决…...

Web Spider Ast-Hook 浏览器内存漫游-数据检索
文章目录一、资源下载二、通过npm安装anyproxy模块三、anyproxy的介绍以及基本使用1. anyproxy的功能介绍2. anyproxy的基本使用四、给浏览器挂代理五、实操极验demo案例总结提示:以下是本篇文章正文内容,下面案例可供参考 一、资源下载 Github&#x…...

计算机网络笔记、面试八股(二)——HTTP协议
本章目录2. HTTP协议2.1 HTTP协议简介2.2 HTTP协议的优点2.3 HTTP协议的缺点2.4 HTTP协议属于哪一层2.5 HTTP通信过程2.6 常见请求方法2.7 GET和POST的区别2.8 请求报文与响应报文2.8.1 HTTP请求报文2.8.2 HTTP响应报文2.9 响应状态码2.10 HTTP 1.0和1.1的区别2.10.1 长连接2.1…...

docker快速上手使用
文章目录一、docker概述1. 为什么需要docker2. 什么是docker3. docker和虚拟机的区别4. docker三要素二、docker安装1. 添加app源2. 安装docker社区版3. 更换国内docker镜像源三、docker基本使用方法1. 获取镜像2. 查看当前系统中的docker镜像3. 运行docker容器4. 查看当前存在…...

<c++> 类的构造函数与类的析构函数
文章目录类的构造函数什么是构造函数声明和定义构造函数如何使用构造函数默认构造函数类的析构函数什么是析构函数声明和定义析构函数小练习银行账户执行效果类的构造函数 什么是构造函数 Q:什么是类的构造函数 A:构造函数是类的一种特殊成员函数,不需…...

华为OD机试真题Java实现【玩牌高手】真题+解题思路+代码(20222023)
玩牌高手 给定一个长度为n的整型数组,表示一个选手在n轮内可选择的牌面分数。选手基于规则选牌, 请计算所有轮结束后其可以获得的最高总分数。 选择规则如下: 1、在每轮里选手可以选择获取该轮牌面,则其总分数加上该轮牌面分数,为其新的总分数。 2、选手也可不选择本轮…...

Hive Sql整体优化思路
如果遇到sql性能问题,可以先查看4040页面的sql执行信息。一个sql解析为多个stage,一个stage分为多个task。对问题Sql的某一个stage,基本的分析思路如下:所有的task都慢,检查下是否有笛卡尔积(关联字段重复值、关联字段…...