Java 输入/输出——File类

  File类是java.io包下代表与平台无关的文件和目录,也就是说,如果希望在程序中操作文件和目录,都可以通过File类来完成。值得指出的是,不管是文件还是目录都是使用File来操作的,File能新建、删除、重命名文件和目录,File不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。

  File类相关的方法参考链接:https://docs.oracle.com/javase/9/docs/api/overview-summary.html

  • Field Summary

    Fields
    Modifier and Type Field Description
    static String pathSeparator
    The system-dependent path-separator character, represented as a string for convenience.
    static char pathSeparatorChar
    The system-dependent path-separator character.
    static String separator
    The system-dependent default name-separator character, represented as a string for convenience.
    static char separatorChar
    The system-dependent default name-separator character.
  • Constructor Summary

    Constructors
    Constructor Description
    File?(File parent, String child)
    Creates a new File instance from a parent abstract pathname and a child pathname string.
    File?(String pathname)
    Creates a new File instance by converting the given pathname string into an abstract pathname.
    File?(String parent, String child)
    Creates a new File instance from a parent pathname string and a child pathname string.
    File?(URI uri)
    Creates a new File instance by converting the given file: URI into an abstract pathname.
  • Method Summary

    All MethodsStatic MethodsInstance MethodsConcrete MethodsDeprecated Methods
    Modifier and Type Method Description
    boolean canExecute?()
    Tests whether the application can execute the file denoted by this abstract pathname.
    boolean canRead?()
    Tests whether the application can read the file denoted by this abstract pathname.
    boolean canWrite?()
    Tests whether the application can modify the file denoted by this abstract pathname.
    int compareTo?(File pathname)
    Compares two abstract pathnames lexicographically.
    boolean createNewFile?()
    Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
    static File createTempFile?(String prefix,String suffix)
    Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
    static File createTempFile?(String prefix,String suffix,File directory)
    Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
    boolean delete?()
    Deletes the file or directory denoted by this abstract pathname.
    void deleteOnExit?()
    Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
    boolean equals?(Object obj)
    Tests this abstract pathname for equality with the given object.
    boolean exists?()
    Tests whether the file or directory denoted by this abstract pathname exists.
    File getAbsoluteFile?()
    Returns the absolute form of this abstract pathname.
    String getAbsolutePath?()
    Returns the absolute pathname string of this abstract pathname.
    File getCanonicalFile?()
    Returns the canonical form of this abstract pathname.
    String getCanonicalPath?()
    Returns the canonical pathname string of this abstract pathname.
    long getFreeSpace?()
    Returns the number of unallocated bytes in the partition named by this abstract path name.
    String getName?()
    Returns the name of the file or directory denoted by this abstract pathname.
    String getParent?()
    Returns the pathname string of this abstract pathname‘s parent, or null if this pathname does not name a parent directory.
    File getParentFile?()
    Returns the abstract pathname of this abstract pathname‘s parent, or null if this pathname does not name a parent directory.
    String getPath?()
    Converts this abstract pathname into a pathname string.
    long getTotalSpace?()
    Returns the size of the partition named by this abstract pathname.
    long getUsableSpace?()
    Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.
    int hashCode?()
    Computes a hash code for this abstract pathname.
    boolean isAbsolute?()
    Tests whether this abstract pathname is absolute.
    boolean isDirectory?()
    Tests whether the file denoted by this abstract pathname is a directory.
    boolean isFile?()
    Tests whether the file denoted by this abstract pathname is a normal file.
    boolean isHidden?()
    Tests whether the file named by this abstract pathname is a hidden file.
    long lastModified?()
    Returns the time that the file denoted by this abstract pathname was last modified.
    long length?()
    Returns the length of the file denoted by this abstract pathname.
    String[] list?()
    Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
    String[] list?(FilenameFilter filter)
    Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
    File[] listFiles?()
    Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
    File[] listFiles?(FileFilter filter)
    Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
    File[] listFiles?(FilenameFilter filter)
    Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
    static File[] listRoots?()
    List the available filesystem roots.
    boolean mkdir?()
    Creates the directory named by this abstract pathname.
    boolean mkdirs?()
    Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
    boolean renameTo?(File dest)
    Renames the file denoted by this abstract pathname.
    boolean setExecutable?(boolean executable)
    A convenience method to set the owner‘s execute permission for this abstract pathname.
    boolean setExecutable?(boolean executable, boolean ownerOnly)
    Sets the owner‘s or everybody‘s execute permission for this abstract pathname.
    boolean setLastModified?(long time)
    Sets the last-modified time of the file or directory named by this abstract pathname.
    boolean setReadable?(boolean readable)
    A convenience method to set the owner‘s read permission for this abstract pathname.
    boolean setReadable?(boolean readable, boolean ownerOnly)
    Sets the owner‘s or everybody‘s read permission for this abstract pathname.
    boolean setReadOnly?()
    Marks the file or directory named by this abstract pathname so that only read operations are allowed.
    boolean setWritable?(boolean writable)
    A convenience method to set the owner‘s write permission for this abstract pathname.
    boolean setWritable?(boolean writable, boolean ownerOnly)
    Sets the owner‘s or everybody‘s write permission for this abstract pathname.
    Path toPath?()
    Returns a java.nio.file.Path object constructed from the this abstract path.
    String toString?()
    Returns the pathname string of this abstract pathname.
    URI toURI?()
    Constructs a file: URI that represents this abstract pathname.
    URL toURL?()
    Deprecated.

    This method does not automatically escape characters that are illegal in URLs. It is recommended that new code convert an abstract pathname into a URL by first converting it into a URI, via the toURImethod, and then converting the URI into a URL via the URI.toURL method.

  1. 访问文件和目录
 1 package com.zyjhandsome.io;
 2
 3 import java.io.*;
 4
 5 public class FileTest {
 6
 7     public static void main(String[] args) throws IOException
 8     {
 9         // 以当前路径来创建一个File对象
10         File file = new File(".");
11         // 直接获取文件名, 输出一点
12         System.out.println(file.getName());
13         // 获取相对路径的父路径可能出错, 下面代码输出null
14         System.out.println(file.getParent());
15         // 获取绝对路径
16         System.out.println(file.getAbsoluteFile());
17         // 获取绝对路径
18         System.out.println(file.getAbsoluteFile().getParent());
19         // 当前路径下创建一个临时文件
20         File tmpFile = File.createTempFile("aaa", ".txt", file);
21         // 指定当JVM退出时候删除该文件
22         tmpFile.deleteOnExit();
23         // 以系统当前时间作为新文件名来创建新文件
24         File newFile = new File(System.currentTimeMillis() + "");
25         System.out.println("newFile对象是否存在1: " + newFile.exists());
26         // 以指定newFile对象来创建一个文件
27         newFile.createNewFile();
28         System.out.println("newFile对象是否存在2: " + newFile.exists());
29         // 以newFile对象来创建一个目录,因为newFile已经存在,所以下面方法返回false, 即无法创建该目录
30         System.out.println("newFile.mkdir():" + newFile.mkdir());
31         System.out.println("----------------------");
32         // 使用list()方法列出当前路径下的所有文件和路径
33         String[] fileList = file.list();
34         for (String fileName : fileList)
35         {
36             System.out.println(fileName);
37         }
38         System.out.println("----------------------");
39         // listRoots()静态方法列出所有的磁盘根路径
40         File[] roots = File.listRoots();
41         System.out.println("====系统所有根路径如下====");
42         for (File root : roots)
43         {
44             System.out.println(root);
45         }
46     }
47 }

