DAY13

1.缓冲字节流:

基于硬盘io文件操作速度太慢,产生了基于内存的缓冲IO流。

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

public class Test {
	public static void main(String[] args) {
		try {
//			Test.testBufferedInputStream();
//			Test.testBufferedOutputStream();
			Test.copyFile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 缓冲字节输入流
	 * @throws Exception
	 */
	public static void testBufferedInputStream() throws Exception {
		//文件字节输入流对象
		FileInputStream in = new FileInputStream("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt.txt");
	
		//缓冲字节输入流对象
		BufferedInputStream br = new BufferedInputStream(in);
		
		byte[] b = new byte[10];
		
		int len = 0;
		
		while((len = br.read(b)) != -1) {
			System.out.println(new String(b,0,len));
		}
		
		//关闭流的时候  本着一个最晚开的最早关,依次关
		br.close();
		in.close();
		
	}
	/**
	 * 缓冲字节输出流
	 * @throws Exception
	 */
	public static void testBufferedOutputStream() throws Exception {
		//创建字节流输出对象
		FileOutputStream out = new FileOutputStream("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt2.txt");
		
		//把字节输出流对象放缓冲字节流
		BufferedOutputStream bo = new BufferedOutputStream(out);
		
		String s = "hello world";
		
		bo.write(s.getBytes());//写道内存
		bo.flush();//刷到硬盘上
		
		//最晚开最早关,依次关
		bo.close();
		out.close();
		
	}
	/**
	 * 缓冲流实现文件的复制
	 * @throws Exception 
	 */
	public static void copyFile() throws Exception {
		//缓冲输入流
		BufferedInputStream br = new BufferedInputStream(new FileInputStream("C:\\\\Users\\\\Daniel\\\\eclipse-workspace\\\\hello\\\\src\\\\day13\\\\tt.txt"));
		//缓冲输出流
		BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("C:\\\\\\\\Users\\\\\\\\Daniel\\\\\\\\eclipse-workspace\\\\\\\\hello\\\\\\\\src\\\\\\\\day13\\\\\\\\tt2.txt"));
		byte[] b = new byte[1024];
		
		int len = 0;
		while((len = br.read(b)) != -1) {
			bo.write(b,0,len);
		}
		bo.flush();//刷到硬盘
		bo.close();
		br.close();
	}
}

2.缓冲字符流:

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

public class Test {
	public static void main(String[] args) {
		try {
//			Test.testBufferedInputStream();
//			Test.testBufferedOutputStream();
			Test.copyFile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 缓冲字节输入流
	 * @throws Exception
	 */
	public static void testBufferedInputStream() throws Exception {
		//文件字节输入流对象
		FileInputStream in = new FileInputStream("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt.txt");
	
		//缓冲字节输入流对象
		BufferedInputStream br = new BufferedInputStream(in);
		
		byte[] b = new byte[10];
		
		int len = 0;
		
		while((len = br.read(b)) != -1) {
			System.out.println(new String(b,0,len));
		}
		
		//关闭流的时候  本着一个最晚开的最早关,依次关
		br.close();
		in.close();
		
	}
	/**
	 * 缓冲字节输出流
	 * @throws Exception
	 */
	public static void testBufferedOutputStream() throws Exception {
		//创建字节流输出对象
		FileOutputStream out = new FileOutputStream("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt2.txt");
		
		//把字节输出流对象放缓冲字节流
		BufferedOutputStream bo = new BufferedOutputStream(out);
		
		String s = "hello world";
		
		bo.write(s.getBytes());//写道内存
		bo.flush();//刷到硬盘上
		
		//最晚开最早关,依次关
		bo.close();
		out.close();
		
	}
	/**
	 * 缓冲流实现文件的复制
	 * @throws Exception 
	 */
	public static void copyFile() throws Exception {
		//缓冲输入流
		BufferedInputStream br = new BufferedInputStream(new FileInputStream("C:\\\\Users\\\\Daniel\\\\eclipse-workspace\\\\hello\\\\src\\\\day13\\\\tt.txt"));
		//缓冲输出流
		BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("C:\\\\\\\\Users\\\\\\\\Daniel\\\\\\\\eclipse-workspace\\\\\\\\hello\\\\\\\\src\\\\\\\\day13\\\\\\\\tt2.txt"));
		byte[] b = new byte[1024];
		
		int len = 0;
		while((len = br.read(b)) != -1) {
			bo.write(b,0,len);
		}
		bo.flush();//刷到硬盘
		bo.close();
		br.close();
	}
}

3.转换流:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

public class Test2 {
	public static void main(String[] args) {
		try {
			Test2.testInputStreamReader();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 转换输入流
	 * @throws Exception 
	 */
	public static void testInputStreamReader() throws Exception {
		FileInputStream fs = new FileInputStream("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt5.txt");
		//字节流转字符流
		InputStreamReader in = new InputStreamReader(fs,"GBK");
		
		char[] c = new char[100];
		int len = 0;
		
		while((len = in.read())!=-1) {
			System.out.println(new String(c,0,len));
		}
		
		in.close();
		fs.close();
	}
}

4.标准输入输出流:

import java.io.BufferedReader;
import java.io.BufferedWriter;import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 标准输入输出流
 * @author Daniel
 *
 */
public class Test3 {
	public static void main(String[] args) {
		try {
//			Test3.testSystemIn();
			Test3.write2TXT();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 标准输入流
	 * @throws Exception
	 */
	public static void testSystemIn() throws Exception {
		//创建一个接收键盘输入数据的输入流
		InputStreamReader is = new InputStreamReader(System.in);
		
		//把输入流放到缓冲流里
		BufferedReader br = new BufferedReader(is);
		
		String str = "";
		
		while((str=br.readLine()) != null) {
			System.out.println(str);
		}
		
		br.close();
		is.close();
	}
	/**
	 * 把控制台输入的内容写到txt中,若接收到over,则结束
	 * @throws Exception 
	 */
	public static void write2TXT() throws Exception {
		//创建一个接收键盘输入数据的输入流
		InputStreamReader is = new InputStreamReader(System.in);
		
		//把输入流放到缓冲流里
		BufferedReader br = new BufferedReader(is);
		
		BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt7.txt"));
		
		String str = "";

		while((str=br.readLine()) != null) {
			if(str.equals("over")) {
				break;
			}
			//读取的每一行都写到指定txt中
			out.write(str);
			
		}
		out.flush();
		out.close();
		br.close();
		is.close();
	}
}

5*.打印流和数据流:

6.对象流:

import java.io.Serializable;

/**
 * 可以序列化与反序列化的对象
 * @author Daniel
 *
 */
public class Person implements	Serializable{
	/**
	 * 用来表明类的不同版本的兼容性
	 */
	private static final long serialVersionUID = 1L;
	
	String name;
	int age;
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * 序列化和反序列化
 * @author Daniel
 *
 */
public class Test5 {
	public static void main(String[] args) {
		  try {
			Test5.testSerialize();
			Test5.testDeserialize();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 对象的序列化
	 * @throws Exception 
	 * @throws FileNotFoundException 
	 */
	public static void testSerialize() throws FileNotFoundException, Exception {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt8.txt"));
		Person p = new Person();
		p.name = "zhangsan";
		p.age = 11;
		
		out.writeObject(p);
		out.flush();
		
		out.close();
		
	}
	/**
	 * 对象的反序列化
	 * @throws Exception 
	 * @throws FileNotFoundException 
	 */
	public static void testDeserialize() throws FileNotFoundException, Exception {
		//创建对象输入流对象,从指定文件中把对象序列化的流读取出来
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt8.txt"));
		Object obj = in.readObject();
		
		Person p = (Person) obj;
		System.out.println(p.name);
		System.out.println(p.age);
		in.close();
	}
}

7.随机读取流:

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

/**
 * 文件的随机读写
 * @author Daniel
 *
 */
public class Test6 {
	public static void main(String[] args) {
		try {
			Test6.testRandomAccessFileRead();
			Test6.testRandomAccessFileWrite();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 随机读文件,此随机指的是想从哪开始就从哪开始
	 * @throws Exception 
	 */
	public static void testRandomAccessFileRead() throws Exception {
		RandomAccessFile ra = new RandomAccessFile("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt10.txt", "r");
		
		ra.seek(0);//设置读取文件内容的起始点
		byte[] b = new byte[1024];
		int len = 0;
		while((len = ra.read(b)) != -1) {
			System.out.println(new String(b,0,len));
		}
		ra.close();
	}
	
	/**
	 * 随机写文件
	 * @throws Exception 
	 */
	public static void testRandomAccessFileWrite() throws Exception {
		RandomAccessFile ra = new RandomAccessFile("C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt10.txt","rw");
		
		ra.seek(0);
//		ra.setLength(ra.length());
		ra.write("你好".getBytes());
		ra.close();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值