欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

I/O流

程序员文章站 2022-06-13 22:22:48
...

I/O流

Input  output

输入输出

相对程序而言。Input认为read,output认为write。

文件—程序 input

程序—文件output

文件file类

2:流stream

字节流  用于读取图片,音视频等二进制文件

InputStream

outputStream

一个汉字  GBK里面占两个字节 UTF-8里面占三个字节

一个字符  GBK UTF-8占1个字节

总结:windows系统下的txt文档默认是ANSI编码是GBK,由于打开txt文档是记事本,所以写入txt文档内容时,记事本会“偷偷的”附加一些字节,使得原本录入的数据字节增大。Word更甚!本次使用IDE来完成txt文档纯净的字节录入,不被软件对录入内容附加格式干扰。

I/O流

 I/O流

 I/O流

 I/O流

 I/O流

 I/O流

 

/**
 * 
 */
package com.zhiyou.S;

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

/**
 * @author Administrator
 *
 */
public class InputStreamDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		

		inputStreamDemo(); 
		
	}
	
	
	public static void inputStreamDemo() {
		
//		文件的输入流
//		创建文件的输入流对象,给定路径
		try {
			FileInputStream inputStream = new FileInputStream("D:\\a.txt");
//			新建一个字节数组,目的是存放读取过来的字节内容
			byte[] fileByte = new byte[1024];
//			有可能产生I/O异常
			inputStream.read(fileByte);
			
//			把字节数组转换成字符串
			String string = new  String(fileByte);
			System.out.println(fileByte);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
//	如果文件是2000字节,byte数组是1024,会发生什么?
//	一次性只能读1024字节,读完肯定要执行多次的read操作。
//	每次可以记录读取的长度
//	read方法返回值为-1代表已经读完。
//	尝试着让《三国》按照章节读取出来
}
/**
 * 
 */
package com.zhiyou.T;

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

/**
 * @author Administrator
 *
 */
public class InputSreamDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

//		read();

//		read1();

//		缓冲区 无论读与写,先一次性塞满缓冲区再让缓冲区执行读与写,提升效率
//		直接进行的读写,是操作硬盘,缓冲区存放的是操作内容。
//		cup与硬盘打交道远远低于CPU与内存的沟通 

//		BufferedInputStream
//		BufferedOutputStream

//		对比fileInputStream和BufferedInputStream的执行效率
		
		timeOfFileIputStream();

		timeForBufferedInputStream();
		
//		从执行时间上来说bufferedInputStream要比FileInputStream快
	}

	public static void timeOfFileIputStream() {
//		获得1970.1.1到现在的毫秒数
		long time1 = System.currentTimeMillis();

		read1();

		long time2 = System.currentTimeMillis();

		System.out.println(time2 - time1);

	}

	public static void timeForBufferedInputStream() {

		long time1 = System.currentTimeMillis();
		read2();
		long time2 = System.currentTimeMillis();
		System.out.println(time2 - time1);
	}

	public static void read1() {

		try {
			FileInputStream inputStream = new FileInputStream("D:\\笔记\\总结.txt");

//			用已经创建的inputStream。转换成bufferedInputStream
//			BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

			byte[] bytes = new byte[1000];
			int length = 0;
//			while后面如果长度为-1就证明已经读完,就不在执行字符数组转换成字符串
			while ((length = inputStream.read(bytes)) != -1) {

				String string = new String(bytes, 0, length);
				System.out.println(string);
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void read2() {

		try {
			FileInputStream inputStream = new FileInputStream("D:\\笔记\\总结.txt");

//			用已经创建的inputStream。转换成bufferedInputStream
			BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

			byte[] bytes = new byte[1000];
			int length = 0;
//			while后面如果长度为-1就证明已经读完,就不在执行字符数组转换成字符串
			while ((length = bufferedInputStream.read(bytes)) != -1) {

				String string = new String(bytes, 0, length);
				System.out.println(string);
			}
			bufferedInputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void read() {

		try {

			FileInputStream inputStream = new FileInputStream("D:\\笔记\\总结.txt");

			byte[] fileByte = new byte[1000];

			int length = inputStream.read(fileByte);
			System.out.println(length);
			System.out.println(new String(fileByte));
			System.out.println("1--------------------------------------");

			int length1 = inputStream.read(fileByte);
			System.err.println(length1);
			System.out.println(new String(fileByte));
			System.out.println("2-------------------------------------");

			int length2 = inputStream.read(fileByte);
			System.err.println(length2);
			System.out.println(new String(fileByte));
			System.out.println("3-------------------------------------");

			int length3 = inputStream.read(fileByte);
			System.err.println(length3);
			System.out.println(new String(fileByte));
			System.out.println("4-------------------------------------");

//		read的结果代表是每次读字节的长度,-1代表读完
			int length4 = inputStream.read(fileByte);

			System.err.println(length4);

//		把字节数组从开始,转换length4长度的内容。
//		fileByte
			String string = new String(fileByte, 0, length4);
			System.out.println(new String(fileByte));
			System.out.println("5-------------------------------------");

			inputStream.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	
}
/**
 * 
 */
package com.zhiyou.T;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author Administrator
 *
 */
public class OutStremDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

//		fileOutputStream();

		fileOutputStreamDemo();
	}

	public static void fileOutputStream() {
		try {
			FileOutputStream outputStream = new FileOutputStream("D:\\笔记\\2.txt");

			String string = "老孙abc";
			byte[] bytes = string.getBytes();

			outputStream.write(bytes);
			outputStream.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void fileOutputStreamDemo() {
		FileOutputStream outputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			outputStream = new FileOutputStream("D:\\笔记\\2.txt");

			bufferedOutputStream = new BufferedOutputStream(outputStream);

			String string = "qwertyuiop";
			byte[] bytes = string.getBytes();

			bufferedOutputStream.write(bytes);

			bufferedOutputStream.flush();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bufferedOutputStream != null) {
				try {
					bufferedOutputStream.close();
				} catch (IOException e2) {
					// TODO: handle exception
				}

			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
			}

		}

	}
}

 

相关标签: I/O流