JavaIO流基础语法及方法的应用

 流的概念: 数据流向某个对象的数据序列, 并且到达这个对象的过程。
 输入流:数据源数据流向计算机内存的过程
 输出流:把数据从程序流向目标数据源的过程
 字节流:以字节为数据单位来处理的流
 字符流:以字符为数据单位来处理的流
 流的基类:
 输入流:以InputStream(字节输入流)和Reader(字符输入流)为基类

 输入流:以OUtputStream(字节输出流)和Writer(字符输出流)为基类

方法的使用:

1.从文件地址中读取内容到程序中,并把字节数组转换成字符串

方法一:

public static void main(String[] args) {
//磁盘路径两种表示方式:1.\\	2./
	try {
		//从文件地址中读取内容到程序中
		InputStream is=new FileInputStream("D:/iodemo/ch01.txt");
		//开始读取信息
		//先定义一个字节数组存放数据
		byte[] b=new byte[5];
		//声明一个int存储每次读取到的数据
		int i=0;
		//定义一个记录索引的变量
		int index=0;
		//循环读取每个数据
		while((i=is.read())!=-1) {
			b[index]=(byte) i;
			index++;
		}
		//把字节数组转成字符串
		System.out.println(new String(b));
		//关闭流
		is.close();
	} catch (FileNotFoundException e) {
		//系统强制解决的问题:文件没有找到
		// TODO Auto-generated catch block
			e.printStackTrace();
	} catch (IOException e) {
		//文件读写异常
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
方法二:

public static void main(String[] args) {
	try {
		InputStream is = new FileInputStream("E:/iodemo/ch01.txt");
		byte[] b = new byte[5];//把所有的数据读取到这个字节当中
		//完整的读取一个文件
		int off = 0;
		while(is.read(b, off, 2)!=-1){
			off+=1;
		}
		is.read(b,off,2);
		//read:返回的是读取的文件大小
		//最大不超过b.length,返回实际读取的字节个数
		System.out.println(Arrays.toString(b));//读取的是字节数组
		System.out.println(new String(b));
		is.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
2.在读取文件时,不全部读取,跳过n个字节后再开始读取

public static void main(String[] args) {
	try {
		FileInputStream fis = new FileInputStream("E:/iodemo/ch01.txt");
		byte[] b = new byte[fis.available()];
		//skip跳过n个字节后再开始读取
		fis.skip(2);
		fis.read(b);
		System.out.println(new String(b));
		fis.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
3.读取时把2个流合并在一起读取
public static void main(String[] args) {
	try {
		//读第一个文件流
		FileInputStream fis1 = new FileInputStream("E:/iodemo/ch01.txt");
		//读第二个文件流
		FileInputStream fis2 = new FileInputStream("E:/iodemo/ch04.txt");
		//合并到序列流中
		SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
			
		//方式1
		/*int len = fis1.available()+fis2.available();
		//临时存放数据的数组
		byte[] b = new byte[2*len+1];
		//一次性读取所有的内容
		int off = 0;
		int i = 0;
		while((i = sis.read(b,off,len)) != -1){
			off += i;
		}
		System.out.println(new String(b));*/
			
		//方式2
		byte[] b = new byte[1024];
		while(sis.read(b)!=-1){
			System.out.println(new String(b));
		}	
		sis.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
4.读取时把3个流合并在一起读取
public static void main(String[] args) {
	try {
		FileInputStream fis1 = new FileInputStream("E:/iodemo/a.txt");
		FileInputStream fis2 = new FileInputStream("E:/iodemo/b.txt");
		FileInputStream fis3 = new FileInputStream("E:/iodemo/c.txt");
		//把三个流添加到集合中
		Vector<FileInputStream> vector = new Vector<>();
		vector.add(fis1);
		vector.add(fis2);
		vector.add(fis3);
		//elements方法的返回值类型是Enumeration类型
//		vector.elements();
		//合并在一个序列流中
		SequenceInputStream sis = new SequenceInputStream(vector.elements());
		byte[] b = new byte[fis1.available()+fis2.available()+fis3.available()];
		//数据读取
		int off = 0;
		for(int i = 0;i<vector.size();i++){
			//off是数组中存放数据的起始下标位置
			//read的返回值就是数组中第i个的长度
			off += sis.read(b,off,vector.get(i).available());				
		}
		System.out.println(new String(b));
		sis.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}		
}
5.IO流中的printStream()方法
public static void main(String[] args) {
	// 构造参数传System.out就是在控制台打印信息
//	PrintStream ps = new PrintStream(System.out);
//	ps.print("skdfhskl");		
	PrintStream ps;
	try {
		ps = new PrintStream(new FileOutputStream("E:/iodemo/print.txt"));
		ps.println("床前明月光,");
		ps.println("疑是地上霜。");
		ps.println("举头望明月,");
		ps.println("低头思故乡。");
		ps.flush();
		ps.close();
		System.out.println("打印成功!");
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
}

发布了127 篇原创文章 · 获赞 61 · 访问量 26万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章