原文地址:https://www.cnblogs.com/zyjhandsome/p/9694039.html

时间: 2024-10-12 11:17:20

Java 输入/输出——File类的相关文章

Java 输入/输出 反射

Java  输入/输出   反射 输入输出和反射 一.数据流的基本概念 流一般分为 ( Input Stream ) 和输出流 ( Output Stream ) 两类,但这种划分并不是绝对的.比如一个文件,当向其中写数 据时,它就是一个输出流:当从其中读取数据时,它就是一个输 入流.当然,键盘只是一个数人流,而屏幕则只是一个输出流. java 的标准数据流: 标准输入输出指在字符方式下 (如 DOS ) , 程序与系统进行交互的方式,分为三种: 标准输入 studin, 对象是键盘. 标准输出

java中的File类

File类 java中的File类其实和文件并没有多大关系,它更像一个对文件路径描述的类.它即可以代表某个路径下的特定文件,也可以用来表示该路径的下的所有文件,所以我们不要被它的表象所迷惑.对文件的真正操作,还得需要I/O流的实现. 1.目录列表 如果我们想查看某个目录下有那些文件和目录,我们可以使用File中提供的list方式来查看,这很像linux下的ls命令. 查看E:/html文件夹下所有的php文件,执行的时候输入的参数为正则表达式 1 package com.dy.xidian; 2

