File class, IO stream

File class

  1. Created on delete;
//如果文件存在返回false;否则返回true
boolean createNewFile();
//创建一个File对象所对应的目录,成功返回true,失败返回false
//必须是路径而不能为文件
//只会创建最后一级目录,如果上级不存在则会抛出异常。
boolean mkdir();
//创建一个File对象所对应的目录,成功返回true,失败返回false
//必须是路径而不能为文件
//创建多级目录,创建路径中所有不存在的目录
boolean mkdirs();
//如果存在返回true并且删除,否则返回false
boolean delete();
//在虚拟机终止,删除File对象所表示的文件或目录
void deleteOnExit();
  1. Judgment method;
//判断文件是否可以执行
boolean canExecute();
//判断文件是否可以读取
boolean canRead();
//判断文件是否可以写
boolean canWrite();
//判断文件是否存在
boolean exists();
//判断是否是目录
boolean isDirectory();
//判断是否是文件
boolean isFile();
//判断是否是隐藏文件或者隐藏目录
boolean isHidden();
//判断是否是绝对路径,文件不存在也能判断
boolean isAbsolute();
  1. Method of obtaining;
//返回文件或是目录的名称
String getName();
//返回路径
String getPath();
//返回绝对路径
String getAbsolutePath();
//返回父目录,如果没有父目录则返回NULL
String getParent();
//返回最后一次修改时间
long lastModified();
//返回文件长度
long length();
//列出所有的根目录
File[] listRoots();
//返回一个字符串数组,给定路径下的文件或目录名称字符串
String[] list();
//返回满足过滤器要求的字符串数组
String[] list(FilenameFilter filter);
//返回一个文件对象数组,给定路径下的目录文件
File[] listFiles();
//返回满足过滤器要求的一个文件对象数组
File[] listFiles(FilenameFilter filter);

IO stream
__ The four types of IO stream : Reader, InputStream, Writer, OutputStream.


  1. The parent class of InputStream input byte stream. It is an abstract class that contains three methods:
//读取一个字节并以整数的形式返回(0~255),如果返回-1已到输入流的末尾。
int read()//读取一系列字节并存储到一个数组buffer,返回实际读取的字节数,如果读取前已
到输入流的末尾返回-1int read(byte[] buffer)//读取length个字节并存储到一个字节数组buffer,从off位置开始存,最多len,
返回实际读取的字节数,如果读取前以到输入流的末尾返回-1int read(byte[] buffer, int off, int len)
  1. Reader is
    the parent class of all input character streams. It is an abstract class.
//读取一个字符并以整数的形式返回(0~255),如果返回-1已到输入流的末尾。
int read()//读取一系列字符并存储到一个数组buffer,返回实际读取的字符数,如果读取前已
到输入流的末尾返回-1int read(char[] cbuf)//读取length个字符,并存储到一个数组buffer,从off位置开始存,最多读取len,
返回实际读取的字符数,如果读取前以到输入流的末尾返回-1int read(char[] cbuf, int off, int len)

To invoke After you perform stream operations close () method to close the input stream, because the program was opened IO resource does not belong to memory resources, the garbage collector can not reclaim the resource . So show close file IO resources.

Move the pointer position in the stream

