第八章 IO

File类

File类是java.io包中很重要的一个类 File类的对象可以表示文件,还可以表示目录,在程序中一个File类对象可以代表一个文件或目录 File对象可以对文件或目录的属性进行操作,如:文件名、最后修改日期、文件大小等 File对象无法操作文件的具体数据,即不能直接对文件进行读/写操作

File类的构造方法

//在当前目录下创建一个与aaa.txt文件名相关联的文件对象
File f1 = new File("aaa.txt");
//指明详细的路径以及文件名,请注意双斜线或用反斜杠
File f2 = new File("D:\\Java\\Hello.java");
//指明详细的路径以及目录名,请注意双斜线
File f3 = new File("D:\\Java");
File file = new File("D:/demo.txt");
        File file1 = new File("D:/","demo.txt");
        System.out.println(file.isFile());//测试此抽象路径名表示的文件是否为普通文件。
        System.out.println(file.canRead());//测试应用程序是否可以读取由此抽象路径名表示的文件。
        System.out.println(file.canWrite());//测试应用程序是否可以修改由此抽象路径名表示的文件。
        System.out.println(file.exists());//测试此抽象路径名表示的文件或目录是否存在。
        System.out.println(file.getAbsoluteFile());//返回此抽象路径名的绝对形式。
        System.out.println(file.getAbsolutePath());//返回此抽象路径名的绝对路径名字符串。
        System.out.println(file.getParent());//返回此抽象路径名的父 null的路径名字符串,如果此路径名未命名为父目录,则返回null。
        System.out.println(file.isAbsolute());//测试这个抽象路径名是否是绝对的。
        System.out.println(file.isHidden());//测试此抽象路径名命名的文件是否为隐藏文件。
        System.out.println(file.isDirectory());//测试此抽象路径名表示的文件是否为目录。
        System.out.println(file.length());//返回由此抽象路径名表示的文件的长度(字节)。  英文1个字节,中文三个
        System.out.println(file.lastModified());//返回此抽象路径名表示的文件上次修改的时间。  返回一个long值
        System.out.println(new Date(file.lastModified()));//用Date形式输出时间

输入及输出的概念

把电脑硬盘上的数据读到程序中,称为输入,即input,进行数据的read操作

从程序往外部设备写数据,称为输出,即output,进行数据的write操作

字节流与字符流

按照流的走向,分为:输入流,输出流

按照流的读取单位分为:

字节流(以原始字节为单位:音频、图片、歌曲等)

字节输入流 FileInputStream

字节输出流 FileOutputStream

字符流(例如txt文件、以单个字符为单位)

字符输入流 FileReader

字符输出流 FileWriter

根据封装类型不同流又分为:节点流,处理流

节点流:如果流封装的是某种特定的数据源,如文件、字符串、字符串数组等, 则称为节点流

字节输入流 FileInputStream

字节输出流 FileOutputStream

字符输入流 FileReader

字符输出流 FileWriter

处理流:如果流封装的是其它流对象,称为处理流。 处理流提供了缓冲功能,提高读写效率,同时增加了一些新的方法。

缓冲字节输出流 BufferedOutputStream

缓冲字节输入流 BufferedInputStream

缓冲字符输入流 BufferedReader

缓冲字符输出流 BufferedWriter

字节流

 public static void main(String[] args) throws IOException {
        File file = new File("D:/demo.txt");
        FileInputStream in = new FileInputStream(file);
        /*for (int i = 0; i < file.length(); i++) {//读取
            System.out.println(in.read());//从文件中每read一次,就输出一个字节
        }*/
        FileOutputStream out = new FileOutputStream("D:/demo1.txt");
        for (int i = 0; i < file.length(); i++) {
            out.write(in.read());
        }
​
        in.close();
        out.close();
    }
   
 public static void main(String[] args) {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream("D:/demo.txt");
            out = new FileOutputStream("D:/demo1.txt");
            int b=0;
            while ((b=in.read()) != -1){//每次读取一个字节并返回,当文件内容读完以后会返回一个-1
                out.write(b);
            }
            byte [] b1 = new byte[100];
            int length = 0;
            //read(byte[]b1)  每次最多读byte数组长度个字节,返回数组中实际表长的字节个数  读完返回-1
            while ((length = in.read(b1)) != -1){
                out.write(b1,0,length);//每次向外写一个byte个字节,从指定位置(一般情况下从0开始)向外写length个长度
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件找不到");
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("读写异常");
        } finally {
                try {
                    if (in!=null) {
                        in.close();
                    }
                    if (out!=null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

字符流

public class FileReaderDemo1 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:/demo.txt");
        FileWriter fw = new FileWriter("D:/demo1.txt");
        int c = 0;
​
        while ((c=fr.read())!=-1){
            System.out.println(c);
        }
         char [] ch = new char[10];
        int length = 0;
        
        while ((length=fr.read(ch))!=-1){
            fw.write(ch,0,length);
        }
        fr.close();
        fw.close();
    }
}
 
  public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:/demo.txt");
        BufferedReader bfr = new BufferedReader(fr);
        FileWriter fw = new FileWriter("D:/demo1.txt",true);//
        BufferedWriter bfw = new BufferedWriter(fw);
        String line = null;//一次读入一行数据
        while ((line = bfr.readLine())!=null){
            bfw.write(line);//一次写出一行数据
            bfw.newLine();//换行
        }
        fr.close();
        fw.flush();
        fw.close();
    }

Print流

        /*
          PrintWriter 打印字符流  单项输出  用于只从服务器端向客户端输出内容
        */
​
        PrintWriter p = new PrintWriter("F:/demo.html");
                    p.println("<a href=''>百度</a>");
                    p.println("<h1>一级标题</h1>");
                    p.println("<a href=''>百度</a>");
​
                    p.close();

对象输入输出流

对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息。 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 。

对象的输出流: ObjectOutputStream

对象的输入流: ObjectInputStream

要将序列化之后的对象保存下来,需要通过对象输出流(ObjectOutputStream) 将对象状态保存,之后再通过对象输入流(ObjectInputStream)将对象状态恢复

在ObjectInputStream 中用readObject()方法可以直接读取一个对象

ObjectOutputStream中用writeObject()方法可以直接将对象保存到输出流中

public class ObjectOutputStreamDemo {
      public static void main(String[] args) throws IOException {
        //案例:将程序运行时,那一刻的时间保存到文件中(直接将对象信息存起来——对象实例化)
        Date date = new Date();
        FileOutputStream out = new FileOutputStream("D:/date.txt");
        ObjectOutputStream objectout = new ObjectOutputStream(out);
        objectout.writeObject(date);
        objectout.close();
    }
}
      public class ObjectInputStreamDemo {
      public static void main(String[] args) throws IOException, ClassNotFoundException {
        FileInputStream  in = new FileInputStream("D:/date.txt");
        ObjectInputStream objectin = new ObjectInputStream(in);
        Object obj = objectin.readObject();
        if (obj instanceof Date){
            Date date = (Date)obj;
            System.out.println(date);
        }
        objectin.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值