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

西宁建一个网站公司网络营销策略包括

西宁建一个网站公司,网络营销策略包括,骗子为啥使用香港服务器,中国建筑设计咨询公司File 类是 Java 中用于处理文件和目录的基本类之一,位于 java.io 包中。它提供了多种方法来创建、删除、检查、修改文件或目录的属性,以及列出文件夹中的内容。虽然 File 类本身不提供直接的读取或写入文件内容的方法(这些操作通常由 FileInp…

File 类是 Java 中用于处理文件和目录的基本类之一,位于 java.io 包中。它提供了多种方法来创建、删除、检查、修改文件或目录的属性,以及列出文件夹中的内容。虽然 File 类本身不提供直接的读取或写入文件内容的方法(这些操作通常由 FileInputStream, FileOutputStream, BufferedReader, BufferedWriter 等类来完成),但它为文件和目录的操作提供了强大的功能。

1. File 类的构造方法

File 类的主要构造方法有:

  • File(String pathname): 根据文件或目录的路径创建 File 对象。
File file = new File("example.txt");
  • File(String parent, String child): 根据父路径和子路径创建 File 对象。
File file = new File("C:/Documents", "example.txt");
  • File(File parent, String child): 根据父 File 对象和子路径创建 File 对象。
File parentDir = new File("C:/Documents");
File file = new File(parentDir, "example.txt");

2. 文件和目录的创建与删除

  • boolean createNewFile():

如果文件不存在,则创建该文件,若文件已经存在,则不会覆盖它。返回 true 表示文件创建成功,返回 false 表示文件已经存在。

File file = new File("example.txt");
if (file.createNewFile()) {System.out.println("文件创建成功");
} else {System.out.println("文件已存在");
}
  • boolean mkdir():

创建单一目录。如果目录已经存在,则返回 false,如果创建成功则返回 true。

File dir = new File("newDirectory");
if (dir.mkdir()) {System.out.println("目录创建成功");
} else {System.out.println("目录已存在或创建失败");
}
  • boolean mkdirs():

创建多层目录,如果父目录不存在,也会一并创建。返回 true 表示成功,false 表示失败。

File dir = new File("parentDir/childDir");
if (dir.mkdirs()) {System.out.println("多层目录创建成功");
} else {System.out.println("创建失败");
}
  • boolean delete():

删除文件或目录。如果文件/目录存在并且可以删除,则返回 true,否则返回 false。需要注意,删除操作是不可恢复的。

File file = new File("example.txt");
if (file.delete()) {System.out.println("文件删除成功");
} else {System.out.println("文件删除失败");
}

3 文件和目录的属性检查

  • boolean exists():

检查文件或目录是否存在。

if (file.exists()) {System.out.println("文件或目录存在");
} else {System.out.println("文件或目录不存在");
}
  • boolean isDirectory():

检查 File 对象是否表示目录。如果是目录返回 true,否则返回 false。

if (file.isDirectory()) {System.out.println("这是一个目录");
}
  • boolean isFile():

检查 File 对象是否表示文件。如果是文件返回 true,否则返回 false。

if (file.isFile()) {System.out.println("这是一个文件");
}
  • String getName(): 获取文件或目录的名称。
String name = file.getName();
System.out.println("文件名: " + name);
  • String getPath(): 获取文件或目录的路径。
String path = file.getPath();
System.out.println("路径: " + path);
  • long length(): 获取文件的字节数。对于目录,该方法返回 0。
long size = file.length();
System.out.println("文件大小: " + size + " 字节");

4. 文件重命名与移动

boolean renameTo(File dest): 将当前文件重命名或移动到目标路径。如果成功返回 true,否则返回 false。

File newFile = new File("newExample.txt");
if (file.renameTo(newFile)) {System.out.println("文件重命名成功");
} else {System.out.println("文件重命名失败");
}

5. 文件权限操作

  • boolean canRead(): 判断是否可以读取文件。
if (file.canRead()) {System.out.println("文件可读");
}
  • boolean canWrite(): 判断是否可以写入文件。
if (file.canWrite()) {System.out.println("文件可写");
}
  • boolean canExecute(): 判断是否可以执行文件。
if (file.canExecute()) {System.out.println("文件可执行");
}

6. 文件列表和目录遍历

  • String[] list(): 返回一个字符串数组,包含当前目录中的所有文件和子目录的名称。
File dir = new File("C:/Documents");
String[] files = dir.list();
if (files != null) {for (String filename : files) {System.out.println(filename);}
}
  • File[] listFiles(): 返回一个 File 数组,包含当前目录中的所有文件和子目录。
File dir = new File("C:/Documents");
File[] files = dir.listFiles();
if (files != null) {for (File f : files) {System.out.println(f.getName());}
}

7. 常见注意事项

  • 路径分隔符:在不同的操作系统中,路径分隔符可能不同。Windows 使用反斜杠(\),而 UNIX-like 系统(如 Linux 和 macOS)使用正斜杠(/)。Java 提供了 File.separator 来获取当前操作系统的路径分隔符,避免硬编码路径分隔符。
String path = "folder" + File.separator + "example.txt";
  • 相对路径与绝对路径:File 可以使用相对路径(相对于项目根目录或当前工作目录)或绝对路径(以文件系统的根目录为起点)来表示文件或目录。

8 案例

场景
我们希望创建一个程序来处理用户上传的文件。程序会:

  • 检查文件是否存在。
  • 如果不存在,则创建一个新的文件。
  • 在文件中写入一些数据。
  • 读取文件中的数据。
  • 最后删除文件。
import java.io.*;public class FileExample {public static void main(String[] args) {// 文件路径File file = new File("example.txt");// 1. 检查文件是否存在if (file.exists()) {System.out.println("文件已存在");} else {try {// 2. 如果文件不存在,创建一个新的文件if (file.createNewFile()) {System.out.println("文件创建成功");} else {System.out.println("文件创建失败");}} catch (IOException e) {System.out.println("发生错误: " + e.getMessage());}}// 3. 向文件写入内容try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {writer.write("Hello, this is a test file.");writer.newLine();writer.write("This file was created and written by the FileExample program.");System.out.println("写入文件成功");} catch (IOException e) {System.out.println("写入文件时发生错误: " + e.getMessage());}// 4. 读取文件内容try (BufferedReader reader = new BufferedReader(new FileReader(file))) {String line;System.out.println("读取文件内容:");while ((line = reader.readLine()) != null) {System.out.println(line);}} catch (IOException e) {System.out.println("读取文件时发生错误: " + e.getMessage());}// 5. 删除文件if (file.delete()) {System.out.println("文件删除成功");} else {System.out.println("文件删除失败");}}
}

9 综合练习

练习1:创建文件夹

​ 在当前模块下的aaa文件夹中创建一个a.txt文件

代码实现:

public class Test1 {public static void main(String[] args) throws IOException {//需求:在当前模块下的aaa文件夹中创建一个a.txt文件//1.创建a.txt的父级路径File file = new File("myfile\\aaa");//2.创建父级路径//如果aaa是存在的,那么此时创建失败的。//如果aaa是不存在的,那么此时创建成功的。file.mkdirs();//3.拼接父级路径和子级路径File src = new File(file,"a.txt");boolean b = src.createNewFile();if(b){System.out.println("创建成功");}else{System.out.println("创建失败");}}
}
练习2:查找文件(不考虑子文件夹)

​ 定义一个方法找某一个文件夹中,是否有以avi结尾的电影(暂时不需要考虑子文件夹)

代码示例:

public class Test2 {public static void main(String[] args) {/*需求:定义一个方法找某一个文件夹中,是否有以avi结尾的电影。(暂时不需要考虑子文件夹)*/File file = new File("D:\\aaa\\bbb");boolean b = haveAVI(file);System.out.println(b);}/** 作用:用来找某一个文件夹中,是否有以avi结尾的电影* 形参:要查找的文件夹* 返回值:查找的结果  存在true  不存在false* */public static boolean haveAVI(File file){// D:\\aaa//1.进入aaa文件夹,而且要获取里面所有的内容File[] files = file.listFiles();//2.遍历数组获取里面的每一个元素for (File f : files) {//f:依次表示aaa文件夹里面每一个文件或者文件夹的路径if(f.isFile() && f.getName().endsWith(".avi")){return true;}}//3.如果循环结束之后还没有找到,直接返回falsereturn false;}
}

练习3:(考虑子文件夹)

​ 找到电脑中所有以avi结尾的电影。(需要考虑子文件夹)

代码示例:

public class Test3 {public static void main(String[] args) {/* 需求:找到电脑中所有以avi结尾的电影。(需要考虑子文件夹)套路:1,进入文件夹2,遍历数组3,判断4,判断*/findAVI();}public static void findAVI(){//获取本地所有的盘符File[] arr = File.listRoots();for (File f : arr) {findAVI(f);}}public static void findAVI(File src){//"C:\\//1.进入文件夹srcFile[] files = src.listFiles();//2.遍历数组,依次得到src里面每一个文件或者文件夹if(files != null){for (File file : files) {if(file.isFile()){//3,判断,如果是文件,就可以执行题目的业务逻辑String name = file.getName();if(name.endsWith(".avi")){System.out.println(file);}}else{//4,判断,如果是文件夹,就可以递归//细节:再次调用本方法的时候,参数一定要是src的次一级路径findAVI(file);}}}}
}

练习4:删除多级文件夹

需求: 如果我们要删除一个有内容的文件夹
1.先删除文件夹里面所有的内容
2.再删除自己

代码示例:

public class Test4 {public static void main(String[] args) {/*删除一个多级文件夹如果我们要删除一个有内容的文件夹1.先删除文件夹里面所有的内容2.再删除自己*/File file = new File("D:\\aaa\\src");delete(file);}/** 作用:删除src文件夹* 参数:要删除的文件夹* */public static void delete(File src){//1.先删除文件夹里面所有的内容//进入srcFile[] files = src.listFiles();//遍历for (File file : files) {//判断,如果是文件,删除if(file.isFile()){file.delete();}else {//判断,如果是文件夹,就递归delete(file);}}//2.再删除自己src.delete();}
}

练习5:统计大小

​ 需求:统计一个文件夹的总大小

代码示例:

public class Test5 {public static void main(String[] args) {/*需求:统计一个文件夹的总大小*/File file = new File("D:\\aaa\\src");long len = getLen(file);System.out.println(len);//4919189}/** 作用:*       统计一个文件夹的总大小* 参数:*       表示要统计的那个文件夹* 返回值:*       统计之后的结果** 文件夹的总大小:*       说白了,文件夹里面所有文件的大小* */public static long getLen(File src){//1.定义变量进行累加long len = 0;//2.进入src文件夹File[] files = src.listFiles();//3.遍历数组for (File file : files) {//4.判断if(file.isFile()){//我们就把当前文件的大小累加到len当中len = len + file.length();}else{//判断,如果是文件夹就递归len = len + getLen(file);}}return len;}
}

练习6:统计文件个数

需求:统计一个文件夹中每种文件的个数并打印。(考虑子文件夹)
打印格式如下:
txt:3个
doc:4个
jpg:6个

代码示例:

public class Test6 {public static void main(String[] args) throws IOException {/*需求:统计一个文件夹中每种文件的个数并打印。(考虑子文件夹)打印格式如下:txt:3个doc:4个jpg:6个*/File file = new File("D:\\aaa\\src");HashMap<String, Integer> hm = getCount(file);System.out.println(hm);}/** 作用:*       统计一个文件夹中每种文件的个数* 参数:*       要统计的那个文件夹* 返回值:*       用来统计map集合*       键:后缀名 值:次数**       a.txt*       a.a.txt*       aaa(不需要统计的)*** */public static HashMap<String,Integer> getCount(File src){//1.定义集合用来统计HashMap<String,Integer> hm = new HashMap<>();//2.进入src文件夹File[] files = src.listFiles();//3.遍历数组for (File file : files) {//4.判断,如果是文件,统计if(file.isFile()){//a.txtString name = file.getName();String[] arr = name.split("\\.");if(arr.length >= 2){String endName = arr[arr.length - 1];if(hm.containsKey(endName)){//存在int count = hm.get(endName);count++;hm.put(endName,count);}else{//不存在hm.put(endName,1);}}}else{//5.判断,如果是文件夹,递归//sonMap里面是子文件中每一种文件的个数HashMap<String, Integer> sonMap = getCount(file);//hm:  txt=1  jpg=2  doc=3//sonMap: txt=3 jpg=1//遍历sonMap把里面的值累加到hm当中Set<Map.Entry<String, Integer>> entries = sonMap.entrySet();for (Map.Entry<String, Integer> entry : entries) {String key = entry.getKey();int value = entry.getValue();if(hm.containsKey(key)){//存在int count = hm.get(key);count = count + value;hm.put(key,count);}else{//不存在hm.put(key,value);}}}}return hm;}
}

文章转载自:
http://dinncobyobu.stkw.cn
http://dinncohartlepool.stkw.cn
http://dinncobelongingness.stkw.cn
http://dinnconylex.stkw.cn
http://dinncosubgovernment.stkw.cn
http://dinncokantele.stkw.cn
http://dinncoluce.stkw.cn
http://dinncoquadriceps.stkw.cn
http://dinncourchin.stkw.cn
http://dinncometeoroid.stkw.cn
http://dinncobrassie.stkw.cn
http://dinncobungalow.stkw.cn
http://dinncoeclectic.stkw.cn
http://dinncodampproof.stkw.cn
http://dinncowot.stkw.cn
http://dinncoundose.stkw.cn
http://dinncomeistersinger.stkw.cn
http://dinncoquintain.stkw.cn
http://dinncodwale.stkw.cn
http://dinncoeparch.stkw.cn
http://dinncohypocenter.stkw.cn
http://dinncobaritone.stkw.cn
http://dinncomastercard.stkw.cn
http://dinncodelicatessen.stkw.cn
http://dinncomesentery.stkw.cn
http://dinncopanoplied.stkw.cn
http://dinncotwelvefold.stkw.cn
http://dinncopresentient.stkw.cn
http://dinncomenshevism.stkw.cn
http://dinncohandedness.stkw.cn
http://dinncopronto.stkw.cn
http://dinncocountrify.stkw.cn
http://dinncosupermundane.stkw.cn
http://dinncopsycology.stkw.cn
http://dinncounwatchful.stkw.cn
http://dinncocarping.stkw.cn
http://dinncomicrobic.stkw.cn
http://dinncoperiodontium.stkw.cn
http://dinncobonami.stkw.cn
http://dinncoindefinably.stkw.cn
http://dinncofallfish.stkw.cn
http://dinncokimzeyite.stkw.cn
http://dinncoscotograph.stkw.cn
http://dinncoelectrization.stkw.cn
http://dinncointerfoliar.stkw.cn
http://dinncounderreact.stkw.cn
http://dinncoreproof.stkw.cn
http://dinncokeratoscopy.stkw.cn
http://dinncolevitron.stkw.cn
http://dinncomegashear.stkw.cn
http://dinncoelucidation.stkw.cn
http://dinncoadfreeze.stkw.cn
http://dinncoterrapin.stkw.cn
http://dinncovaluate.stkw.cn
http://dinncocpa.stkw.cn
http://dinncospiculum.stkw.cn
http://dinncovitality.stkw.cn
http://dinncodekagram.stkw.cn
http://dinncobehaviourist.stkw.cn
http://dinncopaternity.stkw.cn
http://dinncoabsinthe.stkw.cn
http://dinncotrolly.stkw.cn
http://dinncoeupneic.stkw.cn
http://dinncoazine.stkw.cn
http://dinncoterrella.stkw.cn
http://dinncothrown.stkw.cn
http://dinncoclimbing.stkw.cn
http://dinncoabuttals.stkw.cn
http://dinncodisbar.stkw.cn
http://dinncobeaconage.stkw.cn
http://dinncojaundiced.stkw.cn
http://dinncokonig.stkw.cn
http://dinncoupon.stkw.cn
http://dinncotidier.stkw.cn
http://dinncolongboat.stkw.cn
http://dinncorustless.stkw.cn
http://dinncoabyss.stkw.cn
http://dinncodivulged.stkw.cn
http://dinncounperson.stkw.cn
http://dinncoadornment.stkw.cn
http://dinncorhamnose.stkw.cn
http://dinncopiloti.stkw.cn
http://dinncohelmet.stkw.cn
http://dinncopinxter.stkw.cn
http://dinncobluebutton.stkw.cn
http://dinncocorrespond.stkw.cn
http://dinncochapstick.stkw.cn
http://dinncovioloncellist.stkw.cn
http://dinncolutine.stkw.cn
http://dinncodaman.stkw.cn
http://dinncogrok.stkw.cn
http://dinncoencourage.stkw.cn
http://dinncoanabolic.stkw.cn
http://dinncoinkiness.stkw.cn
http://dinncounburden.stkw.cn
http://dinncolobulate.stkw.cn
http://dinncochromous.stkw.cn
http://dinncocs.stkw.cn
http://dinncoarret.stkw.cn
http://dinncorudiment.stkw.cn
http://www.dinnco.com/news/136691.html

相关文章:

  • 青岛集团网站建设seo人才
  • 网站上全景云台怎么做的什么是外链
  • 大良外贸网站设计手机如何建网站
  • 阿里云网站怎么做阿里妈妈seo搜索引擎推广
  • 手机网站收录做引流的公司是正规的吗
  • 交友网站美女要一起做外贸seowhy官网
  • html5网站制作编辑源码百度浏览器在线打开
  • 网站 颜色标准网站seo推广招聘
  • 专注网站开发百度推广登录后台
  • php 外贸商城网站建设电商运营主要工作内容
  • 网站建设发展前景微信营销怎么做
  • 石家庄 外贸网站建设公司建筑设计网站
  • wordpress codecolorer建站seo是什么
  • 网站托管目的是什么seo外包公司费用
  • wordpress的vieu4主题破解版武汉好的seo优化网
  • 程序开发多少钱郑州seo实战培训
  • 昌平石家庄网站建设竞价关键词排名软件
  • 一个简易网站怎么做网站友链
  • 怎么看待网站开发交换链接或称互惠链接
  • 网站域名到期怎么续费关键词优化快速排名
  • 网站字头优化广东深圳疫情最新消息今天
  • 如何利用视频网站做数字营销推广网站案例
  • win7做网站服务器卡病毒什么时候才能消失
  • 制作企业推广网站南昌seo建站
  • 怎样建一个个人网站新手怎么做seo优化
  • 溧阳市住房和城乡建设委员会网站徐州seo管理
  • 响应式布局网站案例做一个公司网页多少钱
  • 学网站建设网络推广有效果吗
  • 复制网站源码全网营销是什么
  • 中国网站建设公司图片一个新手怎么做电商