9.IO流(传智播客)

1.编码

public static void main(String[] args) throws UnsupportedEncodingException {
	String str = "慕课ABC";
	byte [] bytes = str.getBytes("utf-8");
	for(byte demo:bytes){
		System.out.println(Integer.toHexString(demo & 0xff));
	}
	System.out.println(new String(bytes,"utf-8"));
}
  • utf-8(中文占3个字节,英文占一个字节)
  • gbk(中文占2个字节,英文占1个字节)
  • utf-16be(中文和英文都占2个字节)
  • 文本文件字节序列可以是任意编码的字节序列,如果在中文机器上直接创建文本文件,该文本文件只能识别ANSI编码。

2.File文件操作

  • 遍历一个目录下所有目标名称

    public static void main(String[] args) throws UnsupportedEncodingException {
    	File file = new File("/Users/wenlei/Documents/workspace/app/src");
    	if(file.isDirectory()){
    		String[] arr = file.list();
    		for(String demo:arr)
    			System.out.println(demo);
    	}
    }
    
  • 遍历一个目录下所有目标名称(指定后缀名)

    public static void main(String[] args) {
    	File file = new File("/Users/wenlei/Documents/workspace/app/src");	
    	FilenameFilter filter = new FilenameFilter() {	
    		@Override
    		public boolean accept(File dir, String name) {
    			File curFile = new File(dir,name);
    			if(curFile.isFile()&&name.endsWith(".txt"))
    				return true;
    			else
    				return false;
    		}
    	};
    	
    	if(file.isDirectory()){
    		String[] arr = file.list(filter);
    		for(String demo:arr)
    			System.out.println(demo);
    	}
    }
    
  • 递归遍历

    public static void main(String[] args) {
    	File file = new File("/Users/wenlei/Documents/workspace/app/src");	
    	fun(file);
    }
    	
    private static void fun(File file) {
    	File[] arr = file.listFiles();
    	for(File demo:arr){
    		if(demo.isDirectory()){
    			fun(demo);
    		}
    		System.out.println(demo.getName());
    	}
    }
    
  • 遍历删除

    public static void main(String[] args) {
    	File file = new File("/Users/wenlei/Documents/workspace/app/src");	
    	fun(file);
    }
    	
    private static void fun(File file) {
    	File[] arr = file.listFiles();
    	for(File demo:arr){
    		if(demo.isDirectory()){
    			fun(demo);
    		}
    		demo.delete();
    	}
    	file.delete();
    }
    

3.字节流

  • 逐字节读取
    public static void main(String[] args) throws IOException {
    	FileInputStream in = new FileInputStream("/Users/steven/Documents/代码/project/javaBasic/src/test.txt");
    	int b;
    	while((b=in.read())!=-1){
    		System.out.println(Integer.toHexString(b & 0xff));
    	}
    	in.close();
    }
    
  • 逐字节输出
    public static void main(String[] args) throws IOException {
    	FileOutputStream out = new FileOutputStream("/Users/steven/Documents/代码/project/javaBasic/src/test.txt");
    	String str = "hello";
    	byte [] bytes = str.getBytes();
    	for(byte demo:bytes){
    		out.write(demo);
    	}
    	out.close();
    }
    
  • 字节数组读取
    public static void main(String[] args) throws IOException {
    	FileInputStream in = new FileInputStream("/Users/steven/Documents/代码/project/javaBasic/src/test.txt");
    	byte [] bytes = new byte[1024];
    	int num = in.read(bytes);
    	for(int i = 0;i < num;i++){
    		System.out.println(bytes[i] & 0xff);
    	}
    	in.close();
    }
    
  • 字节数组输出
    public static void main(String[] args) throws IOException {
    	FileOutputStream out = new FileOutputStream("/Users/steven/Documents/代码/project/javaBasic/src/test.txt");
    	String str = "hello";
    	byte [] bytes = str.getBytes();
    	out.write(bytes);
    	out.close();
    }
    
  • 字节流缓冲区复制文件
    public static void main(String[] args) throws FileNotFoundException,IOException{
        FileInputStream in = new FileInputStream("/Users/wenlei/Downloads/TestClass/src/source/test.txt");
        FileOutputStream out = new FileOutputStream("/Users/wenlei/Downloads/TestClass/src/destination/test.txt");
        byte[] b = new byte[100];
        long start = System.currentTimeMillis();
        int num = in.read(b);
        out.write(b,0,num);
        long end = System.currentTimeMillis();
        System.out.println("time="+(end-start));
        in.close();
        out.close();
    }
    
  • 字节缓冲流
    public static void main(String[] args) throws UnsupportedEncodingException {
    	BufferedInputStream in = new BufferedInputStream(new FileInputStream("/Users/wenlei/Downloads/TestClass/src/source/test.txt"));
    	BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("/Users/wenlei/Downloads/TestClass/src/destination/test.txt"));
    	byte[] b = new byte[100];
    	long start = System.currentTimeMillis();
    	int num = in.read(b);
    	out.write(b,0,num);
    	long end = System.currentTimeMillis();
    	System.out.println("time="+(end-start));
    	in.close();
    	out.close();
    }
    

