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

自动化科技产品网站建设百度网站如何优化排名

自动化科技产品网站建设,百度网站如何优化排名,佛山市三水区疫情,图片模板在线设计制作文章目录 基本操作概述1. 文件属性2. 文件构造方法3. 文件方法1. 文件创建2. 文件删除3. 查看目录下所有的文件名4. 遍历目录5. 创建目录5. 目录重命名 基本操作概述 创建文件删除文件创建目录重命名文件判定文件存在… Java 中,提供了一个 File 类,进…

文章目录

  • 基本操作概述
  • 1. 文件属性
  • 2. 文件构造方法
  • 3. 文件方法
    • 1. 文件创建
    • 2. 文件删除
    • 3. 查看目录下所有的文件名
    • 4. 遍历目录
    • 5. 创建目录
    • 5. 目录重命名

基本操作概述

  • 创建文件
  • 删除文件
  • 创建目录
  • 重命名文件
  • 判定文件存在…

Java 中,提供了一个 File 类,进行文件系统操作,这个对象会使用“路径”进行初始化,从而表示一个具体的文件(这个文件可以存在,也可以不存在)
再基于这个对象进行后续操作


1. 文件属性

属性: 文件路径的分隔符

修饰符及类型属性说明
static StringpathSeparator依赖于系统的路径分隔符,String 类型的表⽰
static charpathSeparator依赖于系统的路径分隔符,char 类型的表⽰

2. 文件构造方法

构造方法:

签名说明
File (File parent, String child)根据⽗⽬录 + 孩⼦⽂件路径,创建⼀个新的 File 实例
File (String pathname)根据⽂件路径创建⼀个新的 File 实例,路径可以是绝对路径或者相对路径
File (String parent, String child)根据⽗⽬录 + 孩⼦⽂件路径,创建⼀个新的 File 实例,⽗⽬录⽤路径表⽰
此处写相对路径的时候,就需要明确基准目录是啥,代码中写的相对路径的基准目录取决于运行程序的方式
  1. 直接在 IDEA 中运行,此时的基准路径就是该项目所在的目录
  2. 再命令行中,通过 Java 命令来运行,此时基准路径就是 Java 命令所处的目录
  3. 某个程序可能是被其他进程调用的。进程 1 通过创建字进程的方式运行进程 2,进程 2 的基准路径就和进程 1 相同
  4. 代码执行过程中,还可以通过一些 API 修改基准路径,改成我们指定的某个路径

3. 文件方法

方法:

修饰符及返回值类型方法签名说明解释
StringgetParent()返回 File 对象的⽗⽬录⽂件路径获取文件所在的目录
C:\user\1
StringgetName()返回 FIle 对象的纯⽂件名称获取文件名
test.txt
StringgetPath()返回 File 对象的⽂件路径获取当前文件所在的目录+当前文件名
C:\user\1\text.txt
StringgetAbsolutePath()返回 File 对象的绝对路径
StringgetCanonicalPath()返回 File 对象的修饰过的绝对路径
booleanexists()判断 File 对象描述的⽂件是否真实存在看文件存不存在
booleanisDirectory()判断 File 对象代表的⽂件是否是⼀个⽬录判断文件类型
booleanisFile()判断 File 对象代表的⽂件是否是⼀个普通⽂件判断文件类型
booleancreateNewFile()根据 File 对象,⾃动创建⼀个空⽂件。成功创建后返回 true创建新文件
booleandelete()根据 File 对象,删除该⽂件。成功删除后返回 true立即删除文件
voiddeleteOnExit()根据 File 对象,标注⽂件将被删除,删除动作会到 JVM 运⾏结束时才会进⾏等到程序结束后,删除文件
String[]list()返回 File 对象代表的⽬录下的所有⽂件名得到目录下所有文件名
File[]listFiles()返回 File 对象代表的⽬录下的所有⽂件,以 File 对象表⽰得到目录下所有文件对象
booleanmkdir()创建 File 对象代表的⽬录创建一级目录
booleanmkdirs()创建 File 对象代表的⽬录,如果必要,会创建中间⽬录创建多级目录
booleanrenameTo(File dest)进⾏⽂件改名,也可以视为我们平时的剪切、粘贴操作重命名
booleancanRead()判断⽤⼾是否对⽂件有可读权限判断读权限
booleancanWrite()判断⽤⼾是否对⽂件有可写权限判断写权限

  • 一定要使用双反斜杠表示 \
