程序员干货分享

散列表和IO

2020-10-23  本文已影响0人  小白菜aaa

散列表(也叫哈希表)

直接寻址法

数字分析法

平方取中法

折叠法

随机数法

除留取余法

散列表中处理地址重复的方法

开放寻址方式

再散列法

链地址法

建立一个公共溢出区

如果在使用链地址法时,链表过长

IO

java用变量存储数据,用集合存储并管理多个数据,IO负责数据的转移(传输)。

核心:选择一个适合当前场景的流对象,按照正确的流程调用方法。

对于当前的类,考虑两个问题,数据流动方向和数据内容方向:

所以流对象一共四类

java提供了四个抽象类:

所有操作

基本的字符字节操作

输入字符流

public static void main(String[] args) throws Exception {

        try {
            //读取文件
            FileReader reader = new FileReader("/Users/xutingyu/Documents/Tye/TestFile/tye.txt");
            char[] array = new char[10];
            int length = 0;
            while ((length = reader.read(array)) > 0) {
                //将一个字节数组array从0取到length,然后转换成String类型
                System.out.print(new String(array, 0, length));
            }
            reader.close();

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

输出字符流

public static void main(String[] args) {

        String str = "hudhkhdpidsjfpdsifjildsgjidgdfg\n123123123";

        try {
            FileWriter writer = new FileWriter("/Users/xutingyu/Documents/Tye/TestFile/tye.txt");
            writer.write(str);
            //清空缓冲区
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

一个txt文件的复制,先读再写

public static void main(String[] args) throws Exception {

        FileReader reader = new FileReader("/Users/xutingyu/Documents/Tye/TestFile/tye.txt");
        FileWriter writer = new FileWriter("/Users/xutingyu/Documents/Tye/TestFile/tye1.txt");

        char[] array = new char[10];
        int length = 0;
        writer.write("这是复制内容:\n");
        while ((length = reader.read(array)) > 0) {
            writer.write(new String(array, 0, length));
        }
        writer.flush();
        reader.close();
        writer.close();

    }

一个图片的复制,将字符流换成字节流

public static void main(String[] args) throws Exception {

        FileInputStream input = new FileInputStream("/Users/xutingyu/Documents/Tye/TestFile/tye.jpg");
        FileOutputStream out = new FileOutputStream("/Users/xutingyu/Documents/Tye/TestFile/tye1.jpg");

        byte[] array = new byte[1024];
        int times = 0;

        while (input.read(array) > 0) {
            times++;
            out.write(array);
            if (times == 100) {
                break;
            }
        }
        out.flush();
        input.close();
        out.close();
        System.out.println(times);
    }

如果再带上注释内容,可以复制半张图,仅仅作为演示效果

缓冲流,在普通流的基础上多封装了一些功能,便于操作

public static void main(String[] args) throws Exception{

        FileReader reader = new FileReader("/Users/xutingyu/Documents/Tye/TestFile/tye.txt");

        BufferedReader br = new BufferedReader(reader);
        String str="";
        while ((str=br.readLine())!=null) {
            System.out.println(str);
        }

        FileWriter writer=new FileWriter("/Users/xutingyu/Documents/Tye/TestFile/tye1.txt");
        BufferedWriter bw=new BufferedWriter(writer);
        bw.write("dkfkjdgnjknfdlkgn");
        bw.newLine();
        bw.write("32422312321");
        bw.flush();
        bw.close();
        writer.close();

    }

磁盘操作

//创建
    public void create() {
        String dirname = "/Users/xutingyu/Documents/Tye/TestFile/abc";
        File f = new File(dirname);
        f.mkdir();

    }

public void del() {
        String dirname = "/Users/xutingyu/Documents/Tye/TestFile/bcd/efg/hij";
        File f = new File(dirname);
        f.delete();
    }

public void delAll(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    delAll(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }

public void rename() {

        String path = "/Users/xutingyu/Documents/Tye/TestFile/";
        String picName = "abc";
        String newNmae = "bcd";
        File file = new File(path + picName);
        if (file.renameTo(new File(path + newNmae))) {
            System.out.println("修改成功!");
        } else {
            System.out.println("修改失败");
        }
    }

结尾

本文到这里就结束了,感谢看到最后的朋友,都看到最后了,点个赞再走啊,如有不对之处还请多多指正。

上一篇 下一篇

猜你喜欢

热点阅读