//在此输入流中标记当前的位置
//readlimit - 在标记位置失效前可以读取字节的最大限制。
void mark(int readlimit)
// 测试此输入流是否支持 mark 方法
boolean markSupported()
// 跳过和丢弃此输入流中数据的 n 个字节/字符
long skip(long n)
//将此流重新定位到最后一次对此输入流调用 mark 方法时的位置
void reset(

  1. OutputStream
    output byte stream, contains 4 methods
//向输出流中写入一个字节数据,该字节数据为参数b的低8位。
void write(int b) ;
//将一个字节类型的数组中的数据写入输出流。
void write(byte[] b);
//将一个字节类型的数组中的从指定位置(off)开始的,len个字节写入到输出流。
void write(byte[] b, int off, int len);
//将输出流中缓冲的数据全部写出到目的地。
void flush();
  1. Writer
    output character stream parent class, it is an abstract class, mainly contains the following six methods
//向输出流中写入一个字符数据,该字节数据为参数b的低16位。
void write(int c);
//将一个字符类型的数组中的数据写入输出流,
void write(char[] cbuf)
//将一个字符类型的数组中的从指定位置(offset)开始的,length个字符写入到输出流。
void write(char[] cbuf, int offset, int length);
//将一个字符串中的字符写入到输出流。
void write(String string);
//将一个字符串从offset开始的length个字符写入到输出流。
void write(String string, int offset, int length);
//将输出流中缓冲的数据全部写出到目的地。
void flush()

Writer has two more methods than OutputStream, mainly supporting writing character and string data.

1. RandomAccessFile
RandomAccessFile can not only read the contents of the file, but also output data to the file.

import java.io.*;
import java.nio.charset.StandardCharsets;

public class FileMain {
    
    
    public static void main(String[] args) {
    
    
//        String filePath = "E:" + File.separator +
//                "untitled" + File.separator + "src" + File.separator + "Main.java";
//        fileInput(filePath);
//        bufferInputStream(filePath);
        String filePath = "E:" + File.separator +
                "untitled" + File.separator + "src" + File.separator + "randomAccessFile.java";
//            createFile(filePath);
        randomAccessFile(filePath);
    }

    private static void randomAccessFile(String filePath) {
    
    
        String content = "import java.io.*;" +
                "public class randomAccessFile{" +
                "   public static void main(String[]args){" +
                "System.out.println(\"创建文本randomAccessFile\");" +
                "}" +
                "}";

        new FileWriteThread(content.length(), content.getBytes(StandardCharsets.UTF_8), filePath).start();

    }

    static class FileWriteThread extends Thread {
    
    
        private int read;
        private byte[] content;
        private String path;

        public FileWriteThread(int read, byte[] content, String path) {
    
    
            this.read = read;
            this.content = content;
            this.path = path;
        }

        @Override
        public void run() {
    
    
            RandomAccessFile accessFile = null;
            try {
    
    
                accessFile = new RandomAccessFile(path, "rw");
//                accessFile.seek(0);
                accessFile.write(content);
            } catch (FileNotFoundException e) {
    
    
                e.printStackTrace();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            } finally {
    
    
                if (accessFile != null) {
    
    
                    try {
    
    
                        accessFile.close();
                    } catch (IOException e) {
    
    
                        e.printStackTrace();
                    }

                }
            }
        }
    }

    private static void createFile(String filePath) {
    
    
        String content = "import java.io.*;" +
                "public class DemoTest{" +
                "   public static void main(String[]args){" +
                "System.out.println(\"创建文本DemoTest\");" +
                "}" +
                "}";
        File file = new File(filePath);

        OutputStream outputStream = null;
        try {
    
    
            if (!file.exists()) {
    
    
                file.createNewFile();
            }
            file.setReadable(true);
            file.setWritable(true);
            outputStream = new FileOutputStream(file);
            outputStream.write(content.getBytes());

        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (outputStream != null) {
    
    
                try {
    
    
                    outputStream.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

    private static void bufferInputStream(String filePath) {
    
    
        try {
    
    
            File file = new File(filePath);
            if (!file.isFile()) {
    
    
                return;
            }
            InputStream fileInputStream = new FileInputStream(file.getPath());
            BufferedInputStream bis = new BufferedInputStream(fileInputStream);
            byte[] bytes = new byte[(int) file.length()];
            int len = 0;
            if ((len = bis.read(bytes)) != -1) {
    
    
                System.out.println(new String(bytes, 0, len));
            }
            bis.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    private static void fileInput(String filePath) {
    
    
        File mainFile = new File(filePath);
        if (!mainFile.isFile()) {
    
    
            System.out.println("文件不存在");
            return;
        }
        try {
    
    
            FileInputStream fis = new FileInputStream(mainFile.getAbsoluteFile());
            byte[] fLength = new byte[(int) mainFile.length()];
            while (fis.read(fLength) != -1) {
    
    
                System.out.println(new String(fLength));
            }

        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_42789301/article/details/114263702