Java—IO流 File类的常用API

File类 1.只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. package cn.test; import java.io.File; import java.io.IOException; public class Demo16 { public static void main(String[] args) { File file = new File("F:\\javaio"); //文件(目录)是否存在 if(!file.exists()) { //

java io包File类

1.java io包File类, Java.io.File(File用于管理文件或目录: 所属套件:java.io)1)File对象,你只需在代码层次创建File对象,而不必关心计算机上真正是否存在对象文件. file类的几个常用方法:file.canRead() boolean ,file.canWrite() boolean,file.canExecute() boolean,file.createNewFile() boolean.(1)file.createNewFile() boole

java学习一目了然——File类文件处理

java学习一目了然--File类文件处理 File类(java.io.File) 构造函数: File(String path) File(String parent,String child) File(File parent,String child) 创建文件: boolean createNewFile(); 创建文件夹: boolean mkdir(); 用于创建一层未定义文件夹 boolean mkdirs(); 用于创建多层未定义文件夹,相当于多个mkdir() 删除文件/文件夹

Java学习笔记——File类之文件管理和读写操作、下载图片

Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图片 文件和文件夹 相关函数 (boolean) mkdir() 创建此抽象路径名指定的目录  (boolean) mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录. (boolean) delete() 删除此抽象路径名表示的文件或目录 (boolean) createNe

Java基础之File类的使用

Java基础之File类的使用 1.File类的构造方法和常用方法 2.对File中listFile(FileNameFilter name)学习 3.与File文件类相关的实现 File类的构造方法和常用方法: 在Java中File类在IO流中被频繁使用,可以使用一个文件路径来表示在特定位置上的文件,但是需要注意的是这个路径只表示一个文件抽象的概念, 文件到底在不在这个路径下是不确定,换句话说,是不能通过文件路径来判断文件是否存在. 构造方法 File(File parent, String 

输入和输出--File类

输入和输出 写在前面的:Java中的输入和输出还是比较重要的,之前我都没想整理IO和多线程的,但是在研究后面的好多东西时候,经常要读取资源文件,这个时候就不得不回过头来整理IO了.要玩熟Java的输入和输入,就必须先玩熟一个类:File.然后在就是Java.io包下的类和接口了.Java的IO主要包括了输入和输出2种流,每种输入和输出又分为字节流和字符流.值得注意和研究的是Java的IO流使用了一种装饰器设计模式,它将IO流分成底层节点流和上层处理流.除此之外,还要研究2个东西:1,Java对象

黑马程序员——【Java基础】——File类、Properties集合、IO包中的其他类

---------- android培训.java培训.期待与您交流! ---------- 一.File类 (一)概述 1.File类:文件和目录路径名的抽象表现形式 2.作用: (1)用来将文件或文件夹封装成对象 (2)方便于对“文件”与“文件夹属性信息”进行操作 (3)File对象,可以作为参数传递给流的构造函数 (二)构造方法 * 通过File的构造函数创建File对象 方式1:File f = new File("c:\\a.txt"); 方式2:File f2 = newF