java IO

查找某目录下以avi结尾的大于100M的文件

public class Demo {
    public static void main(String[] args) {
        File e = new File("e://");
        File[] files = e.listFiles();
        listFile(files);

    }

    public static void listFile(File[] files){
        if(files!=null&&files.length>0){
            for (File file:files) {
                if(file.isFile()){
                    if(file.getName().endsWith(".avi")){
                        if(file.length()>100*1024*1024)
                        System.out.println("找到了一个avi文件"+file.getAbsolutePath());
                    }
                }else {
                    File[] files2 = file.listFiles();
                    listFile(files2);
                }
            }
        }

    }
}

字节流输出

public class Demo {
    //IO流    可以把数据传输的操作看作是数据的流动,分为输入Input和输出Output
    //java的IO操作就是java.io包下的一些常用类的使用,通过这些常用类对数据进行读取和写出
    //IO流的分类    通过流的方向可以分为:输入流和输出流
    //按照流动的数据类型分为:字节流  字符流
    //字节流:输入流   InputStream
    //       输出流   outputStream
    //字符流:输入流  Reader
    //       输出流  Write
    //一切皆字节
    public static void main(String[] args) throws IOException {
        //OutputStream
        FileOutputStream fos = new FileOutputStream("c://a.txt");
        /*byte[] bytes = {65,66,67,68,69};
        fos.write(bytes);*/
        //byte[] bytes2 = {65,66,67,68,69};
        byte[] bytes2 = "ABCDEF".getBytes();
        fos.write(bytes2,2,2);
        //fos.write(bytes2);
        fos.close();                //写在哪在哪关闭
        System.out.println("已经写出");

    }
}

字节流读入

public class Demo {
    //InputStream
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("c://a.txt");
        /*while (true){
            byte b = (byte) fis.read();
            if(b==-1){
                break;
            }
            System.out.println(b);
        }*/
        byte[] bytes = new byte[10];
        int len = fis.read(bytes);
        System.out.println(new String(bytes,0,len));
        len = fis.read(bytes);
        System.out.println(new String(bytes,0,len));
        len = fis.read(bytes);
        System.out.println(new String(bytes,0,len));
        fis.close();
    }
}

给文件做异或加密

public class Demo {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入文件的全路径");
        Scanner scanner = new Scanner(System.in);
        String filename = scanner.nextLine();
        //原文件
        File oldFile = new File(filename);
        //加密文件    加前缀mi-
        File newFile = new File(oldFile.getParent(),"mi-"+oldFile.getName());
        FileInputStream fis = new FileInputStream(oldFile);
        FileOutputStream fos = new FileOutputStream(newFile);
        while (true){
            int b = fis.read();
            if(b==-1){
                break;
            }
            //任何数据^10两次  结果还是其本身   加密解密过程
            fos.write(b^10);

        }
        System.out.println("加密或解密完成");
    }
}

字符流输出

public class Demo {
    public static void main(String[] args) throws IOException {
        //writer
        FileWriter fw = new FileWriter("c://b.txt",true);
        //fw.write('a');
        fw.append("锄禾日当午").append(",").append("汗滴禾下土");   //可以一致追加
        fw.write("锄禾日当午");
        fw.flush();        //刷新
        fw.close();

    }
}

字符流读取

public class Demo10 {
    public static void main(String[] args) throws IOException {
        //reader
        FileReader fr = new FileReader("b.txt");
        while (true){
            int c = fr.read();
            if(c==-1){
                break;
            }
            System.out.println((char)c);
        }
        char[] chars = new char[100];
        //fr.read(chars);
        System.out.println(chars[0]);
        fr.close();
    }
}

字节流转字符流

public class Demo11 {
    public static void main(String[] args) throws IOException {
        //转换流  :将字节流转换成字符流     使用了装饰者模式
        FileInputStream fis = new FileInputStream("c://a.txt");
        //将字节输入流转换为字符输入流  参数为要转换的字节流
        InputStreamReader isr = new InputStreamReader(fis,"gbk");
        while (true){
            int c = isr.read();
            if(c==-1){
                break;
            }
            System.out.println((char) c);
        }

    }
}

收集异常信息

public class Demo12 {
    public static void main(String[] args) throws FileNotFoundException {
        //收集异常信息
        try {
            String s = null;
            s.toString();
        }catch (Exception e){
            PrintWriter pw = new PrintWriter("c://bug.txt");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            pw.print(format.format(new Date()));
            e.printStackTrace(pw);
            pw.close();
        }


    }
}

properties文件读写

public class Demo13 {
    public static void main(String[] args) throws IOException {
        //properties文件与properties类
        /*Properties pt = new Properties();
        //键=值
        pt.put("name","金苹果");
        pt.put("info","讲述历了金苹果种植的过程");
        FileWriter fw = new FileWriter("c://book.properties");
        pt.store(fw,"存储的图书");
        fw.close();*/

        Properties pt = new Properties();
        Reader fw = new FileReader("c://book.properties");
        pt.load(fw);
        System.out.println("name");
        System.out.println("info");
    }
}

序列化与反序列化

public class Demo14 {
    public static void main(String[] args) throws IOException {
        //序列化与反序列化
        Book b = new Book("金苹果","讲述了种植过程");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c://book.hahas"));
        oos.writeObject(b);
        oos.close();

    }
    static class Book implements Serializable {
        private String name;
        private String info;

        public Book(String name, String info) {
            this.name = name;
            this.info = info;
        }

        public Book() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }

        @Override
        public String toString() {
            return "Book{" +
                    "name='" + name + '\'' +
                    ", info='" + info + '\'' +
                    '}';
        }
    }
}

JDK9特性

public class Demo15 {
    public static void main(String[] args) throws FileNotFoundException {
        //try-with-resources
        /*try {
            FileReader fr = new FileReader("c://book.txt");
            int c = fr.read();
            System.out.println((char)c);
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        //jdk9
        FileReader fr = new FileReader("c://book.txt");
        PrintWriter pw = new PrintWriter("c://book.txt");
        try(fr;pw){
            int c = fr.read();
            System.out.println((char)c);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

序列化反序列化工具类实现

public class MySerializableUtils {
    public static void mySerializable(Object obj) throws IOException {
        OutputStream outputStream = new FileOutputStream("expressStore.txt");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(obj);
        objectOutputStream.close();
    }

    public static ExpressHouse myDeserializable() throws IOException, ClassNotFoundException {
        File file = new File("expressStore.txt");
        if(!file.exists()) {
            file.createNewFile();
        }
        if(file == null || file.length()==0){
            ExpressHouse expressHouse = new ExpressHouse();
            expressHouse.setLocationMap(new LinkedHashMap<Integer, Express>());
            return expressHouse;
        }
        InputStream inputStream = new FileInputStream(file);
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        Object obj = objectInputStream.readObject();
        //objectInputStream.close();
        if(obj instanceof ExpressHouse) {
            return (ExpressHouse)obj;
        }
        return null;
    }
}

Print与BufferedReader

/*PrintStream ps = new PrintStream("d:\\d.txt");
ps.println("锄禾日当午");
ps.println("锄禾日当午");*/
/*PrintWriter pw = new PrintWriter("d:\\d.txt");
pw.println("汗滴禾下土");
pw.println("汗滴禾下土");
pw.flush();*/
/*FileOutputStream fos = new FileOutputStream("d:\\d.txt");
PrintWriter pw = new PrintWriter(fos);
pw.println("锄禾日当午");
pw.println("锄禾日当午");
pw.flush();*/
FileReader fr = new FileReader("d:\\d.txt");
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
System.out.println(str);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值