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

哈尔滨seo优化专注廊坊seo排名优化

哈尔滨seo优化专注,廊坊seo排名优化,郑州专业的网站建设公司排名,怎样做简单的网站文章目录 概述构造器常用方法1、获取文件和目录基本信息2、列出目录的下一级3.File类的重命名功能4、判断功能的方法5、创建、删除功能 练习 概述 File类及本章下的各种流,都定义在java.io包下。一个File对象代表硬盘或网络中可能存在的一个文件或者文件目录&#…

文章目录

  • 概述
  • 构造器
  • 常用方法
      • 1、获取文件和目录基本信息
      • 2、列出目录的下一级
      • 3.File类的重命名功能
      • 4、判断功能的方法
      • 5、创建、删除功能
  • 练习

概述

  • File类及本章下的各种流,都定义在java.io包下。
  • 一个File对象代表硬盘或网络中可能存在的一个文件或者文件目录(俗称文件夹),与平台无关。(体会万事万物皆对象)
  • File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
    • File对象可以作为参数传递给流的构造器。
  • 想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。

构造器

  • public File(String pathname) :以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
  • public File(String parent, String child) :以parent为父路径,child为子路径创建File对象。
  • public File(File parent, String child) :根据一个父File对象和子文件路径创建File对象

关于路径:

  • **绝对路径:**从盘符开始的路径,这是一个完整的路径。
  • **相对路径:**相对于项目目录的路径,这是一个便捷的路径,开发中经常使用。
    • IDEA中,main中的文件的相对路径,是相对于"当前工程"
    • IDEA中,单元测试方法中的文件的相对路径,是相对于"当前module"
import org.junit.Test;import java.io.File;
import java.io.IOException;
public class FileObjectTest {public static void main(String[] args) {// 文件路径名String pathname = "D:\\aaa.txt";File file1 = new File(pathname);// 文件路径名String pathname2 = "D:\\aaa\\bbb.txt";File file2 = new File(pathname2);// 通过父路径和子路径字符串String parent = "d:\\aaa";String child = "bbb.txt";File file3 = new File(parent, child);System.out.println(file3);File parentDir = new File("d:\\aaa");String childFile = "bbb.txt";File file4 = new File(parentDir, childFile);}@Testpublic void test01() throws IOException {File f1 = new File("d:\\dic\\javase\\HelloIO.java"); //绝对路径System.out.println("文件/目录的名称:" + f1.getName());System.out.println("文件/目录的构造路径名:" + f1.getPath());System.out.println("文件/目录的绝对路径名:" + f1.getAbsolutePath());System.out.println("文件/目录的父目录名:" + f1.getParent());}@Testpublic void test02()throws IOException{File f2 = new File("/HelloIO.java");//绝对路径,从根路径开始System.out.println("文件/目录的名称:" + f2.getName());System.out.println("文件/目录的构造路径名:" + f2.getPath());System.out.println("文件/目录的绝对路径名:" + f2.getAbsolutePath());System.out.println("文件/目录的父目录名:" + f2.getParent());}@Testpublic void test03() throws IOException {File f3 = new File("HelloIO.java");//相对路径System.out.println("user.dir =" + System.getProperty("user.dir"));System.out.println("文件/目录的名称:" + f3.getName());System.out.println("文件/目录的构造路径名:" + f3.getPath());System.out.println("文件/目录的绝对路径名:" + f3.getAbsolutePath());System.out.println("文件/目录的父目录名:" + f3.getParent());}@Testpublic void test04() throws IOException{File f5 = new File("HelloIO.java");//相对路径System.out.println("user.dir =" + System.getProperty("user.dir"));System.out.println("文件/目录的名称:" + f5.getName());System.out.println("文件/目录的构造路径名:" + f5.getPath());System.out.println("文件/目录的绝对路径名:" + f5.getAbsolutePath());System.out.println("文件/目录的父目录名:" + f5.getParent());}
}

注意:

  1. 无论该路径下是否存在文件或者目录,都不影响File对象的创建。

  2. window的路径分隔符使用“\”,而Java程序中的“\”表示转义字符,所以在Windows中表示路径,需要用“\”。或者直接使用“/”也可以,Java程序支持将“/”当成平台无关的路径分隔符。或者直接使用File.separator常量值表示。比如:

    File file2 = new File(“d:” + File.separator + “path” + File.separator + “info.txt”);

  3. 当构造路径是绝对路径时,那么getPath和getAbsolutePath结果一样

    当构造路径是相对路径时,那么getAbsolutePath的路径 = user.dir的路径 + 构造路径

常用方法

1、获取文件和目录基本信息

  • public String getName() :获取名称
  • public String getPath() :获取路径
  • public String getAbsolutePath():获取绝对路径
  • public File getAbsoluteFile():获取绝对路径表示的文件
  • public String getParent():获取上层文件目录路径。若无,返回null
  • public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
  • public long lastModified() :获取最后一次的修改时间,毫秒值

如果File对象代表的文件或目录存在,则File对象实例初始化时,就会用硬盘中对应文件或目录的属性信息(例如,时间、类型等)为File对象的属性赋值,否则除了路径和名称,File对象的其他属性将会保留默认值。

import java.io.File;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;public class FileInfoMethod {public static void main(String[] args) {File f = new File("d:/aaa/bbb.txt");System.out.println("文件构造路径:"+f.getPath());System.out.println("文件名称:"+f.getName());System.out.println("文件长度:"+f.length()+"字节");System.out.println("文件最后修改时间:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(f.lastModified()),ZoneId.of("Asia/Shanghai")));File f2 = new File("d:/aaa");System.out.println("目录构造路径:"+f2.getPath());System.out.println("目录名称:"+f2.getName());System.out.println("目录长度:"+f2.length()+"字节");System.out.println("文件最后修改时间:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(f.lastModified()),ZoneId.of("Asia/Shanghai")));}
}
输出结果:
文件构造路径:d:\aaa\bbb.java
文件名称:bbb.java
文件长度:636字节
文件最后修改时间:2022-07-23T22:01:32.065目录构造路径:d:\aaa
目录名称:aaa
目录长度:4096字节
文件最后修改时间:2022-07-23T22:01:32.065

2、列出目录的下一级

*public String[] list():返回一个String数组,表示该File目录中的所有子文件或目录。
* public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。

