java io流

11 篇文章 0 订阅
package haixinpackage;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class How2jTest {
    public static void main(String[] args)  {
        byte[] data = new byte[]{88,99};
        //write("/Users/haixin/IdeaProjects/javatext/src/test1.txt",data);
            Father chen = new Father("chen");
            File f = new File("/Users/haixin/IdeaProjects/javatext/src/test.txt");
            try(FileInputStream fis = new FileInputStream(f);
                ObjectInputStream ois = new ObjectInputStream(fis);
                FileOutputStream fos = new FileOutputStream(f);
                ObjectOutputStream oos = new ObjectOutputStream(fos)
                ){
                oos.writeObject(chen);

                Father father = null;
                try {
                    father = (Father) ois.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                System.out.println(father.getName());

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




    }

    public static long searchFile(File f){
        long max = 0;
        long min = Long.MAX_VALUE;
        File[] fs = f.listFiles();

        for (File f2:fs){
            if(f2.isDirectory()){
                searchFile(f2);
            }
            if(f2.isFile()){
                max = f2.length() > max ? f2.length() : max;
                min = f2.length() < min ? f2.length() : max;
            }
        }
        return min;
    }

    public static void write(String str,byte[] data) {
            File f = new File(str);
            String parentstr = f.getParent();
            File parent = new File(parentstr);
            parent.mkdirs();
        try {
            f.createNewFile();
                FileOutputStream fos = new FileOutputStream(f);
                fos.write(data);
                fos.close();
                System.out.println("完成");


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void copyFile(String srcFile, String destFile){

        File srcfile = new File(srcFile);
        File destfile = new File(destFile);
        File destParent = destfile.getParentFile();
        //获取父目录路径
        destParent.mkdirs();

        try(
            FileInputStream srcfis = new FileInputStream(srcfile);
            FileOutputStream desfos = new FileOutputStream(destfile);){

            byte[] alldata = new byte[(int)srcfile.length()];

            if(-1 != srcfis.read(alldata)){
                desfos.write(alldata);
            }


        }catch(IOException e){
            e.printStackTrace();
        }
    }
    //查询文件尾为search的文件
    public static void search(File folder, String search){
        //File parentfile = folder.getParentFile();
        while(folder.isDirectory()){
            while(folder.list() != null){
                File[] filelis = folder.listFiles();
                for (int i = 0; i < filelis.length; i++){
                    if(search.equals(filelis[i].getName().substring(filelis[i].getName().length()-4))){

                    }
                }
            }

        }
    }
}
class Father implements Serializable{
    private String name;
    public Father(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值