IO操作类

本文是结合李兴华老师课程和自己的体会写的

一:核心
 1. 文件操作类:File;
 2. 流操作类:InputStream、OutputStream、Reader、Writer;
 3. 一个接口:Serializable

二:File类基本操作

 1.方法:
    1) 设置操作完整路径:public File​(String pathname)—>企业开发使用
    2)设置父路径与子文件:public File​(File parent,String child) Android使用
    3)判断路径是否正确:public boolean exists​()
   
   测试1:

File file = new File("D:\\demo.txt");
if (file.exists()) {
    System.out.println("hello, 文件存在!");
} else {
    file.createNewFile();
}

 2. 操作注意:
     定义路径是注意分隔符,“\”为“\”。也可以使用 “D:/demo.txt”
 
    测试2:

File file = new File("D:/demo.txt");
     if (file.exists()) {
     System.out.println("hello, 文件存在!");
  } else {
  file.createNewFile();
}

3. 操作File
  3.1.) 创建新文件:public boolean createNewFile​()throws IOException
          如果文件已存在,返回 false, 不存在创建文件并返回 true
  3.2. )删除文件:public boolean delete​()
  3.3.)创建文件夹
    i. public boolean mkdir()
      1) 创建目录,成功返回 true。这只能创建一个文件夹,要求所有的         父目录都存在,否则创建失败,返回 false 。
       2) 如果要创建的目录已存在,返回 false。
    ii. public boolean mkdirs()
      1) 创建目录,成功返回 true。会创建所有不存在的父目录。
      2) 如果要创建的目录已存在,返回 false。
  3.4). 获取文件信息

String getName()  获取文件的名称
boolean canRead() 判断文件是否是可读的
boolean canWrite() 判断文件是否可被写入
Boolean canExecute() 测试应用程序是否可以执行此抽象路径名表示的文件。
boolean exits() 判断文件长度是否存在
int length()  获取文件的长度(以字节为单位)
String getAbsolutePath()  获取文件的绝对路径
String getParent() 获取文件的父路径
boolean isFile() 判断此抽象路径名表示的文件是否为普通文件
boolean isDirectory() 判断此抽象路径名表示的是否是一个目录
boolean isHidden 判断文件是否是隐藏文件
long lastModified() 获取文件最后修改时间
boolean createNewFile() 当且仅当具有该名称的文件尚不存在时,原子地创 建一个由该抽象路径名命名的新的空文件。
boolean delete() 删除由此抽象路径名表示的文件或目录。
File[] listFiles() 返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件。
String[] list() 返回一个字符串数组,命名由此抽象路径名表示的目录中的文件和目录。
boolean mkdirs() 创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录。可创建多层文件包                
public File getParentFile​() 返回此抽象路径名的父级的抽象路径名
boolean mkdir() 创建由此抽象路径名命名的目录。只能创建一层文件包
boolean setReadOnly() 标记由此抽象路径名命名的文件或目录,以便只允许读取操作。
boolean setWritable(boolean writable) 一种方便的方法来设置所有者对此抽象路径名的写入权限。

本表格是在线创建表格网站完成,感觉不错
  测试3:

public static void main(String[] args) throws IOException {
    File file = new File("D:/demo/demo.txt");
    if (file.exists()) {
        System.out.println("是普通文件?"+ file.isFile()); //文件加不算普通文件
        String name = file.getName();
        long length = file.length();
        System.out.println("文件名称:" + name);
        System.out.println("长度:"+length);
        System.out.println("文件是否可读:"+file.canRead());;
        System.out.println("文件是否可以写入:"+file.canWrite());
        System.out.println("文件的父路径:" + file.getParent());
//          file.delete(); //删除文件
    } else {
        System.out.println(file.createNewFile());
    }
}

三:流操作

 1. 数据流
   1.1)分类:
     (1)字节流:InputStream(字节输入流)、OutputStream(字节输出流)
     (2)字符流:Reader(字符输入流)、Writer(字符输出流)

 2. OutputStream
   2.1)类定义:public abstract class OutputStream extends Object implements Closeable, Flushable 
   OutputStream 类是一个抽象类,抽象类不能实例化。所以应该实例化 OutputStream 类的子类,FileOutputStream。利用子类的构造方法来实现。
   Java SE 9 & JDK 9
   2,2)方法:

