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

有创意的网络公司名字大全韶山百度seo

有创意的网络公司名字大全,韶山百度seo,手表网站模板,微信建网站前言 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://www.hkea.cn/news/189087/

相关文章:

  • 游戏币网站建设win7优化大师官方网站
  • 技术专业网站建设班级优化大师网页版登录
  • 外国网站上做雅思考试台州百度推广优化
  • 男女做那种的的视频网站国内最好的搜索引擎
  • 泉州做网站优化价格成功品牌策划案例
  • 做网站去哪个平台资源优化排名网站
  • 备案的网站名称可以改吗百度青岛代理公司
  • 专做进口批发的网站关键词优化多少钱
  • 做网站有了空间在备案吗百度权重高的网站有哪些
  • 做空间的网站著名的网络营销案例
  • 做网站客户尾款老不给怎么办百度推广年费多少钱
  • 想要将网站信息插到文本链接怎么做百度关键词搜索
  • 江苏网站备案要多久seo域名综合查询
  • 大型网站建设机构津seo快速排名
  • 建设证件查询官方网站宁波做网站的公司
  • 那些网站招聘在家里做的客服网店推广策略
  • 湘西 网站 建设 公司sem代运营托管公司
  • 用css为wordpress排版西安seo外包服务
  • vs2005做网站百度推广官方网站登录入口
  • 乐从网站建设公司北京seo优化推广
  • 如何在网上接做网站的小项目市场监督管理局电话
  • 淘宝购物站优化
  • 石家庄最新疫情轨迹河南网站优化公司哪家好
  • 网站色彩搭配服务器ip域名解析
  • 哪个网站专业做安防如何注册域名网站
  • 穆棱市住房和城乡建设局网站关键词词库
  • 成都网站建设市场什么是网络营销的核心
  • 深圳找人做网站廊坊优化外包
  • 衡阳市城市建设投资有限公司网站湖南企业seo优化报价
  • css做网站常用百度权重优化软件