Java IO类

2020最新版 Java基础-IO框架
输入流:将<存储设备>中的内容读入到<内存>中。
输出流:将<内存>中的内容写入到<存储设备>中。在这里插入图片描述
节点流:具有实际传输数据的读写功能。
过滤流:在节点流的基础之上增强功能。

所有在java.io中的类都将相对路径名解释为以用户工作目录开始,通过下面代码来获取这个信息

System.out.println(System.getProperty("user.dir"));  //用户工作目录  io类的相对路径

字节流:以字节为单位的流
字节流的父类
InputStream:字节输入流
OutputStream:字节输出流

通过FileInputStream读取文件

 static void readone() throws IOException {
     FileInputStream file = new FileInputStream("src\\main\\resources\\application.xml");
     System.out.println("file = " + file);
     int date = 0 ;
     while ((date=file.read())!=-1){
         System.out.print(  (char)date);
     }
     file.close();
 }

FileInputStream自定义缓冲数组

static void readmore() throws IOException {
    FileInputStream file = new FileInputStream("src\\main\\resources\\asd.txt");
    System.out.println("file = " + file);
    byte[] buf = new byte[1024] ;           //缓冲数组
    int count = 0  ;
    while ((count=file.read(buf))!=-1){     //将file文件读入到buf数组里
        System.out.print(new String(buf,0,count));  //最后可能不是1024个byte
    }
    file.close();
}

使用BufferedInputStream读取数据

static void readbuffer() throws IOException {
    FileInputStream file = new FileInputStream("src\\main\\resources\\asd.txt");
    BufferedInputStream bufferedInputStream = new BufferedInputStream(file);
    int date = 0 ;
    while ((date=bufferedInputStream.read())!=-1){
        System.out.print(  (char)date);
    }
    bufferedInputStream.close();   //file内部已经关了
}

数据写入

static void wirte() throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream("src\\\\main\\\\resources\\\\asd.txt",true); //true代表追加
    String world = "hello world" ;
    fileOutputStream.write(world.getBytes());
    fileOutputStream.close();
}

文件复制

static void filecopy() throws IOException {
    FileInputStream fileInputStream = new FileInputStream("C:\\Users\\xiaocai\\Pictures\\sadw20200427121533.png");
    FileOutputStream fileOutputStream = new FileOutputStream("src\\main\\resources\\daw.png");
    byte[] bytes = new byte[1024];
    int count = 0 ;
    while ((count = fileInputStream.read(bytes))!=-1){
        fileOutputStream.write(bytes,0,count);
    }
    fileInputStream.close();
    fileOutputStream.close();
}

序列化

public class Student implements Serializable {    //序列化的类要继承Serializable
	//判断类的serialVersionUID来验证版本一致性的
    private static final long serialVersionUID = -2961257442669125983L ;  
    String name ;
    transient int age ;  //该属性不能序列化    同样静态属性也不能序列化
    //constructor   setter  getter  toString  .......
}
static void outobject() throws IOException {   //序列化  对象写入文件中
    FileOutputStream fileOutputStream = new FileOutputStream("src\\main\\resources\\aaa.bin");
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
    Student zy = new Student("zy", 20);
    objectOutputStream.writeObject(zy);
    objectOutputStream.close();
}

反序列化

static void Inobject() throws IOException, ClassNotFoundException {
    FileInputStream fileInputStream = new FileInputStream("src\\main\\resources\\aaa.bin");
    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
    Object o = objectInputStream.readObject();
    System.out.println("o = " + o);
    objectInputStream.close();
}

文件操作

File file = new File("qqqq.txt");    //只是创建个文件对象
System.out.println(file.toString());

/*文件创建*/
if (!file.exists()){       //如果文件不存在  就创建
    boolean newFile = file.createNewFile();
    System.out.println("newFile = " + newFile);
}

/*文件删除*/
boolean delete = file.delete();
System.out.println("delete = " + delete);

/*文件夹创建*/
File dir= new File("aaa\\bbb\\ccc");
if (!dir.exists()){
	//dir.mkdir() ;//单级
    dir.mkdirs() ;//多级
    System.out.println("create sucess");
}

/*文件夹删除  注意只能是空目录*/
boolean deletedir = dir.delete();
System.out.println("deletedir = " + deletedir );

/*目录遍历*/
File dirlist= new File("C:\\");
String[] list = dirlist.list();
for (String name : list) {
    System.out.println("name = " + name );
}


/*文件过滤*/
File filefilter = new File("C:\\Users\\xiaocai\\Pictures");
File[] files = filefilter.listFiles(new FileFilter() {
    public boolean accept(File pathname) {
        if (pathname.getName().endsWith(".jpg"))    //如果文件名最后为.jgp
            return true;                            //返回文件
        else return false;//false  不返回文件
    }
});
for (File name : files) {
    System.out.println(name);
}

/*遍历文件夹*/
static void listDir(File dir){
    File[] files = dir.listFiles();
    if (files!=null && files.length>0){
        for (File f :   files) {
            if (f.isDirectory()) listDir(f);  //递归
            else System.out.println(f);
        }
    }
}

/*递归删除文件夹*/
static void deleteDir(File dir){
    File[] files = dir.listFiles();
    if (files!=null && files.length>0){
        for (File f :   files) {
            if (f.isDirectory()) deleteDir(f);  //递归
            else System.out.println(f.delete());
        }
    }
    dir.delete() ;      //最后删除最外围的文件夹
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值