public void close​() throws IOException 关闭此输出流并释放与此流相关联的任何系统资源。
public void flush() throws IOException 刷新此输出流并强制任何缓冲的输出字节被写出。
public void write(byte[] b, int off, int len) throws IOException 从指定的字节数组写入len字节,从偏移off开始输出到此输出流。
public void write(byte[] b) throws IOException 将b.length字节从指定的字节数组写入此输出流。
public abstract void write(int b) throws IOException 将指定的字节写入此输出流。

 3. FileOutputStream类
   3.1)UML图例
   
  (1)public class FileOutputStream extends OutputStream
  (2)构造方法:FileOutputStream​(File file)
  (3)其覆写方法功能和父类一样。
 
  3.2) 测试:
 
   在文件中写入文本

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("D:"+ File.separator + "hello" + File.separator + "my.txt");

        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        OutputStream output = new FileOutputStream(file);
        String msg = "hfsljfsfusfsfsofh";
        byte data[] = msg.getBytes(); //由于FileOutputStream 方法write(byte[])
        for (int x = 0; x < data.length; x ++) {
            output.write(data[x]); //写入文件中
        }
        output.close();
    }
}

  追加文字,则 OutputStream output = new FileOutputStream(file);
        改为:OutputStream output = new FileOutputStream(file,true);
但是添加的文字是直接从文本末尾添加,要想换行的话,加入: \r\

4.InputStream类
  4.1)定义:public abstract class InputStream extends Object implements          Closeable
  4.2)方法:
    4.2.1)public abstract int read​() throws IOException 从输入流读取数据的下        一个字节。
   4.2.2)public int read(byte[] b)throws IOException 从输入流读取一些字        节数,并将它们存储到缓冲区b
   4.2.3)public int read(byte[] b, int off, int len)throws IOException从输入        流读取len字节的数据到一个字节数组。尝试读取多达len个字节,但
       可以读取较小的数字。实际读取的字节数作为整数返回。
 4.3)FileInputStream 类
   4.3.1)构造方法:FileInputStream​(File file)
   4.3.2)UML图例
       
  4.3,3)测试:

        public static void main(String[] args) throws IOException {
            //找到文件
            File file = new File("D:"+ File.separator + "hello" + File.separator + "my.txt");
            //实例化 InputStream 类对象
            InputStream input = new FileInputStream(file);
            //实现数据的读取操作
            byte b[] = new byte[1024];
    //      int len = input.read(b);
    //      System.out.println("读取的内容:"+new String(b,0,len));
    //      input.close(); 
            //这里时单个字节输入
            int foot = 0;
            int temp = 0;
            while ((temp=input.read()) != -1) {
                b[foot++] = (byte)input.read();
            }
            System.out.println(new String(b,0,foot));
            input.close();
}

   在测试单个字节输入时,遇到了字符缺失和乱码的现象,还在解决中,如果有大佬知道,请指教。
 
5.Writer类
 1)定义:public abstract class Writer extends Object implements Appendable, Closeable, Flushable
   UML 图例
  
 2.)主要方法:
    2.1)public void write​(String str) throws IOException
 3.)FileWriter 类
   3.1)构造方法:FileWriter​(File file)
   3.2)其他方法:
    1. append, write, write (Methods inherited from class java.io.Writer)
    2.clone, equals, getClass (Methods inherited from class java.lang.Object)
 4.)测试:
    输出内容到文件中

public static void main(String[] args) throws IOException {
    File file = new File("D:"+File.separator+"demo.txt");
    FileWriter out = new FileWriter(file,true);
    String a = "hello, 你好!";
    out.write(a);
    out.close();
}

6.Reader类
 1.)定义:public abstract class Reader extends Object implements Readable,         Closeable
  UML 图例:
    
 2.)主要方法:
    2.1)public int read​(char[] cbuf) throws IOException
 3.)FileReader类
   3.1)构造方法:public FileReader​(File file) throws FileNotFoundException
   3.2)其他方法:
    mark, markSupported, read, read, reset, skip(Methods inherited from class     java.io.Reader)
 4.)测试:
   从文本中读取前50个字符串

public static void main(String[] args) throws IOException {
    File file = new File("D:"+File.separator+"demo.txt");
    Reader in = new FileReader(file);
    char [] cbuf = new char[50];
    in.read(cbuf);
    System.out.println(new String(cbuf));
    in.close();
}

猜你喜欢

转载自blog.csdn.net/qq_37131037/article/details/80756064