泛型 File对象 和IO流

目录

1.泛型

2.File对象

3.IO流

1泛型

1.1什么是泛型

泛型就是限制我们的数据类型

1.2为什么使用泛型

ArrayList arrayList = new ArrayList();
arrayList.add("java01");
arrayList.add("张三");
//获取元素 需要进行强制类型转换
String a = (String) arrayList.get(0);
//获取元素时不方便对元素进行相应的其他操作
System.out.println(a);

1.3如何使用泛型

List<类型> list=new List<类型>{} 只能在该集合中存储指定的类型       

//限制集合中每个元素的类型
ArrayList<String> s= new ArrayList<>();
//集合中只能添加String类型
s.add("java01");
s.add("张三");
//在获取元素时 默认就是获取相应的类型 无需进行转换
String s1 = s.get(0);
System.out.println(s1);
//<k,v> k表示键的泛型 V表示值的泛型
HashMap<String,Integer> hashMap=new HashMap<>();
hashMap.put("张三",18);
hashMap.put("李四",19);
Set<String> keys = hashMap.keySet();

1.4能否自己定义泛型类

public class<标识,标识,....>{

        标识 变量名;

        public 标识 方法名(){

}

        public void 方法名(标识 参数){

}

}

例子

public static void main(String[] args) {
        //在创建泛型类对象时 没有指定相应的泛型类型默认为Object类型
        Coordinate coordinate = new Coordinate();
        coordinate.setX("java01");
        coordinate.setX(12);
        //这里的泛型都是对象类型
        Coordinate<Integer> coordinate1 = new Coordinate<>();
        coordinate1.setX(34);
        coordinate1.setY(143);
        Integer x = coordinate1.getX();
        Integer y = coordinate1.getY();
        System.out.println(x);
        System.out.println(y);
        coordinate1.show();

    }
}
     class Coordinate<T>{
        private T x;
        private T Y;
        public void show(){
            System.out.println("X坐标为"+this.x+"Y坐标为"+this.Y);
        }

        public Coordinate() {
        }

        public Coordinate(T x, T y) {
            this.x = x;
            Y = y;
        }

        public T getX() {
            return x;
        }

        public void setX(T x) {
            this.x = x;
        }

        public T getY() {
            return Y;
        }

        public void setY(T y) {
            Y = y;
        }

        @Override
        public String toString() {
            return "coordinate{" +
                    "x=" + x +
                    ", Y=" + Y +
                    '}';
        }

2File对象

2.1File的介绍

file类就是当前系统中文件或者文件夹的抽象表示

通俗的讲就是使用file对象来操作我们电脑系统中文件或者文件夹

学习file类其实就是学习通过file对象对系统中文件或者文件夹进行增删改查

2.2创建File对象

磁盘中准备个Fon目录以后对文件的操作都要放在该目录下(防止误删)

//创建一个文件对象 并指定文件路径 \转义字符 例如\t
// \\目录层级 在window系统下层级分割符为\ 但是在Linux和mac分割符为 /
//我们的java代码是一种跨平台语言 我们的开发在window 项目部署在Linux我们
//使我们的代码在任意系统下来用 我们有两种解决办法

File file01 = new File("D:\\Fon\\aaa.text");
//第一种因为window兼容/ 和 \
File file02 = new File("D:/Fon/bbb.text");
//第二种 file.separator根据代码所在系统自动获取响应的分割符
System.out.println(File.separator);
File file03 = new File("D:" + File.separator + "Fon" + File.separator + "ccc.text");

2.3增加

//创建File对象
File file01 = new File("D:/Fon/aaa.text");
//创建相应的文件
file01.createNewFile();
File file02 = new File("D:/Fon/bbb");
//创建目录 make directory(单层目录)
file02.mkdir();
File file03 = new File("D:/Fon/eee/fff");
//创建多层目录
file03.mkdirs();
File file04 = new File("D:/Fon/ggg");
file04.createNewFile();

2.4删除

File file01 = new File("D:/Fon/aaa.text");
//删除文件
file01.delete();
File file02 = new File("D:/Fon/ggg");
//程序结束后删除
file02.deleteOnExit();
Thread.sleep(10000);
File file03 = new File("D://Fon/bbb");
//删除空目录
file03.delete();

2.5修改

public static void main(String[] args) throws IOException {
    File file = new File("D:/Fon/eee.text");
    file.createNewFile();
    //设置文件权限为不能读
    boolean b1 = file.setReadable(false);
    //设置文件不能写
    boolean b = file.setWritable(false);
    //设置文件只读
    boolean b2 = file.setReadOnly();
    System.out.println(b2);
    System.out.println(b);
    System.out.println(b1);
    file.renameTo(new File("D:/Fon/hhh.text"));
}

2.6查询

public static void main(String[] args) {
    File file = new File("D:/Fon/qqq/www");
    file.mkdirs();
    //获取文件的名称
    String name = file.getName();
    System.out.println("name "+name);
    //得到父级路径的名称
    String parent = file.getParent();
    System.out.println("parent "+parent);
    //的到文件的路径名称
    String path = file.getPath();
    System.out.println("path "+path);
    //判断该文件对象是否为文件类型
    boolean file1 = file.isFile();
    System.out.println(file1);
    //判断该文件对象是否为目录类型
    boolean directory = file.isDirectory();
    System.out.println(directory);
    //列出Fon目录下所有子文件的名称
    File file2 = new File("D:/Fon");
    String[] list = file2.list();
    System.out.println(Arrays.toString(list));
    //列出Fon目录下所有文件对象
    File[] files = file2.listFiles();
    System.out.println(files);
    for (File f:files){
        System.out.println(f.toString());
    }
}

经典的题目方法递归调用

//显示指定目录下的所有文件
public class Recursion {
    public static void main(String[] args) {
        ShowAllFiles("D:/Fon");
    }

    public static void ShowAllFiles(String path) {
        //使用传入的文件路径构建文件对象
        File file = new File(path);
        //判断文件是否存在或是否为目录
        if (!file.exists()||!file.isDirectory()){
            return;
        }
        //列出该目录下所有的文件对象
        File[] files = file.listFiles();
        for (File f:files){
            //判断是否是目录
            if (f.isDirectory()){
                //Dir 代表目录结构  输出目录
                System.out.println(f.getPath()+"<Dir>");
                //继续调用本方法
                ShowAllFiles(f.getPath());

            }else {
                System.out.println(f.getPath()+" ");
            }
        }
    }

3 IO流

IO表示两个单词的缩写

I:Input 输入 O:Output 输出

IO流的作用:就是对文件中的内容进行操作

输入:读操作(读取文件的内容)  输出:写操作(往文件中写内容)

IO流的分类:

(1)根据流的方向:

---输入流:程序可以从中读取数据的流。

---输出流:程序能向其中写入数据的流

(2)根据流的单位

---字节流 以字节为单位传输数据的流

---字符流 以字符为单位传输数据的流

(3)根据功能

---节点流 直接和文件进行交互

---处理流 不直接作用在文件上

四个基本的流: 其他的流都是在这四个流的基础上进行扩展的

                字节输入流

                字节输出流

                字符输入流

                字符输出流

3.1Writer字符输出流

它是所有字符输出流的根类-----FileWriter

public static void main(String[] args) throws IOException {
    //字符输出流----对指定文件(路径)进行操作
    FileWriter fileWriter = new FileWriter("D:/Fon/aaa.text");
    String str="hello aaa 今天开演唱会";
    fileWriter.write(str);
    //刷新流
    fileWriter.flush();
    //关闭资源流
    fileWriter.close();
}

上面每次往文件中写内容时 就会把原来的内容给覆盖了,如何追加内容。

public static void main(String[] args) throws IOException {
    //字符输出流----对指定文件(路径)进行操作
    //true:表示追加内容到文件中
    FileWriter fileWriter = new FileWriter("D:/Fon/aaa.text",true);
    String str="hello aaa 今天开演唱会";
    String str1="今天请来了很多明星";
    fileWriter.write(str);
    fileWriter.write(str1);
    //刷新流
    fileWriter.flush();
    //关闭资源流
    fileWriter.close();
}

3.2字符输入流

它是所有字符输入流的根类 它的实现类还有很多,我们使用我们使用FileReader实现类

public static void main(String[] args) throws IOException {
    FileReader fileReader = new FileReader("D:/Fon/aaa.text");
    //表示读取的个数
    int count=0;
    //每次读取到的内容存储到该数组中 (每次只能读取一个字符效率慢,可以一次读取多个字符并存入一个字符数组中)
    char[] chars = new char[10];
    while ((count= fileReader.read(chars))!=-1){
        //把字符数组改为字符串 从0开始读count个
        String str=new String(chars,0,count);
        System.out.println(str);
    }
}

完成文件内容的复制

要求D:/Fon/d.text复制到C:/Fon/d.txt

@Test
public void test01() throws IOException {
    //创建一个字符输入流 (读取文件的内容)
    FileReader fileReader = new FileReader("D:/Fon/aaa.text");
    //创建字节输出流 (往文件中输入内容)
    FileWriter fileWriter = new FileWriter("C:/AAA/bbb.text");
    //读取到字符的个数
    int s=0;
    //每次读取到的内容放到数组中
    char[] chars = new char[10];
    //文件读取的内容放到数组中直到读取不到数据
    while ( (s=fileReader.read(chars) )!=-1){
        //将读取到的内容输入到文件中
        fileWriter.write(chars,0,s);
        //刷新流
        fileWriter.flush();
    }
    //关闭流
    fileWriter.close();
    fileReader.close();
}

字符流只能对文本操作 视频 图片 电影 压缩文件 这些都属于二进制

3.3字节输出流 outputStream

它可以对任意文件进行操作,对文件进行输出操作,以字节为单位。它是所有字节输出流的父类,

子类FileoutputStream

@Test
public void test01() throws IOException {
    OutputStream os=new FileOutputStream("D:/Fon/aaa.text");
    //将字符串转为字节数组
    String str="abcdef";
    byte[] bytes = str.getBytes();
    os.write(bytes);
    os.flush();
    os.close();

}

3.4字节输入流

它可以对任意文件进行操作,以字节为单位,它是所有字节输入流的父类,子类有FileInputStream

@Test
public void TestInputStream() throws IOException {
    InputStream is=new FileInputStream("D:/Fon/aaa.text");
    //一次读取三个 并把读取到的内容存储到数组中 返回读取的个数
    byte[] bytes = new byte[3];
    int c = is.read(bytes);
    System.out.println(bytes+"______________"+c);
    is.close();
}
//如果读取的内容比较大用循环进行读取
@Test
public void TestInputStream1() throws IOException {
    InputStream is01=new FileInputStream("D:/Fon/aaa.text");
    byte[] bytes = new byte[10];
    int c=0;
    while ((c=is01.read(bytes))!=-1){
        String s = new String(bytes, 0, c);
        System.out.println(s);
    }
    is01.close();
}

3.5缓存流

缓存流是在基础流[ InputStream OutputStream reader Writer ]之上 添加了一个缓存池功能

BufferInputStream BufferOutputStream BufferReader BufferWriter 提高IO的效率,降低IO的次数

@Test
public void Test01()throws IOException {
    OutputStream os=new FileOutputStream("D:/Fon/aaa.text");
    //缓存流要做用在基础流上
    BufferedOutputStream bos=new BufferedOutputStream(os);
    String str="java基础流";
    //将字符串转化为字节数组
    byte[] bytes = str.getBytes();
    //写的内容暂时放在缓存池中 没用直接放入文件中 所以文件没有你的内容
    bos.write(bytes);
    //刷新缓存池将缓存池中的内容输入到文件中
    //bos.flush();
    //关闭资源 系统会先刷新缓存池再关闭资源
    bos.close();
}

3.6对象流--对java对象进行IO操作

为什么需要对象流

我们现在操作IO流的时候都是将字符串读写操作 可不可以将java对象在文件中进行读写呢?可以的 Student st=new Student(); 对象

将java对象进行读写操作,意义在于持久化信息,例如:游戏存档。

//因为运行的时候,所有的数据都是在运行内存中的,持久化 将运行的内存数据保存到硬盘上 

存档(写) 读档(读)

@Test
public void test01() throws IOException {
    OutputStream os=new FileOutputStream("D:/Fon/aaa.txt");
    //对象输出流 持久化信息
    ObjectOutputStream oos = new ObjectOutputStream(os);
    //使用对象输出流调用输出方法 输出类的对象 该类必须实现Serializable序列化接口
    Role role = new Role("吕布", "99级", 1, "勇猛");
    oos.writeObject(role);
    oos.close();
}
@Test
public void test02() throws Exception {
    //测试 读档:----反序列化
    InputStream is=new FileInputStream("D:/Fon/aaa.txt");
    ObjectInputStream ois = new ObjectInputStream(is);
    Object o = ois.readObject();
    System.out.println(o);
    ois.close();

}

1. 序列化: 把内存中的java对象存储到磁盘[网盘]的过程。
         ---java对象所属的类必须实现序列化接口.implements Serializable 
2. 反序列化: 把磁盘中的内容读取到java对象内存中的过程。

总结

1.  通过字符流完成文件的复制---->它只能复制文本文件
2.  字节流:---字节输入流和字节输出流。
3.  字节流 我们也可以完成文件的复制功能---它可以复制任意类型的文件.
4.  缓存流--->它基本流的基础上 添加了一个缓存池 
5.  对象流: ObjectInputStream  ObjectOutputStream
     序列化:
     反序列化:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值