java-io流

FileInputStream(重点)
package javase.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
 * java.io.FileInputStream:
 *   1.文件字节输入流,万能的,任何类型的文件都可以采用这个流来读
 *   2.字节的方式,完成输入的操作,完成读的操作(硬盘-->内存)
 *
 *   分析程序的缺点:
 *   一次只能读取一个字节,内存和硬盘交互太频繁了
 */
public class FileInputStreamTest01 {
    public static void main(String[] args) {
        FileInputStream fis=null;
        // 创建字节输入流对象
        try {
            fis=new FileInputStream("D:\\editplus\\java代码\\temp");
            // 开始读
            int readData=0;
            while((readData=fis.read())!=-1){
                System.out.println(readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 在finally语句中确保流一定要关闭,条件是流不能为空
            if (fis != null) {
                try{
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }

    }
}

如何进行读文件???
package javase.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamTest02 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=null;
        try {
            fis=new FileInputStream("temp");
            // 准备一个byte数组
//            byte[] bytes=new byte[4];
//            while (true){
//                int readCount=fis.read(bytes);
//                if (readCount==-1){
//                    break;
//                }
//                // 将byte数组中的转换为字符串,读到多少个就转换多少个
//                System.out.println(new String(bytes,0,readCount));
//            }
            // 循环改造
            byte[] bytes=new byte[4];
            int readCount=0;
            while((readCount=fis.read(bytes))!=-1)
            {
                System.out.println(new String(bytes,0,readCount));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fis != null) {
                    fis.close();

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

aviable()和skip()方法的使用
package javase.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
 int	available()
返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞

long	skip(long n)
跳过并从输入流中丢弃 n字节的数据
 */
public class FileInputStreamTest03 {
    public static void main(String[] args) {
        FileInputStream fis=null;
        try {
            fis=new FileInputStream("temp");
            System.out.println("总字节数量:"+fis.available());
            // 得到的就是总字节数量  这样就不用使用循环来读取字节
            //byte[] bytes=new byte[fis.available()]; // 这种方式不能适合大文件 因为byte[] 数组不能太大
            //int readCount=fis.read(bytes);
            //System.out.println(new String(bytes));


            // skip 跳过几个字节不读取
            fis.skip(3);
            System.out.println(fis.read());


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

            }
        }
    }
}

FileOutputStream(重点)
package javase.bjpowernode.java.io;

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

/*
 * 文件戒子输出流,负责写
 * 从内容到硬盘
 */
public class FileOutputStreamTest01 {
    public static void main(String[] args) {
        FileOutputStream fos=null;
        try {
            // 这种方式会先将原文件清空 然后再写入
            // fos=new FileOutputStream("myfile");
            fos=new FileOutputStream("myfile",true); // 后面加上一个true 就表示再源文件后面追加写入
            //开始写
            byte[] bytes={97,98,99,100};
            //fos.write(bytes); // 将byte[] 数组全部写出
            // 将byte[] 数组的一部分写出
            fos.write(bytes,0,2);
            
            
            String s="我是一个中国人,我骄傲";
            // 将字符串转换为byte数组
            byte[] bs=s.getBytes();
            fos.write(bs);

            // 写完之后,最后一定要刷新
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try{
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
}

文件的拷贝
package javase.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * FileInputStream 和FileOutputStream的使用
 * 文件的拷贝
 */
public class Copy01 {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            // 创建输入流
            fis=new FileInputStream("D:\\001\\take.m4a");
            // 创建输出流
            fos=new FileOutputStream("D:\\002\\take.m4a");
            // 最核心的就是 接下来就是一边读一边写
            byte[] bytes=new byte[1024*1024];
            // 然后就是都多少写多少的问题
            int readCount=0;
            while ((readCount=fis.read(bytes))!=-1){
                fos.write(bytes,0,readCount);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 分开try 不要一起try
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

}

FileRreader
package javase.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * FileInputStream 和FileOutputStream的使用
 * 文件的拷贝
 */
public class Copy01 {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            // 创建输入流
            fis=new FileInputStream("D:\\001\\take.m4a");
            // 创建输出流
            fos=new FileOutputStream("D:\\002\\take.m4a");
            // 最核心的就是 接下来就是一边读一边写
            byte[] bytes=new byte[1024*1024];
            // 然后就是都多少写多少的问题
            int readCount=0;
            while ((readCount=fis.read(bytes))!=-1){
                fos.write(bytes,0,readCount);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 分开try 不要一起try
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

}

进行遍历
package javase.bjpowernode.java.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest02 {
    public static void main(String[] args) {
        FileReader fir=null;
        try {
            fir=new FileReader("myfile");
            char[] chars=new char[4];
            fir.read(chars); // 按照字符的方式去读取
            // 进行遍历
            for (char c:chars)
            {
                System.out.println(c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fir != null) {
                try{
                    fir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

FileWriter
package javase.bjpowernode.java.io;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 FileWriter:
 文件字符输出流,写
 只能输出普通文本
 */
public class FileWriterTest01 {
    public static void main(String[] args) {
        FileWriter fiw=null;
        try {
            fiw=new FileWriter("filewriter");
            // 开始写
            char[] chars={'我','是','中','国','人'};
            //fiw.write(chars);
            fiw.write(chars,2,3);
            // 记得刷新
            fiw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fiw != null) {
                try{
                    fiw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

FileReader和FileWriter联合使用进行拷贝
package javase.bjpowernode.java.io;
/*
 使用FileReader和FileWriter进行拷贝
 */

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Copy02 {
    public static void main(String[] args) {
        FileReader fir = null;
        FileWriter fiw = null;

        try {
            // 创建输入流对象
            fir = new FileReader("D:\\001\\niaho");
            // 创建输出流对象
            fiw = new FileWriter("D:\\002\\niaho");
            // 开始拷贝
            char[] chars=new char[4];// 一次拷贝四个字节
            int readCount=0;
            while ((readCount=fir.read(chars))!=-1){
                fiw.write(chars,0,readCount);
            }
            // 刷新
            fiw.flush();
        } catch (Exception e) {
            e.printStackTrace();

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

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

            }
        }
    }
}

BufferReader
package javase.bjpowernode.java.io;

import java.io.BufferedReader;
import java.io.FileReader;

/*
 BufferReader:
 带有缓冲区的字符输入流,
 使用这个流的时候不需要自定义char数组,或者说不要自定义byte[]
 */
public class BufferReaderTest01 {
    public static void main(String[] args) throws Exception {
        // 创建对象
        FileReader reader=new FileReader("myfile");
        // 当一个构造方法中需要传进来一个流时,这个被传进来的流叫做:节点流
        //外部负责包装的这个流 叫做:处理流
        // 像当前这个程序来说:FileReader是一个节点流
        BufferedReader br=new BufferedReader(reader);
        // 读一行 br.readLine();
        String s=null;
        // br读取一个文本行 不带换行符
        while ((s=br.readLine())!=null){
            System.out.println(s);
        }


        // 我们关闭也只需要关系最外层的流就行了
        br.close();


    }
}

将字节字符流之间的转换
package javase.bjpowernode.java.io;

import java.io.*;

/*
 * 将字节字符流之间的相互转换
 */
public class BufferReaderTest02 {
    public static void main(String[] args) throws IOException {
        // 创建对象
            // 字节流
            FileInputStream fis=new FileInputStream("myfile");
            // InputStreamReader的使用
            InputStreamReader reader=new InputStreamReader(fis);
            // 这个构造方法只能传一个字符流,不能传字节流,所以需要转换
            BufferedReader br=new BufferedReader(reader);
            String s=null;
            while ((s=br.readLine())!=null){
                System.out.println(s);
            }
            br.close();
    }
}

BufferWriter
package javase.bjpowernode.java.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/*
 * BufferedWriter:
 * 缓冲字符流
 */
public class BufferWriterTest01 {
    public static void main(String[] args) throws IOException {

        // 创建对象
        FileWriter fiw=new FileWriter("myfile12");
        BufferedWriter out=new BufferedWriter(fiw);
        // 开始写
        out.write("hello world");
        out.write("\n");
        out.write("hello kitty");
        // 刷新
        out.flush();
        // 关闭
        out.close();
    }
}

PrintStream (将输出在控制台的文件输出到对应的文件)
package javase.bjpowernode.java.io;

import java.io.FileNotFoundException;
import java.io.PrintStream;

/*
java.io.PrintStream:标准输出流  默认输出到控制台
 */
public class PrintStreamTest {
    public static void main(String[] args) throws Exception {
        System.out.println("hello world");
        PrintStream ps=System.out;
        ps.println("hekko");
        ps.println("hello lisi");

        // 标准输出流不需要关闭

        // 可以使标准输出流不输出到控制台吗?
        PrintStream printStream=new PrintStream("log");
        // 修改输出方向,将输出方向修改到log文件中
        System.setOut(printStream);
        System.out.println("hello world");
        System.out.println("hello kitty");
        System.out.println("hello zhangsan");
    }
}

日志工具
package javase.bjpowernode.java.io;



import javax.xml.crypto.Data;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 日志工具
 */
public class Logger {
    // 记录日志的测试类

    public static void log(String msg) throws Exception {
        // 标准输出流指向一个日志文件
        PrintStream printStream=new PrintStream(new FileOutputStream("log.txt",true));
        // 改变输出方向
        System.setOut(printStream);
        // 时间格式化
        Date nowTime= new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String strTime=sdf.format(nowTime);

        System.out.println(strTime+":"+msg);


    }
}

package javase.bjpowernode.java.io;

public class LogTest {
    public static void main(String[] args) {
        // 测试工具类是否好用
        try {
            Logger.log("调用了System类的gc方法,建议启动垃圾回收器");
            Logger.log("调用了UserService的doSome方法");
            Logger.log("用户尝试进行登录,验证失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

File方法
package javase.bjpowernode.java.io;

import java.io.File;
import java.io.IOException;

/*
 File类和四大家族没有关系 所以不能读和写
 */
public class FileTest01 {
    public static void main(String[] args) throws IOException {
        // 创建一个file对象
        File f1=new File("F:\\file");
        // 判断file是否存在
        System.out.println(f1.exists());
        // 如果F:\\file中的文件不存在,则以文件的形式创建出来
//        if (!f1.exists()){
//            // 以文件的形式创建
//            f1.createNewFile();
//        }

//        if (!f1.exists()){
//            // 以目录的形式创建出来
//            f1.mkdir();
//        }

        // 可以创建多重目录吗???
        File f2=new File("F:\\a\\b\\c\\d\\e\\f");
        if (!f2.exists()){
            // 以多重目录创建
            f2.mkdirs();
        }

        File f3=new File("Q:\\素材库\\阴阳师作业图.png");
        // 获取文件的父路径
        String ParentPath=f3.getParent();
        System.out.println(ParentPath);

        File ParentFile=f3.getParentFile();
        System.out.println(ParentFile);

        // 获取绝对路径
        System.out.println(ParentFile.getAbsoluteFile());



        File f4=new File("log");
        System.out.println("绝对路径"+f4.getAbsolutePath());



    }
}

package javase.bjpowernode.java.io;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileTest02 {
    public static void main(String[] args) {
        File f1=new File("Q:\\idm 下载地\\idm音乐\\Fractures.m4a");
        // 获取文件名
        System.out.println("文件名:"+f1.getName());
        // 判断是否是一个目录
        System.out.println("是否为目录:"+f1.isDirectory());// false
        // 判断是否是一个文件
        System.out.println("是否为文件:"+f1.isFile());// true
        // 获取文件修改的最后时间
        System.out.println("文件最后的修改时间是:"+f1.lastModified());// 返回值是一个long类型,这个毫秒是1970年到现在的总毫秒数
        // 将总毫秒数转换为日期
        Date time =new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String str=sdf.format(time);
        System.out.println(str);
        // 获取文件的大小
        System.out.println(f1.length());
    }
}

ListFiles方法专题
package javase.bjpowernode.java.io;

import java.io.File;

/*
 * File中ListFiles方法
 */
public class ListFileTest {
    public static void main(String[] args) {
        // File[] listFiles()
        // 获取当前目录下所有的子文件
        File f=new File("Q:\\idm 下载地");
        File[] ff=f.listFiles();
        for (File file:ff){
            System.out.println(file.getAbsoluteFile());
        }
    }

}

序列化
package javase.bjpowernode.java.io;

import javase.bjpowernode.java.bean.Student;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

/*
 * ObjectOutputStream:
 *  java.io.NotSerializableException: javase.bjpowernode.java.bean.Student
 * 这个类不能序列化
 *
 * 参与序列化和反序列化的对象 必须实现Serializable接口
 *  注意:通过源代码发现,Serializable接口只是一个标志接口
 *  public interface Serializable {}
 * 这个接口中什么代码都没有
 * 那么这个接口起到什么作用???
 *   起到一个标识的作用,标志的作用,java虚拟机看到这个类实现了这个接口,可能会对这个类进行特殊的待遇
 *   java虚拟机看到这个会自动生成一个序列化版本号
 *   序列号版本号有什么用???
 *    
 */
public class ObjectOutputStreamTest01 {
    public static void main(String[] args) throws Exception {
        // 创建java对象
        Student student=new Student();
        // 序列化
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("students"));
        // 序列化对象
        oos.writeObject(student);
        // 刷新
        oos.flush();
        // 关闭
        oos.close();
    }
}

序列化多个对象
package javase.bjpowernode.java.io;

import javase.bjpowernode.java.bean.User;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/*
 可以一次性序列化多个对象吗??
 可以,可以将对象放到集合当中,序列化集合

 */
public class ObjectOutputStreamTest02 {
    public static void main(String[] args) throws Exception{
        List<User> userList=new ArrayList<>();
        userList.add(new User(1,"zhangsan"));
        userList.add(new User(2,"lisi"));
        userList.add(new User(3,"wangwu"));
        // 开始序列化对象
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("users"));
        // 形成序列化集合
        oos.writeObject(userList);
        // 刷新
        oos.flush();
        // 关闭
        oos.close();
    }
}

将序列化集合反序列化
package javase.bjpowernode.java.io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

/*
 * 将序列化集合反序列化
 */
public class ObjectInputStreamTest02 {
    public static void main(String[] args) throws Exception{
        // 创建序列化对象
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("users"));
        // 开始序列化
        Object obj=ois.readObject();
        System.out.println(obj);
        // 关闭
        ois.close();
    }
}

IO和Properties联合使用
package javase.bjpowernode.java.io;

import java.io.FileReader;
import java.util.Properties;

/*
 IO和Properties的联合使用
   以后经常需要改变的数据,我们就可以写到一个文件当中,使用程序动态更新文件
   将来只要修改文件的内容,java代码不需要改动,服务器也不需要重启,就可以拿到动态的信息
   
   类似以上机制(文件)叫做配置文件
   并且配置文件中的内容格式是:
   key1=value
   key2=value
   
   java中规范:属性配置文件要以properties后缀结尾,但不是必须的
   其中Properties这个类是专门来存放属性配置文件
 */
public class IoPropertiesTest01 {
    public static void main(String[] args) throws Exception{
        // 将硬盘上的文件放到Map集合当中
        // 从硬盘到内存  是输入
        // 创建一个输入流对象
        FileReader reader=new FileReader("chapter\\userinfo");
        // 新建一个Map集合
        Properties pro=new Properties();
        // 调用Properties对象中的方法load数据加载到Map集合中
        pro.load(reader); // 文件中的数据顺着管道加载到Map集合中,其中等号=左边做key,右边做value

        // 通过key来过去value
        String username=pro.getProperty("username");
        System.out.println(username);
        String password=pro.getProperty("password");
        System.out.println(password);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值