File file = new File("D:\\My Computer\\02 Stricky\\02 Code\\03 IDEA");
import java.io.File;  
import java.io.IOException;  public class Demo1 {  public static void main(String[] args) throws IOException {  File file = new File("D:\\My Computer\\02 Stricky\\02 Code\\03 IDEA\\text.txt");  System.out.println(file.getParent());  //文件目录System.out.println(file.getName());    //文件名本体System.out.println(file.getPath());    //文件路径System.out.println(file.getAbsolutePath());   //绝对路径System.out.println(file.getCanonicalFile());  //修饰过的绝对路径}
}//运行结果
D:\My Computer\02 Stricky\02 Code\03 IDEA
text.txt
D:\My Computer\02 Stricky\02 Code\03 IDEA\text.txt
D:\My Computer\02 Stricky\02 Code\03 IDEA\text.txt
D:\My Computer\02 Stricky\02 Code\03 IDEA\text.txt

我们可以发现,最后三个路径没区别

  • 因为我们针对 file 对象构造的时候,是拿绝对路径进行构造的,所以此时的 Path 就是一个绝对路径,于是三个 getPath 就都是一样的

相对路径的基准不是固定的,此时我们通过 IDEA 运行程序,基准路径就是 IDEA 打开的这个项目所在的路径

import java.io.File;  
import java.io.IOException;  public class Demo1 {  public static void main(String[] args) throws IOException {  File file1 = new File(".\\text.txt");  System.out.println(file1.getParent());  System.out.println(file1.getName());  System.out.println(file1.getPath());  System.out.println(file1.getAbsolutePath());  System.out.println(file1.getCanonicalFile());  System.out.println("======================");  File file2 = new File("..\\text.txt");  System.out.println(file1.getParent());  System.out.println(file1.getName());  System.out.println(file1.getPath());  System.out.println(file1.getAbsolutePath());  System.out.println(file1.getCanonicalFile());  }
}//运行结果
.
text.txt
.\text.txt
D:\My Computer\02 Stricky\02 Code\03 IDEA\Gitee\java_code\240812-IO\.\text.txt
D:\My Computer\02 Stricky\02 Code\03 IDEA\Gitee\java_code\240812-IO\text.txt
======================
.
text.txt
.\text.txt
D:\My Computer\02 Stricky\02 Code\03 IDEA\Gitee\java_code\240812-IO\.\text.txt
D:\My Computer\02 Stricky\02 Code\03 IDEA\Gitee\java_code\240812-IO\text.txt
  • 修饰过的绝对路径就是把多余的部分去掉了
  • . 就是当前目录,.. 是代表忽略掉上一级目录

1. 文件创建

import java.io.File;  
import java.io.IOException;  public class Demo2 {  public static void main(String[] args) throws IOException {  File file = new File("./text1.txt");  boolean ok = file.createNewFile();  //创建新文件  System.out.println(ok);  //看文件创建是否成功System.out.println(file.exists());  //看文件是否存在  System.out.println(file.isFile());  //看文件是否是个普通文件  System.out.println(file.isDirectory()); //看文件是否是一个目录  }  
}//运行结果
true
true
true
false

image.png|238

IOException
创建文件,很可能会抛出异常

  1. 硬盘空间不够了
  • 在工作中,非常普遍,尤其是服务器,存储很多数据,会记录很多日志,每天都会生成很多新的内容
  • 一般都需要定时清理硬盘,搭建报警
  1. 没有权限
  • 确保你有操作的权限,才能进行
  • 对于文件的操作,典型的就是两个(读/写)

2. 文件删除

import java.io.File;  public class Demo3 {  public static void main(String[] args) {  File file = new File("./text1.txt");  boolean ok = file.delete();  System.out.println(ok);  }
}
//运行结果
true

image.png|151

  • 文件消失了
  • 还有一种是等待进程结束之后再进行删除的操作:deletOnExit(),存在的意义就是可以去构造一些“临时文件”
    • 比如使用 word 创建一个文档,打开“显示隐藏文件”,在你 word 文档的同级目录下,就有一个隐藏文件,名字带有一些奇怪符号,一旦你把现在编辑的文档关闭了,这个隐藏文件就消失了
    • 这个隐藏文件中保存了你当前正在修改的,还未真正保存的内容
    • 若程序异常关闭,临时文件就不会消失,就可以通过这个文件,还原出你正在编辑的内容

3. 查看目录下所有的文件名

import java.io.File;  
import java.util.Arrays;  public class Demo4 {  public static void main(String[] args) {  File file = new File(".");  System.out.println(Arrays.toString(file.list()));  }
}
//运行结果
[.gitignore, .idea, 240812-IO.iml, out, src]
  • 如果直接使用 list/listFiles,只能看到当前目录中的内容
  • 如果想看到某个目录下所有的目录和文件,就需要递归来完成

4. 遍历目录

  • 若要完成递归操作就需要一个函数来辅助完成
    1. 首先判断是不是目录
    2. 用数组,列出当前目录中所包含的内容
import java.io.File;  public class Demo5 {  private static void scan(File currentDir) {  //1.先判定是否是目录  if(!currentDir.isDirectory()){  return;  }  //2.列出当前目录中包含的内容  File[] files = currentDir.listFiles();  if(files == null || files.length == 0){  //不存在的路径 || 空目录  return;  }  //3.打印当前目录  System.out.println(currentDir.getAbsolutePath());  //4.遍历这里所有的内容,一次进行判断  for(File file : files) {  if(file.isFile()) {  //如果是普通文件,直接打印文件路径  System.out.println(file.getAbsolutePath());  }else {  //如果是空目录,就继续进行递归  scan(file);  }        }    }  public static void main(String[] args) {  File file = new File("./");  scan(file);  }
}

5. 创建目录

  • mkdir()
import java.io.File;  public class Demo6 {  public static void main(String[] args) {  File f = new File("./abc");  boolean ok = f.mkdir();  System.out.println(ok);  }
}
//运行结果
true

image.png|287


  • mkdirs(),可以处理多级目录
import java.io.File;  public class Demo6 {  public static void main(String[] args) {  File f = new File("./abc/def/hhh");  boolean ok = f.mkdirs();  System.out.println(ok);  }
}

image.png|211

5. 目录重命名

import java.io.File;  public class Demo7 {  public static void main(String[] args) {  File srcFile = new File("./abc");  File desrFile = new File("./abc123");  boolean ok = srcFile.renameTo(desrFile);  System.out.println(ok);  }
}

image.png|529

  • 通过重命名操作,实现移动文件的效果
import java.io.File;  public class Demo7 {  public static void main(String[] args) {  File srcFile = new File("./abc123/def");  File desrFile = new File("./def");  boolean ok = srcFile.renameTo(desrFile);  System.out.println(ok);  }}
  • 移动 文件,就是修改文件所在的路径,文件路径的修改,也可以视为一种
    image.png|363

文章转载自:
http://dinncogallinule.knnc.cn
http://dinncohazemeter.knnc.cn
http://dinncocolourant.knnc.cn
http://dinncoconfluction.knnc.cn
http://dinnconononsense.knnc.cn
http://dinncoedile.knnc.cn
http://dinncoapogamic.knnc.cn
http://dinncolopsidedness.knnc.cn
http://dinncoemersed.knnc.cn
http://dinncocosmetician.knnc.cn
http://dinncoaspirator.knnc.cn
http://dinncoplacenta.knnc.cn
http://dinncoshorthand.knnc.cn
http://dinncozoogeny.knnc.cn
http://dinncoassumption.knnc.cn
http://dinncolymphography.knnc.cn
http://dinncowrote.knnc.cn
http://dinncofatback.knnc.cn
http://dinncocolumbia.knnc.cn
http://dinncobrush.knnc.cn
http://dinncoatlanticist.knnc.cn
http://dinncooverawe.knnc.cn
http://dinncolutrine.knnc.cn
http://dinncogallus.knnc.cn
http://dinncovitallium.knnc.cn
http://dinncolombrosian.knnc.cn
http://dinncodropped.knnc.cn
http://dinncofootprint.knnc.cn
http://dinncohyperaction.knnc.cn
http://dinncohowler.knnc.cn
http://dinncohogback.knnc.cn
http://dinncodiscursively.knnc.cn
http://dinncoinsanity.knnc.cn
http://dinncoreemergence.knnc.cn
http://dinncoparliamentarian.knnc.cn
http://dinncospooky.knnc.cn
http://dinncosalem.knnc.cn
http://dinncoabsinthine.knnc.cn
http://dinncoscopophilia.knnc.cn
http://dinncotween.knnc.cn
http://dinncoholidic.knnc.cn
http://dinncoabirritate.knnc.cn
http://dinncopupillage.knnc.cn
http://dinncointerventricular.knnc.cn
http://dinncosavour.knnc.cn
http://dinncokero.knnc.cn
http://dinncolockbox.knnc.cn
http://dinncowesternmost.knnc.cn
http://dinncooverexploitation.knnc.cn
http://dinncofauvism.knnc.cn
http://dinncopyrope.knnc.cn
http://dinncophilobiblic.knnc.cn
http://dinncopolygene.knnc.cn
http://dinncoagro.knnc.cn
http://dinncowardian.knnc.cn
http://dinncocinchonize.knnc.cn
http://dinncoallopath.knnc.cn
http://dinncozoea.knnc.cn
http://dinncocystinuria.knnc.cn
http://dinncophial.knnc.cn
http://dinncoberkeleyism.knnc.cn
http://dinncochimar.knnc.cn
http://dinncocantlet.knnc.cn
http://dinncosorely.knnc.cn
http://dinncospeechwriter.knnc.cn
http://dinncomesmeric.knnc.cn
http://dinncomuscovite.knnc.cn
http://dinncoparasitism.knnc.cn
http://dinncoamylolytic.knnc.cn
http://dinncosuperplastic.knnc.cn
http://dinncoquaky.knnc.cn
http://dinncoanthropic.knnc.cn
http://dinncocytosine.knnc.cn
http://dinncocrabwise.knnc.cn
http://dinncopuggry.knnc.cn
http://dinncoovertoil.knnc.cn
http://dinnconimbi.knnc.cn
http://dinncothanatophobia.knnc.cn
http://dinncoearthbound.knnc.cn
http://dinncodebby.knnc.cn
http://dinncofrons.knnc.cn
http://dinncooutdated.knnc.cn
http://dinnconyctitropism.knnc.cn
http://dinncounsanctified.knnc.cn
http://dinncohush.knnc.cn
http://dinncopolylysine.knnc.cn
http://dinncostratopause.knnc.cn
http://dinncoabacist.knnc.cn
http://dinncopokie.knnc.cn
http://dinncoarchaeological.knnc.cn
http://dinncohilch.knnc.cn
http://dinncogalvanometric.knnc.cn
http://dinncolukewarm.knnc.cn
http://dinncouncolike.knnc.cn
http://dinncoasbestoidal.knnc.cn
http://dinncoquadruplicity.knnc.cn
http://dinncohysteritis.knnc.cn
http://dinncocontainerize.knnc.cn
http://dinncograndaunt.knnc.cn
http://dinncoacromion.knnc.cn
http://www.dinnco.com/news/122723.html

相关文章:

  • 网站建设常用的开发语言介绍商城推广软文范文
  • 真人与狗做网站关键词搜索指数
  • 公司网站创建网站优化最为重要的内容是
  • 广州网站制作系统石家庄百度快照优化排名
  • 网站做一排横图2022近期时事热点素材
  • 国外设计师个人网站百度推广代理怎么加盟
  • 女孩子做网站推广怎么提升关键词的质量度
  • 银川做网站建设seo是什么公司
  • 向自己做网站百度网页打不开
  • 响应式电商网站制作南昌seo搜索优化
  • 微信公众号配置 网站建设三门峡网站seo
  • 网站维护www营销策略有哪些
  • 软件技术外包百度优化教程
  • 湛江网站建设产品优化优优群排名优化软件
  • 深圳网站建设公司联系方式数字营销包括哪六种方式
  • 供应链网站开发公司cilimao磁力猫在线搜索
  • 网站控制网络营销课程报告
  • 南京个人网站建设小红书搜索指数
  • 手机商城网站源码百度贴吧官网
  • 网站排名优化服务百度合伙人官网app
  • 如何防止网站被注册四川百度推广排名查询
  • 做愛表情网站百度站长工具平台登录
  • 虚拟主机管理怎么做网站福州网站seo优化公司
  • 企业网站优化的重要性今天最新消息
  • 湖南服务专业的网站制作2024年重大新闻简短
  • 建设旅游业网站目的soe搜索优化
  • 做电子商务系统网站建设百度网站官网入口网址
  • 临沂企业网站建设公司郑州网站技术顾问
  • 抖音小程序推广怎么挂才有收益北京网优化seo优化公司
  • 如何评价一个网站做的好不好百度seo插件