4.字符流

  • 字符输入流
    public static void main(String[] args) throws FileNotFoundException,IOException{
       FileReader in = new FileReader("/Users/wenlei/Downloads/TestClass/src/source/test.txt");
        int b;
        while((b=in.read())!=-1){
            System.out.println((char)b);
        }
        in.close();
    }
    
  • 字符输出流
    public static void main(String[] args) throws FileNotFoundException,IOException{
       FileReader in = new FileReader("/Users/wenlei/Downloads/TestClass/src/source/test.txt");
        int b;
        while((b=in.read())!=-1){
            System.out.println((char)b);
        }
        in.close();
    }
    
  • 字符缓冲流
    public static void main(String[] args) throws FileNotFoundException,IOException{
        BufferedReader br = new BufferedReader(new FileReader("/Users/wenlei/Downloads/TestClass/src/source/test.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/wenlei/Downloads/TestClass/src/destination/test.txt"));
        String str;
        while((str = br.readLine())!=null){
            bw.write(str);
            bw.newLine();
        }
        br.close();
        bw.close();
    }
    
  • 字符转换流
    public static void main(String[] args) throws FileNotFoundException,IOException{
        FileInputStream in = new FileInputStream("/Users/wenlei/Downloads/TestClass/src/source/test.txt");
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        FileOutputStream out = new FileOutputStream("/Users/wenlei/Downloads/TestClass/src/destination/test.txt");
        OutputStreamWriter osw = new OutputStreamWriter(out);
        BufferedWriter bw = new BufferedWriter(osw);
        String str;
        while((str = br.readLine())!=null){
            bw.write(str);
        }
        br.close();
        bw.close();
    }
    

5.序列化与反序列化

public class Student implements Serializable {
    private String name;
    private transient int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

    private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {
        s.defaultWriteObject();
        s.writeInt(age);
    }

    private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
        s.defaultReadObject();
        this.age = s.readInt();
    }
}

public static void main(String[] args) throws Exception {
        String pathname = "/Users/wenlei/Project/IdeaProjects/TestClass/src/app.txt";
//        FileOutputStream out = new FileOutputStream(pathname);
//        ObjectOutputStream oos = new ObjectOutputStream(out);
//        Student s = new Student("steven",20);
//        oos.writeObject(s);
//        oos.close();
        FileInputStream in = new FileInputStream(pathname);
        ObjectInputStream ois = new ObjectInputStream(in);
        Student s = (Student)ois.readObject();
        System.out.println(s.toString());
        ois.close();
    }

由transient关键字修饰的字段不尽行jvm默认的序列化,在输出时会输出对象默认初始化值,但可以进行自己的序列化与反序列化,在对象类中定义writeObject()和readObject()。
哪一层父类没有实现serializable接口,反序列化时会输出相应的构造函数。

6.RandomAccessFile

public static void main(String[] args) throws IOException {
	String file = "test.txt";
	RandomAccessFile rf = new  RandomAccessFile(file, "rw");
	for(int i = 0;i<10;i++){
	    rf.seek(rf.length());
	    String str = "追加内容\r\n";
	    rf.write(str.getBytes());
	}
	rf.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值