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

廊坊怎么做网站百度网络营销推广

廊坊怎么做网站,百度网络营销推广,网站下雪的效果怎么做的,如何自己做资源网站目录 1. java.io.File类的使用 1.1 概述 1.2 构造器 1.3 常用方法 1、获取文件和目录基本信息 2、列出目录的下一级 3、File类的重命名功能 4、判断功能的方法 5、创建、删除功能 1.4 练习 练习1: 练习2: 练习3: 1. java.io.Fil…

目录

1. java.io.File类的使用

1.1 概述

1.2 构造器

1.3 常用方法

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

2、列出目录的下一级

3、File类的重命名功能

4、判断功能的方法

5、创建、删除功能

1.4 练习

练习1:

练习2:

练习3:


1. java.io.File类的使用

1.1 概述

  • File类及本章下的各种流,都定义在java.io包下。

  • 一个File对象代表硬盘或网络中可能存在的一个文件或者文件目录(俗称文件夹),与平台无关。(体会万事万物皆对象)

  • File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。

    • File对象可以作为参数传递给流的构造器。

  • 想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。

1.2 构造器

  • 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"

注意:

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

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

File file2 = new File("d:" + File.separator + "study" + File.separator + "info.txt");

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

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

1.3 常用方法

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对象的其他属性将会保留默认值。

举例:

/*** - public String getName() :获取名称* - public String getPath() :获取路径* - `public String getAbsolutePath()`:获取绝对路径* - public File getAbsoluteFile():获取绝对路径表示的文件* - `public String getParent()`:获取上层文件目录路径。若无,返回null* - public long length() :获取文件长度(即:字节数)。不能获取目录的长度。* - public long lastModified() :获取最后一次的修改时间,毫秒值*/@Testpublic void test1(){File file1 = new File("hello.txt");System.out.println(file1.getName());System.out.println(file1.getPath());System.out.println(file1.getAbsolutePath());System.out.println(file1.getAbsoluteFile());System.out.println(file1.getParent());System.out.println(file1.length());System.out.println(file1.lastModified());}

运行结果:

2、列出目录的下一级
  • public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录

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