import org.junit.Test;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;public class DirListFiles {@Test// 显示D盘下dir 里的文件和文件夹public void test01() {File dir = new File("d:/dir");String[] subs = dir.list();for (String sub : subs) {System.out.println(sub);}}}

3.File类的重命名功能

public boolean renameTo(File dest):把文件重命名为指定的文件路径。

4、判断功能的方法

  • public boolean exists() :此File表示的文件或目录是否实际存在。
  • public boolean isDirectory() :此File表示的是否为目录。
  • public boolean isFile() :此File表示的是否为文件。
  • public boolean canRead() :判断是否可读
  • public boolean canWrite() :判断是否可写
  • public boolean isHidden() :判断是否隐藏
import java.io.File;public class FileIs {public static void main(String[] args) {File f = new File("d:\\aaa\\bbb.java");File f2 = new File("d:\\aaa");// 判断是否存在System.out.println("d:\\aaa\\bbb.java 是否存在:"+f.exists());System.out.println("d:\\aaa 是否存在:"+f2.exists());// 判断是文件还是目录System.out.println("d:\\aaa 文件?:"+f2.isFile());System.out.println("d:\\aaa 目录?:"+f2.isDirectory());}
}

5、创建、删除功能

  • public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false。
  • public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
  • public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建。
  • public boolean delete() :删除文件或者文件夹
    删除注意事项:① Java中的删除不走回收站。② 要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录。
import java.io.File;
import java.io.IOException;public class FileCreateDelete {public static void main(String[] args) throws IOException {// 文件的创建File f = new File("aaa.txt");System.out.println("aaa.txt是否存在:"+f.exists()); System.out.println("aaa.txt是否创建:"+f.createNewFile()); System.out.println("aaa.txt是否存在:"+f.exists()); // 目录的创建File f2= new File("newDir");System.out.println("newDir是否存在:"+f2.exists());System.out.println("newDir是否创建:"+f2.mkdir());System.out.println("newDir是否存在:"+f2.exists());// 创建一级目录File f3= new File("newDira\\newDirb");System.out.println("newDira\\newDirb创建:" + f3.mkdir());File f4= new File("newDir\\newDirb");System.out.println("newDir\\newDirb创建:" + f4.mkdir());// 创建多级目录File f5= new File("newDira\\newDirb");System.out.println("newDira\\newDirb创建:" + f5.mkdirs());// 文件的删除System.out.println("aaa.txt删除:" + f.delete());// 目录的删除System.out.println("newDir删除:" + f2.delete());System.out.println("newDir\\newDirb删除:" + f4.delete());}
}

API中说明:delete方法,如果此File表示目录,则目录必须为空才能删除。

练习

判断指定目录下是否有后缀名为.jpg的文件。如果有,就输出该文件名称

public void test1(){File srcFile = new File("d:\\code");String[] fileNames = srcFile.list();for(String fileName : fileNames){if(fileName.endsWith(".jpg")){System.out.println(fileName);}}}//方法2:@Testpublic void test2(){File srcFile = new File("d:\\code");File[] listFiles = srcFile.listFiles();for(File file : listFiles){if(file.getName().endsWith(".jpg")){System.out.println(file.getAbsolutePath());}}}//方法3:/** File类提供了两个文件过滤器方法* public String[] list(FilenameFilter filter)* public File[] listFiles(FileFilter filter)*/
@Testpublic void test3(){File srcFile = new File("d:\\code");File[] subFiles = srcFile.listFiles(new FilenameFilter() {@Overridepublic boolean accept(File dir, String name) {return name.endsWith(".jpg");}});for(File file : subFiles){System.out.println(file.getAbsolutePath());}}

2.遍历指定目录所有文件名称,包括子文件目录中的文件。

// 传入路径public static void printSubFile(File dir) {// 打印目录的子文件File[] subfiles = dir.listFiles();for (File f : subfiles) {if (f.isDirectory()) {// 文件目录printSubFile(f);} else {// 文件System.out.println(f.getAbsolutePath());}}}

文章转载自:
http://teched.c7630.cn
http://jowled.c7630.cn
http://dioxin.c7630.cn
http://indisposed.c7630.cn
http://ial.c7630.cn
http://spectrometry.c7630.cn
http://proserpina.c7630.cn
http://orthognathous.c7630.cn
http://sennet.c7630.cn
http://protopodite.c7630.cn
http://pomaceous.c7630.cn
http://jugoslavian.c7630.cn
http://puisne.c7630.cn
http://eidoptometry.c7630.cn
http://magcard.c7630.cn
http://millenarianism.c7630.cn
http://scrotitis.c7630.cn
http://portfolio.c7630.cn
http://poundage.c7630.cn
http://constructive.c7630.cn
http://endothecium.c7630.cn
http://pulmonate.c7630.cn
http://spacearium.c7630.cn
http://lofter.c7630.cn
http://glossina.c7630.cn
http://ringbolt.c7630.cn
http://grist.c7630.cn
http://minnie.c7630.cn
http://repower.c7630.cn
http://ankylosis.c7630.cn
http://sudra.c7630.cn
http://variously.c7630.cn
http://aeration.c7630.cn
http://oer.c7630.cn
http://loquitur.c7630.cn
http://veneration.c7630.cn
http://psychopathology.c7630.cn
http://saddlebill.c7630.cn
http://mononucleate.c7630.cn
http://dungeon.c7630.cn
http://spotty.c7630.cn
http://cogently.c7630.cn
http://dentoid.c7630.cn
http://answerable.c7630.cn
http://corm.c7630.cn
http://nagor.c7630.cn
http://wasteful.c7630.cn
http://asbestosis.c7630.cn
http://precedents.c7630.cn
http://common.c7630.cn
http://circumvallate.c7630.cn
http://vivo.c7630.cn
http://ligamentum.c7630.cn
http://artefact.c7630.cn
http://numnah.c7630.cn
http://am.c7630.cn
http://breezily.c7630.cn
http://ardently.c7630.cn
http://recomfort.c7630.cn
http://cuboid.c7630.cn
http://gloriously.c7630.cn
http://rimose.c7630.cn
http://purism.c7630.cn
http://silicicolous.c7630.cn
http://redware.c7630.cn
http://fretsaw.c7630.cn
http://bouncy.c7630.cn
http://salmanazar.c7630.cn
http://sociolinguistics.c7630.cn
http://meprobamate.c7630.cn
http://rejection.c7630.cn
http://shamble.c7630.cn
http://ashtray.c7630.cn
http://counselee.c7630.cn
http://dimm.c7630.cn
http://pentatomic.c7630.cn
http://assets.c7630.cn
http://appel.c7630.cn
http://skywalk.c7630.cn
http://geotactic.c7630.cn
http://cloacae.c7630.cn
http://subtenancy.c7630.cn
http://comfortably.c7630.cn
http://fumaric.c7630.cn
http://floodlight.c7630.cn
http://dinothere.c7630.cn
http://fraudulence.c7630.cn
http://karpinskyite.c7630.cn
http://hierarchy.c7630.cn
http://grandness.c7630.cn
http://initiation.c7630.cn
http://clench.c7630.cn
http://redargue.c7630.cn
http://dichroic.c7630.cn
http://sheerly.c7630.cn
http://inoculate.c7630.cn
http://redesignate.c7630.cn
http://bes.c7630.cn
http://nitrogenase.c7630.cn
http://laciness.c7630.cn
http://www.zhongyajixie.com/news/68228.html

相关文章:

  • 门户网站的传播特点企业网络组建方案
  • 网站图片用什么格式可以免费打开网站的软件下载
  • 公司的网站建设哪家比较好seo推广招聘
  • 耒阳市古雍网站建设店seo站外优化最主要的是什么
  • 嘉定区 网站建设友情链接的作用有哪些
  • 网站推广服务网站连锁推广网站有哪些
  • 做基础网站主机要aso优化app推广
  • 太原模板建站百度网站大全首页
  • 免费怎样搭建网站中国优秀网页设计案例
  • php智能建站系统友情链接交易网
  • 海南网站优化最近新闻热点大事件
  • 网站手机版下悬浮条怎么做唐山百度搜索排名优化
  • 亚马逊虚拟主机做网站如何搭建公司网站
  • 织梦网做企业网站需要授权吗汕头seo优化项目
  • 网站设计 图片电脑优化软件推荐
  • 手机怎么制作网站教程天津快速关键词排名
  • 做网站需要哪些知识seo关键词排名优化费用
  • wordpress 移动 seo南京seo排名优化公司
  • 女生wordpress网站适合品牌广告策划方案
  • 网站竞价词怎么做网站关键词怎么添加
  • 网站能不能一边用 一边备案宁波seo关键词培训
  • 网站建设 移动端12345浏览器网址大全
  • 做兼职什么网站比较好企业网
  • 香港国际物流公司网站怎么做搜索引擎优化论文
  • 做个网站怎样做的宁波seo推广平台
  • 吉林天宇建设集团网站网站建设黄页免费观看
  • 网站备案查询流程全网整合营销推广
  • 肇庆网站建设搭建网站平台需要多少钱
  • 申请400客服电话优化防疫政策
  • 怎样做网站二维码百度风云榜明星