java学习笔记之枚举类型、file、IO流

目录

枚举类型

File

文件过滤

递归删除所有文件

IO流

字节流(最小单位是字节)

FileOutputStream

FileInputStream

高效的方法:

拷贝图片:

BufferedOutputStream

BufferedInputStream

效率比较

字符流

BufferedWriter

BufferedReader

序列化

反序列化

transient关键字


枚举类型

新建,在others搜索enum

//Color.java
package com.mcq;

public enum Color {
	RED,YELLOW,BLACK,WHITE,GREEN;
	
}
package com.mcq;

public class EnumDemo {
	public static void main(String[] args) {
		Color color=Color.WHITE;//这里必须写Color.
		switch (color){
		case RED: //这里必须不写Color.
			System.out.println("红色");
			break;
		case WHITE:
			System.out.println("白色");
			break;
		case YELLOW:
			System.out.println("黄色");
			break;
		case BLACK:
			System.out.println("黑色");
			break;
		case GREEN:
			System.out.println("绿色");
			break;
		}
	}
}

File

绝对路径:以盘符开头的路径名
相对路径:相对于工程目录的路径

package com.mcq;

import java.io.File;
import java.io.IOException;

public class FileDemo {
    public static void main(String[] args) throws IOException {
        File file=new File("E:\\javademo");
        System.out.println(file);//并不会创建
        System.out.println(file.mkdir());//创建文件夹
        //首次创建返回true,已经有的情况下返回false
        File file2=new File("E:\\javademo\\a\\b");//注意都是双\
        System.out.println(file2.mkdir());//只能一级一级的创建
        System.out.println(file2.mkdirs());//多级文件夹
        File file3=new File("E:\\javademo\\1.txt");
        System.out.println(file3.createNewFile());
        File file4=new File("aa");//相对路径
        System.out.println(file4.mkdir());
        //file的删除功能只能从最底下删一层,如果文件夹还有文件,即使是最后一级目录,也无法删除
        System.out.println(file4.delete());
        //renameTo
        new File("vv.txt").createNewFile();
        new File("vv.txt").renameTo(new File("gg.txt"));
        //判断 isFile isDirectory
        System.out.println(new File("gg.txt").isFile());
        //长度 Bytes
        System.out.println(new File("gg.txt").length());
    }
}

文件过滤

package com.mcq;

import java.io.File;
import java.io.FilenameFilter;

public class FileDemo2 {
    public static void main(String[] args) {
        File file = new File("E://");
        String []list=file.list(new FilenameFilter() { //文件过滤
            
            public boolean accept(File dir, String name) {
                if(name.endsWith(".txt")){
                    return true;
                }else 
                return false;
            }
        });
        for(String string:list){
            System.out.println(string);
        }
    }
}

递归删除所有文件
 

package com.mcq;

import java.io.File;
import java.io.FilenameFilter;

public class FileDemo2 {
    public static void main(String[] args) {
        File file=new File("E:\\javademo");
        delete(file);
    }
    public static void delete(File file){
        if(file.isDirectory()){//如果是目录
            File [] sub=file.listFiles();//获取子文件/目录
            for(File file2:sub){
                delete(file2);
            }
        }
        file.delete();
    }
}

IO流

字节流(最小单位是字节)


FileOutputStream

package com.mcq;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class IODemo {
    public static void main(String[] args) throws IOException {
        File file=new File("mm.txt");
        file.createNewFile();
        FileOutputStream fos=new FileOutputStream(file,true);//true表示可追加
        fos.write("\nhello world".getBytes());
        fos.close();
    }
}

FileInputStream

package com.mcq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IODemo {
    public static void main(String[] args) throws IOException {
        File file=new File("mm.txt");
        FileInputStream fis=new FileInputStream(file);
        int read=fis.read();//读一个字节,这里输出ASCII码
        System.out.println(read);
        int b=-1;
        while((b=fis.read())!=-1){
            System.out.println((char)b);
        }
    }
}