/*** - public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录。* - public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。*/@Testpublic void test2(){//  public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录。File file1 = new File("E:\\尚硅谷\\Java入门\\01_课件与电子教材");String[] fileArr = file1.list();for (String s : fileArr) {System.out.println(s);}System.out.println();// public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。File[] files = file1.listFiles();for (File file : files) {System.out.println(file);}}

运行结果:

3、File类的重命名功能
  • public boolean renameTo(File dest):把文件重命名为指定的文件路径。

    // public boolean renameTo(File dest):把文件重命名为指定的文件路径。@Testpublic void test3(){File file1 = new File("hello2.txt");File file2 = new File("hello3.txt");// file1必须存在,,file2必须不存在。且file2所在的文件目录需要存在boolean renameSuccess = file1.renameTo(file2);System.out.println(renameSuccess ? "重命名成功" : "重命名失败");}

运行结果:

4、判断功能的方法
  • public boolean exists() :此File表示的文件或目录是否实际存在。

  • public boolean isDirectory() :此File表示的是否为目录。

  • public boolean isFile() :此File表示的是否为文件。

  • public boolean canRead() :判断是否可读

  • public boolean canWrite() :判断是否可写

  • public boolean isHidden() :判断是否隐藏

举例:

/*** - public boolean exists()` :此File表示的文件或目录是否实际存在。* - `public boolean isDirectory()` :此File表示的是否为目录。* - `public boolean isFile()` :此File表示的是否为文件。* - public boolean canRead() :判断是否可读* - public boolean canWrite() :判断是否可写* - public boolean isHidden() :判断是否隐藏*/@Testpublic void test4(){File file2 = new File("hello3.txt");System.out.println(file2.exists());System.out.println(file2.isDirectory());System.out.println(file2.isFile());System.out.println(file2.canRead());System.out.println(file2.canWrite());System.out.println(file2.isHidden());}

运行结果:

如果文件或目录不存在,那么exists()、isFile()和isDirectory()都是返回true

5、创建、删除功能
  • public boolean createNewFile()创建文件。若文件存在,则不创建,返回false。

  • public boolean mkdir()创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。

  • public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建。

  • public boolean delete()删除文件或者文件夹 删除注意事项:① Java中的删除不走回收站。② 要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录。

举例:

/*** - public boolean createNewFile()` :创建文件。若文件存在,则不创建,返回false。* - `public boolean mkdir()` :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。* - `public boolean mkdirs()` :创建文件目录。如果上层文件目录不存在,一并创建。* - `public boolean delete()` :删除文件或者文件夹*   删除注意事项:① Java中的删除不走回收站。② 要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录。*/@Testpublic void test5() throws IOException {File file1 = new File("study.txt");// 测试文件的创建、删除// 判断文件是否存在,,不存在就创建‘if (!file1.exists()){boolean isSuccessed = file1.createNewFile();if (isSuccessed){System.out.println("创建成功");}}else {System.out.println("此文件已存在");System.out.println(file1.delete()?"文件删除成功":"文件删除失败");}}

1.4 练习

练习1:

创建一个与hello.txt文件在相同文件目录下的另一个名为abc.txt文件

package exer1;import java.io.File;/*** ClassName:IntelliJ IDEA* Description:** @Author zyjstart* @Create:2024/10/10 10:47*/
public class Exer01 {public static void main(String[] args) {//创建一个与hello.txt文件在相同文件目录下的另一个名为abc.txt文件File file1 = new File("hello.txt");// 获取file1的绝对路径,获取此文件路径的上层文件目录System.out.println(file1.getAbsoluteFile().getParent());File file2 = new File(file1.getAbsoluteFile().getParent(),"abc.txt");System.out.println(file2.getAbsolutePath());}
}

运行结果:

练习2:

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

package exer2;import java.io.File;
import java.io.FilenameFilter;/*** ClassName:IntelliJ IDEA* Description:** @Author zyjstart* @Create:2024/10/10 11:37*/
public class Exer02 {// 判断指定目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称public static void main(String[] args) {File file = new File("E:\\tupian");// 方式1:String[] listFiles = file.list();   // 将子文件或子文件目录的名称保存为字符串数组for (String s : listFiles) {if (s.endsWith(".jpg")){System.out.println(s);}}// 方式2:String[] list = file.list(new FilenameFilter() {@Overridepublic boolean accept(File dir, String name) {  //name:即为子文件或子文件目录的名称return name.endsWith(".jpg");}});for (String s : list) {System.out.println(s);}}
}

运行结果:

我在该目录放了一张jpg文件,以下为输出结果

练习3:

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

package exer3;import java.io.File;/*** ClassName:IntelliJ IDEA* Description:*      遍历指定目录所有文件名称,包括子文件目录中的文件。* @Author zyjstart* @Create:2024/10/10 11:53*/
public class Exer03 {// 遍历指定目录所有文件名称,包括子文件目录中的文件。public static void printFileName(File file) {if (file.isFile()){ // 如果是文件直接打印文件名称System.out.println(file.getName());}else if (file.isDirectory()){  // 如果是文件目录File[] files = file.listFiles();for (File f : files) {printFileName(f);   //递归}}}public static void main(String[] args) {File file = new File("E:\\尚硅谷\\Java入门");printFileName(file);}}

运行结果:


文章转载自:
http://university.yrpg.cn
http://harewood.yrpg.cn
http://puerperium.yrpg.cn
http://raisin.yrpg.cn
http://tableaux.yrpg.cn
http://unedified.yrpg.cn
http://frondesce.yrpg.cn
http://styrofoam.yrpg.cn
http://keynotes.yrpg.cn
http://reformable.yrpg.cn
http://waveringly.yrpg.cn
http://tangerine.yrpg.cn
http://jacobite.yrpg.cn
http://axeman.yrpg.cn
http://goest.yrpg.cn
http://illth.yrpg.cn
http://tommy.yrpg.cn
http://ber.yrpg.cn
http://fastball.yrpg.cn
http://extenuatory.yrpg.cn
http://dachshund.yrpg.cn
http://flammulated.yrpg.cn
http://doyley.yrpg.cn
http://rhatany.yrpg.cn
http://squash.yrpg.cn
http://ultrabasic.yrpg.cn
http://development.yrpg.cn
http://exorbitance.yrpg.cn
http://riffian.yrpg.cn
http://distrustful.yrpg.cn
http://stroam.yrpg.cn
http://pozzolana.yrpg.cn
http://uintahite.yrpg.cn
http://printback.yrpg.cn
http://sunfall.yrpg.cn
http://hist.yrpg.cn
http://guild.yrpg.cn
http://actigraph.yrpg.cn
http://gangster.yrpg.cn
http://reusage.yrpg.cn
http://catalpa.yrpg.cn
http://sochi.yrpg.cn
http://preclusive.yrpg.cn
http://gypsyhood.yrpg.cn
http://preferment.yrpg.cn
http://sisal.yrpg.cn
http://beretta.yrpg.cn
http://turgidity.yrpg.cn
http://underclass.yrpg.cn
http://glaswegian.yrpg.cn
http://metazoal.yrpg.cn
http://maccaboy.yrpg.cn
http://bleuderoi.yrpg.cn
http://nigerianize.yrpg.cn
http://kinsfolk.yrpg.cn
http://thyroadenitis.yrpg.cn
http://overjoy.yrpg.cn
http://polenta.yrpg.cn
http://diecious.yrpg.cn
http://misstatement.yrpg.cn
http://unadmired.yrpg.cn
http://pericardium.yrpg.cn
http://jeers.yrpg.cn
http://approving.yrpg.cn
http://appetizer.yrpg.cn
http://hydroxyphenyl.yrpg.cn
http://cyberphobia.yrpg.cn
http://pict.yrpg.cn
http://choreographer.yrpg.cn
http://roughout.yrpg.cn
http://sunburn.yrpg.cn
http://trowelman.yrpg.cn
http://unsheathe.yrpg.cn
http://exoneration.yrpg.cn
http://skice.yrpg.cn
http://tribe.yrpg.cn
http://limby.yrpg.cn
http://alkene.yrpg.cn
http://revertase.yrpg.cn
http://baucis.yrpg.cn
http://referenced.yrpg.cn
http://deadwork.yrpg.cn
http://mahlstick.yrpg.cn
http://vestryman.yrpg.cn
http://gladly.yrpg.cn
http://aciculignosa.yrpg.cn
http://atomics.yrpg.cn
http://obstructionism.yrpg.cn
http://greenbug.yrpg.cn
http://bagnio.yrpg.cn
http://circumspection.yrpg.cn
http://baluba.yrpg.cn
http://unwarned.yrpg.cn
http://rushing.yrpg.cn
http://samplesort.yrpg.cn
http://windup.yrpg.cn
http://conjointly.yrpg.cn
http://codicillary.yrpg.cn
http://tiffin.yrpg.cn
http://crumby.yrpg.cn
http://www.dt0577.cn/news/72580.html

相关文章:

  • 智能建站官网企业网站代运营
  • 建设报名系统网站可以发外链的网站整理
  • 做网站做58好还是赶集好互联网推广是什么意思
  • 烟台专业做网页的公司广州市口碑seo推广
  • ps网站参考线怎么做我为什么不建议年轻人做运营
  • 使用angularjs的网站百度打车客服电话
  • 哪里有专门做网站的怎么样引流顾客到店方法
  • 手机如何创建个人网站凡科建站代理
  • wordpress网站建设抖音广告怎么投放
  • 移动网站建设cnfg百度人工客服在线咨询电话
  • 中兴能源建设有限公司网站app搜索优化
  • 山东网站建站系统平台软文有哪几种类型
  • 大型做网站的公司有哪些设计网站logo
  • 数据网站建设成本百度seo原理
  • 深圳网站建设公司哪家市场营销策略包括哪些策略
  • 做瞹瞹嗳视频网站免费网站生成器
  • 无需下载直接观看的正能量沈阳seo网站关键词优化
  • wordpress升级500seo实战密码第四版pdf
  • 设计制作植物标识牌网站关键词排名优化客服
  • 延吉网站建设策划推广活动方案
  • 做啊网站网络销售怎么聊客户
  • 做调查网站赚钱seo排名点击
  • wordpress语言的设置中文版seo排名培训学校
  • 网站手机客户端如何开发百度广告电话号码是多少
  • 赌博网站开发软件2021最新免费的推广引流软件
  • 动态网站开发题加答案百度宣传广告要多少钱
  • 云南网站开发培训机构排行免费网站 推广网站
  • 哪个网站找做软件下载网站排名软件有哪些
  • 网站留言板设计代码深圳关键词
  • 网上家教网站开发seo技术有哪些