当前位置: 首页 > news >正文

网站建设中有关数据库问题b站推出的短视频app哪个好

网站建设中有关数据库问题,b站推出的短视频app哪个好,可以做网站AB测的软件,网站源码哪个好文章目录前言1-文件、目录、路径2-在当前路径下创建一个文件3-在当前路径下创建一个文件夹(目录)3.1 测试1-路径已经存在3.2 测试2-路径不存在3.2 创建不存在的路径并新建文件3.3 删除已存在的文件并新建4-总结前言 学习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

http://www.hrbkazy.com/news/40174.html

相关文章:

  • 网站建设需要经过哪几个步骤苏州百度快照优化排名
  • 安徽省交通运输厅网站网址查询
  • 苏州seo推广公司化工网站关键词优化
  • 怎么用手机做网站指数查询
  • 网站样式用什么做的关键词营销推广
  • 公司网站开发项目百度账号客服
  • 卡盟网站建设2022年可以打开的网址
  • 常见的网站建设技术电商如何从零做起
  • 有免费制作单页的网站吗淘宝网络营销方式
  • 单页网站seo优化百度收录什么意思
  • 模板网站怎么做北京网站优化培训
  • 网站开发产品设计书系统优化软件排行榜
  • 做游戏模板下载网站有哪些怎么给公司做网站
  • 备案网站内容说明新浪体育世界杯
  • 办公用品网站建设策划书搜索引擎优化关键词选择的方法有哪些
  • ui设计师网站百度信息流账户搭建
  • 成都设计咨询集团官网seo入口
  • 逐鹿网站建设程序员培训机构哪家好
  • 专业企业网站开发公司赤峰seo
  • 旅游网站排名全球海外推广营销系统
  • axure怎么做网站首页如何制作网站二维码
  • 曲阜建设局网站怎么建立一个自己的网站
  • wordpress邮箱汉化插件下载地址简述seo的应用范围
  • 深圳网站建设服务商万创网百度广告太多
  • 宁波网站建设设计搜狗识图
  • 视觉冲击力的网站设计品牌营销与推广
  • wordpress赚钱网站百度应用商店下载
  • 电子商务网站建设的相关流程文大侠seo博客
  • 尧都网站建设百度推广开户渠道
  • 焦作网站设计多少钱企业培训机构有哪些