高效的方法:

package com.mcq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IODemo {
    public static void main(String[] args) throws IOException {
        File file=new File("mm.txt");
        FileInputStream fis=new FileInputStream(file);
        /*
         * 1.创建一个固定长度的字节数组
         * 2.当返回!=-1的时候,每次都将固定长度的字节放入数组,放不满补0
         */
        byte []bys=new byte[1024];
        while(fis.read(bys)!=-1){
            for(byte b:bys){
                System.out.print((char)b);
            }
        }
    }
}

拷贝图片:

package com.mcq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyPictureDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream(new File("C:/Users/Administrator/Desktop/照片/1518715908475.jpg"));
        FileOutputStream fos=new FileOutputStream(new File("tourist.png"));
        byte []b=new byte[1024];
        int cnt=0;
        while(fis.read(b)!=-1){
            fos.write(b);
            ++cnt;
        }
        System.out.println(cnt);
    }
}

BufferedOutputStream

带缓冲的输出流,关闭前需要刷新(flush),将缓冲区的内容刷到流中,上面两个不带缓冲的不需要刷新。

package com.mcq;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferDemo {
    public static void main(String[] args) throws IOException {
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File("mm.txt")));
        bos.write("hello mm".getBytes());
        bos.flush();//要flush才能写入文件
//        bos.close();//close调用了flush
        
    }
}

BufferedInputStream

package com.mcq;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferDemo {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                new File("mm.txt")));
        // 第一种方法
        // int b=0;
        // while((b=bis.read())!=-1){
        // System.out.print((char)b);
        // }
        // bis.close();
        // 第二种方法
        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = bis.read(bys)) != -1) {
            System.out.println(new String(bys, 0, len));
        }
        bis.close();
    }

}

效率比较

buffered n字节>n字节>buffer一个字节>一个字节
 

字符流


输入输出中文的时候很可能会产生问题,这时就需要用字符流,字符流每次处理的是字符。
使用哪种方案进行编码,就要使用那种方案进行解码,不一致就会出现乱码。

BufferedWriter

package com.mcq;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class OutputStreamWriterDemo {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("mm.txt"))));
        bw.write("好的");
        bw.flush();
        bw.append("haha");
        bw.flush();
        bw.close();
    }
}

BufferedReader

.
复制文件:

package com.mcq;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class OutputStreamWriterDemo {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("gg.txt"))));
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("mm.txt"))));
        String line=null;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();//自己加一个换行,因为读入的时候遇到换行结束,换行符并没有读进去
            bw.flush();
        }
        bw.close();
        br.close();
    }
}


 

序列化

//student.java
package com.mcq;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamDemo {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser"));
//        oos.writeObject(new Student("小明",18));
//        oos.close();
        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("student.ser"));
        Student student=(Student)ois.readObject();
        System.out.println(student);
    }
}
package com.mcq;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectStreamDemo {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser"));
        oos.writeObject(new Student("小明",18));
        oos.close();
    }
}

反序列化

package com.mcq;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamDemo {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser"));
//        oos.writeObject(new Student("小明",18));
//        oos.close();
        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("student.ser"));
        Student student=(Student)ois.readObject();
        System.out.println(student);
    }
}

可序列化的类需要实现Serializable接口,并且自动生成一个seriaVersionUID,使用两种生成方式均可。
如果不生成seriaVersionUID,则在序列化后修改类,再反序列化的过程中会抛出异常。

package com.mcq;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamDemo {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser"));
//        oos.writeObject(new Student("小明",18));
//        oos.close();
        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("student.ser"));
        Student student=(Student)ois.readObject();
        System.out.println(student);
        student.test();
    }
}

transient关键字


    如果在序列化的过程中,有不想去序列化的成员变量,那么可以添加transient关键字来修饰。
    语法:public transient String name;
    不能用来修饰方法
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值