JVerice Java Dev Engineer

操作文件

2017-08-20
JVerice

Java SE7操作文件

操作文件

Path

目录名序列,其后可以跟着一个文件名

java.nio.file.Paths 7

  • 通过连接给定的字符串创建一个路径
    static Path get(String first,String...more)
    
  • Path resolve(Path other)
    Path resolve(String other)
    如果other是绝对路径,那么返回other;否则,返回通过连接this和other获得的路径
    
  • 解析指定路径的父路径产生兄弟路径
    Path resolveSibling(Path other)
    Path resolveSibling(String other)
    如果other是绝对路径,那么返回other;否则,返回通过连接this的父路径和other获得的路径
    
  • 对两个路径进行相对化操作 /home/cay为目标对/home/fred/myprog进行相对化操作产生../fred/mypr
    Path relativize(Path other)
    返回用this进行解析,相对于other的相对路径。
    
  • Path normalize()
    移除诸如.和..等冗余的路径元素。
    
  • Path toAbsolutePath()
    得到绝对路径
    
  • Path getParent()
    返回父路径,没有则为null
    
  • Path getFileName()
    返回最后一个部件,没有则为null
    
  • Path getRoot()
    返回该路径的根部件,没有则为null
    
  • Path toFile()
    从该路径中创建一个File对象
    

Files

读写文件

java.nio.file.Files 7

  • 普通文本

    1.读取文件所有内容
    byte[] bytes = Files.readAllBytes(path);
    2.将文件当做字符串读入
    String content = new String(bytes,charset);
    3.如果希望将文件当做行序列读入
    List<String> lines = Files.reasAllLines(path,charset);
    4.相反的, 写出一个字符串到文件中
    Files.write(path,content.getBytes(charset));
    5.向指定文件追加内容
    Files.write(path,content.getBytes(charset),StandardOpenOption.APPEND);
    6.将一行的集合写出到文件中
    Files.write(path,lines);
    
  • 大文件或者二进制

    InputStream in = Files.newInputStream(path);
    OutputStream out = Files.newOutputStream(path);
    Read in = Files.newBuffereReader(path,charset);
    Write out = Files.newBuffereWriter(path,charset);
    

复制、移动和删除

1.复制
Files.copy(fromPath,toPath);
2.移动
Files.move(fromPath,toPath);
Files.move(fromPath,toPath,StandardCopyOption.REPLACE_EXISTING,
			StandardCopyOption.COPY_ATTRIBUTES)
REPLACE_EXISTING:目标已存在,覆盖已有目标路径
COPY_ATTRIBUTES:复制所有文件属性

Files.move(fromPath,toPath,StandardCopyOption.ATOMIC_MOVE)
ATOMIC_MOVE:原子性,要么移动成功要么源文件继续保存原来位置
3.删除
Files.delete(path);//文件不存在抛出异常
boolean deleted = Files.deleteIfExists(path);

创建文件和目录

1.创建新目录
Files.createDirectory(path);
路径中除最后一个部件外,其他必须是已存在
2.创建中间目录
Files.createDirectories(path);
3.创建一个空文件
Files.createFile(path);
4.创建临时文件或目录
Path newPath = Files.createTempFile(dir,prefix,suffix);
Path newPath = Files.createTempFile(prefix,suffix);
Path newPath = Files.createTempDirectory(dir,prefix,suffix);
Path newPath = Files.createTempDirectory(prefix,suffix);
dir:一个Path对象
prefix,suffix:可为null
Files.createTempFile(null,".txt");返回像/tmp/1234124123123123.txt这样的路径

获取文件信息

1.返回boolean值,表示检查路径的某个属性的结果
boolean exists(Path path);
isHidden;
isReadable,isWritable,isExecutable
isRegularFile,isDirectory,isSymbolicLink
2.返回文件的字节数
long fileSize = File.size(path);

迭代目录中的文件

Files类设计了一个方法,可以产生一个Iterable对象
try(DirectoryStream<Path> entries = Files.newDirectoryStream(dir))
{
  for(Path entry:entries)
  	Process entries
}
try语句用来确保目录流可以被正确关闭。

打印给定目录下的所有子目录
Files.walkFileTree(dir,new SimpleFileVisitor<Path>(){
  public FileVisitReslut visitFile(Path path,BasicFileAttributes attrs) throws IOEction{
 	 System.out.println(path);
  	 return FileVisitResult.CONTINUE;
   }
  public FileVisitReslut visitFileFailed(Path path,IOException) throws IOEction{
  	 return FileVisitResult.CONTINUE;
   }
});
注意:需要覆盖visitFileFailed方法,否则,访问会在遇到不允许打开的目录时立即失败

ZIP文件系统

建立一个文件系统,包含zip文档中的所有文件。
FileSystem fs = FileSystem.newFileSystem(Paths.get(zipname),null);
zipname:某个zip文件的名字。
Files.cope(fs.getPath(source),targetPath);
列出zip文档中所有文件,可以遍历文件树:
FileSystem fs = FileSystem.newFileSystem(Paths.get(zipname),null);
Files.walkFileTree(fs.getPath("/"),new SimpleFileVisitor<Path>(){
  public FileVisitReslut visitFile(Path file,BasicFileAttributes attrs) throws IOEction{
 	 System.out.println(file);
  	 return FileVisitResult.CONTINUE;
   }
});

Similar Posts

下一篇 XML

Comments