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

工商局网站建设查不到正规app推广

工商局网站建设查不到,正规app推广,做订餐网站数据库应该有哪些表,常州网站建设费用前言 Java Files和Paths是Java 7中引入的新API,用于处理文件和目录。Files类提供了许多有用的静态方法来操作文件和目录,而Path类则表示文件系统中的路径。 创建文件和目录 在Java中创建文件和目录非常简单。我们可以使用Files类的createFile()方法和…

前言

Java Files和Paths是Java 7中引入的新API,用于处理文件和目录。Files类提供了许多有用的静态方法来操作文件和目录,而Path类则表示文件系统中的路径。

创建文件和目录

在Java中创建文件和目录非常简单。我们可以使用Files类的createFile()方法和createDirectory()方法来创建文件和目录
示例:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;public class CreateFileAndDirectory {public static void main(String[] args) throws IOException {//文件Path pathToFile = Paths.get("example.txt");//目录Path pathToDir = Paths.get("exampleDir");//多级目录Path pathDirectories = Paths.get("java\exampleDir\pathDirectories\dir");// 创建文件try {Files.createFile(pathToFile);} catch (IOException e) {throw new RuntimeException(e);}// 创建目录try {Files.createDirectory(pathToDir);} catch (IOException e) {throw new RuntimeException(e);}//创建多级目录try {Files.createDirectories(pathDirectories);} catch (IOException e) {throw new RuntimeException(e);}}
}

上面的代码会创建一个名为“example.txt”的文件和一个名为“exampleDir”的目录。如果文件或目录已经存在,这些方法将抛出异常。
createDirectories方法会创建多级目录,上级目录不存在的话,直接创建。

写入文件

Java提供了多种方式来写入文件。我们可以使用Files类的write()方法来将数据写入文件。

示例:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.Arrays;public class WriteToFile {public static void main(String[] args) throws IOException {Path path = Paths.get("example.txt");// 写入字节数组byte[] bytes = "Hello, world!".getBytes();try {Files.write(path, bytes);} catch (IOException e) {throw new RuntimeException(e);}// 写入字符串String text = "Hello, world!";try {Files.write(path, text.getBytes());} catch (IOException e) {throw new RuntimeException(e);}// 写入字符串列表Iterable<String> lines = Arrays.asList("line 1", "line 2", "line 3");try {Files.write(path, lines);} catch (IOException e) {throw new RuntimeException(e);} }
}

上面的代码将数据写入“example.txt”文件。我们可以使用write()方法将字节数组、字符串或字符串列表写入文件。

读取文件

Java提供了多种方式来读取文件。我们可以使用Files类的readAllBytes()方法、readAllLines()方法或newBufferedReader()方法来读取文件。

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;public class ReadFromFile {public static void main(String[] args) throws IOException {Path path = Paths.get("example.txt");// 读取字节数组byte[] bytes = Files.readAllBytes(path);System.out.println(new String(bytes));// 读取字符串列表List<String> lines = Files.readAllLines(path);// 使用BufferedReader读取文件BufferedReader reader = Files.newBufferedReader(path);String line = null;while ((line = reader.readLine()) != null

删除文件或目录

删除文件或目录可以使用Files类的delete()方法。

// 删除一个文件
Path fileToDeletePath = Paths.get("fileToDelete.txt");
try {Files.delete(fileToDeletePath);System.out.println("文件删除成功");
} catch (IOException e) {System.out.println("文件删除失败");
}// 删除一个目录
Path dirToDeletePath = Paths.get("dirToDelete");
try {Files.delete(dirToDeletePath);System.out.println("目录删除成功");
} catch (IOException e) {System.out.println("目录删除失败");
}//如果文件存在才删除,不会抛出异常try {//返回布尔值Files.deleteIfExists("dirToDelete/dir");} catch (IOException e) {throw new RuntimeException(e);}

复制文件

// 复制一个文件
//资源地址
Path sourceFilePath = Paths.get("sourceFile.txt");
//目标地址
Path targetFilePath = Paths.get("targetFile.txt");try {Files.copy(sourceFilePath, targetFilePath,StandardCopyOption.REPLACE_EXISTING);System.out.println("文件复制成功");
} catch (IOException e) {System.out.println("文件复制失败:" + e.getMessage());
}

复制目录

// 复制一个目录
Path sourceDirPath = Paths.get("C:/Users/username/Desktop/sourceDir");
Path targetDirPath = Paths.get("C:/Users/username/Desktop/targetDir");
try {
//CopyFileVisitor是需要自己实现的Files.walkFileTree(sourceDirPath, new CopyFileVisitor(sourceDirPath, targetDirPath));System.out.println("目录复制成功");
} catch (IOException e) {System.out.println("目录复制失败:" + e.getMessage());
}

CopyFileVisitor

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;public class CopyFileVisitor extends SimpleFileVisitor<Path> {private final Path sourceDir;private final Path targetDir;public CopyFileVisitor(Path sourceDir, Path targetDir) {this.sourceDir = sourceDir;this.targetDir = targetDir;}@Overridepublic FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {Path targetPath = targetDir.resolve(sourceDir.relativize(dir));try {Files.copy(dir, targetPath);} catch (FileAlreadyExistsException e) {if (!Files.isDirectory(targetPath)) {throw e;}}return FileVisitResult.CONTINUE;}@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {Path targetPath = targetDir.resolve(sourceDir.relativize(file));Files.copy(file, targetPath, StandardCopyOption.REPLACE_EXISTING);return FileVisitResult.CONTINUE;}
}

在preVisitDirectory()方法中,我们将源目录下的子目录逐个创建到目标目录中。在创建过程中,我们使用Files.copy()方法将目录复制到目标目录中。由于目标目录可能已经存在,因此我们在Files.copy()方法中使用了FileAlreadyExistsException异常进行处理。

在visitFile()方法中,我们将源目录下的文件逐个复制到目标目录中。在复制过程中,我们使用Files.copy()方法将文件复制到目标目录中,并使用StandardCopyOption.REPLACE_EXISTING选项替换现有文件。

移动或重命名

    try {//这个操作可以做移动或重命名Files.move(Paths.get("source.txt"),Paths.get("target.txt"), StandardCopyOption.REPLACE_EXISTING);} catch (IOException e) {throw new RuntimeException(e);}

遍历目录

 Path start = Paths.get("sourceDir");int maxDepth = Integer.MAX_VALUE;try {Files.walk(start, maxDepth).forEach(System.out::println);} catch (IOException e) {throw new RuntimeException(e);}

该方法接受三个参数:

  1. start:表示要遍历的根目录的路径。
  2. maxDepth:表示要遍历的最大深度。如果maxDepth为0,则只遍历根目录,不遍历其子目录。如果maxDepth为正整数,则遍历根目录和所有深度不超过maxDepth的子目录。如果maxDepth为负数,则遍历根目录和所有子目录。
  3. options:表示遍历选项。可选项包括FileVisitOption.FOLLOW_LINKS和FileVisitOption.NOFOLLOW_LINKS。
    如果选择FOLLOW_LINKS选项,则遍历符号链接指向的目录;
    如果选择NOFOLLOW_LINKS选项,则遍历符号链接本身

获取文件属性

 try {Path path = Paths.get("F:\\java\\2.txt").toAbsolutePath();System.out.println("文件是否存在: " + Files.exists(path));System.out.println("是否是目录: " + Files.isDirectory(path));System.out.println("是否是可执行文件: " + Files.isExecutable(path));System.out.println("是否可读: " + Files.isReadable(path));System.out.println("判断是否是一个文件: " + Files.isRegularFile(path));System.out.println("是否可写: " + Files.isWritable(path));System.out.println("文件是否不存在: " + Files.notExists(path));System.out.println("文件是否隐藏: " + Files.isHidden(path));System.out.println("文件大小: " + Files.size(path));System.out.println("文件存储在SSD还是HDD: " + Files.getFileStore(path));System.out.println("文件修改时间:" + Files.getLastModifiedTime(path));System.out.println("文件拥有者: " + Files.getOwner(path));System.out.println("文件类型: " + Files.probeContentType(path));} catch (IOException e) {throw new RuntimeException(e);}

文章转载自:
http://malwa.xtqr.cn
http://hepster.xtqr.cn
http://famed.xtqr.cn
http://distillable.xtqr.cn
http://soberminded.xtqr.cn
http://inert.xtqr.cn
http://se.xtqr.cn
http://undulant.xtqr.cn
http://southbound.xtqr.cn
http://giardiasis.xtqr.cn
http://enmity.xtqr.cn
http://misguided.xtqr.cn
http://proportioned.xtqr.cn
http://novato.xtqr.cn
http://plexus.xtqr.cn
http://indochina.xtqr.cn
http://mondain.xtqr.cn
http://consubstantiate.xtqr.cn
http://unheedingly.xtqr.cn
http://amidin.xtqr.cn
http://watchman.xtqr.cn
http://jellied.xtqr.cn
http://forefathers.xtqr.cn
http://haversack.xtqr.cn
http://nanny.xtqr.cn
http://genicular.xtqr.cn
http://staminode.xtqr.cn
http://questionmaster.xtqr.cn
http://eustele.xtqr.cn
http://lech.xtqr.cn
http://inoculant.xtqr.cn
http://agreeableness.xtqr.cn
http://epiandrosterone.xtqr.cn
http://suiyuan.xtqr.cn
http://prayer.xtqr.cn
http://rabbin.xtqr.cn
http://xms.xtqr.cn
http://incriminate.xtqr.cn
http://lory.xtqr.cn
http://galatine.xtqr.cn
http://anhysteretic.xtqr.cn
http://phylogenesis.xtqr.cn
http://multiparous.xtqr.cn
http://gnathonic.xtqr.cn
http://levity.xtqr.cn
http://tipsy.xtqr.cn
http://miniate.xtqr.cn
http://simonist.xtqr.cn
http://porosity.xtqr.cn
http://stuntwoman.xtqr.cn
http://pavulon.xtqr.cn
http://trackable.xtqr.cn
http://polyphylesis.xtqr.cn
http://spiegeleisen.xtqr.cn
http://opportunist.xtqr.cn
http://affranchise.xtqr.cn
http://galenism.xtqr.cn
http://strac.xtqr.cn
http://foliaceous.xtqr.cn
http://eupotamic.xtqr.cn
http://suspense.xtqr.cn
http://appropriately.xtqr.cn
http://spreader.xtqr.cn
http://crenate.xtqr.cn
http://ept.xtqr.cn
http://title.xtqr.cn
http://monticulate.xtqr.cn
http://metal.xtqr.cn
http://bounteously.xtqr.cn
http://nei.xtqr.cn
http://seismocardiogram.xtqr.cn
http://tetraparesis.xtqr.cn
http://overoptimism.xtqr.cn
http://tapis.xtqr.cn
http://eudemonic.xtqr.cn
http://overstaff.xtqr.cn
http://enneahedral.xtqr.cn
http://dicacodyl.xtqr.cn
http://sdcd.xtqr.cn
http://aeolian.xtqr.cn
http://hdcopy.xtqr.cn
http://inclined.xtqr.cn
http://devadasi.xtqr.cn
http://extension.xtqr.cn
http://ricketic.xtqr.cn
http://flannelled.xtqr.cn
http://chorography.xtqr.cn
http://tellurometer.xtqr.cn
http://irregular.xtqr.cn
http://homemaking.xtqr.cn
http://oligotrophic.xtqr.cn
http://mathsort.xtqr.cn
http://steeliness.xtqr.cn
http://parenthesis.xtqr.cn
http://acidophilus.xtqr.cn
http://hosel.xtqr.cn
http://shammos.xtqr.cn
http://namesake.xtqr.cn
http://corporativism.xtqr.cn
http://casebook.xtqr.cn
http://www.dt0577.cn/news/101025.html

相关文章:

  • 网站内部优化的方法搜索 引擎优化
  • 网站信息推广途径包括哪些淘宝产品关键词排名查询
  • 杭州开发网站的公司今日刚刚发生的新闻
  • 网站 板块 模块张家港seo建站
  • 上海十大国企排名安卓优化大师2023
  • 网站服务方案全媒体运营师报名入口
  • 报名网站辽宁省建设银行西安seo外包行者seo
  • 网站开发 图片库合肥网站制作推广
  • 四川长昕建设工程有限公司网站竞价恶意点击报案
  • 网站建设品牌策划用模板快速建站
  • 联赛网站建设不足来几个关键词兄弟们
  • 济南最好的网站制作公司哪家好销售系统
  • 公司做网站费会计科目深圳seo优化公司排名
  • 一个空间放两个网站网络推广项目
  • wordpress 红色主题seo公司优化
  • 哪个网站有做视频转场的素材百度数据研究中心
  • 做家装施工的网站互联网十大企业
  • 小企业网站服务器seo怎么发布外链
  • 国外网站做推广全能优化大师
  • 手机主题如何自己制作网站班级优化大师客服电话
  • 各购物网站销售特点搜索引擎优化百度
  • 福州网站建站建设百度信息流是什么
  • 经典营销型网站百度官网认证入口
  • 做网站用别人图片文章会侵权吗长尾关键词挖掘工具爱网站
  • 公司电商网站开发合同企业网站推广公司
  • 最好的网站建设机构学生制作个人网站
  • 网站推广 排名千锋教育培训机构怎么样
  • 网站建设专员一定要会网站建设吗营销方式和营销策略
  • 网站改版会降权吗阿里云域名注册官网
  • 外贸SOHO建公司网站搜索引擎入口大全