Java IO学习总结

最近没事,就把以前学过的一些基础东西回顾回顾总结总结。这两天又看了看IO,也大概有了一个知识框架,下图就是IO这块的知识体系。

所谓IO(Input Output):是用来处理设备之间数据的传输,java对数据的操作都是通过流的方式,java中用来操作流的对象都在java.io包中。 java中IO流按照流向分为:输入流和输出流;按照操作的数据类型分为:字节流和字符流。由于数据在计算机中的最终是通过0101这样二进制数据进行保存和运算的,所以字节流可以处理任意数据类型,比如音视频文件、图片、文本等数据;而字符流是专门为了方便操作文本数据而生的,它结合了字符编码,在创建字符流的时候,java会默认使用平台的字符编码,当然我们也可以指定字符编码。




1.字节流

1).字节流抽象基类
    InputStream   , OutputStream

2).字节流中 FileInputStream  , FileOuputStream基本操作
    在操作图片,音视频数据的时候就需要使用到字节流

    //使用FileOutputStream往文件中写入数据
    public static void fileOutputStreamDemo(){
	FileOutputStream fos = null;
	try{
		//1.创建字节输入流对象,并于文件fos.txt关联,
		fos = new FileOutputStream("fos.txt");
		//2.往字节流中输入字节数据
		fos.write("FileOutputStream Demo".getBytes());  //将字节数据写入到文件中
		fos.flush();   //该方法是一个空实现;因为不存在缓冲,直接操作的字节数据
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		//3.关闭资源
		close(fos);
	}
}

//使用FileInputStream从文件中读取数据
public static void fileInputStreamDemo(){
	FileInputStream fis = null;
	try{
		fis = new FileInputStream("fos.txt");
		int len = 0;
		byte[] buf = new byte[1024];
		while((len=fis.read(buf)) != -1){
			System.out.println(new String(buf,0,len));
		}
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		close(fis);
	}
}


//使用字节流拷贝文件
public static void copyFile(){
	FileInputStream fis = null;
	FileOutputStream fos = null;
	try{
		fis = new FileInputStream("fos.txt");
		fos = new FileOutputStream("fos_copy.txt");
		int len = 0;
		byte[] buf = new byte[1024];
		while((len=fis.read(buf)) != -1){
			fos.write(buf,0,len);
		}
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		close(fis);
		close(fos);
	}
}

//使用带有缓冲区的BufferedInputStream ,BufferedOutputStream拷贝文件

public static void copyFileWithBuffer(){
	BufferedInputStream bis = null;
	BufferedOutputStream bos = null;
	try{
		bis = new BufferedInputStream(new FileInputStream("fos.txt"));
		bos = new BufferedOutputStream(new FileOutputStream("fos_copy.txt"));
		int by = 0;
		while((len=bis.read(by)) != -1){
			bos.write(by);
		}
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		close(bis);
		close(bos);
	}
}

2.字符流

1).字符流抽象基类
    Reader   ,   Writer 

2).字符流中FileReader , FileWriter基本操作  

//使用FileWriter创建文件并写入数据 , 注意FileWriter使用了默认编码,即系统本身编码
 public static void fileWriterDemo(){
	FileWriter fw = null;
	try{	
		fw = new FileWriter("demo.txt");     //在指定目录下创建demo.txt文件,无论有没有都会创建该文件,所以如果文件存在,就会覆盖掉原文件;
	//	fw = new FileWriter("demo.txt" , true);  // 在指定目录下创建文件,true表示不覆盖文件,并在已有文件的末尾进行续写;
		fw.write("FileWriter Demo");   // 将字符串写入到流中
		fw.flush();  // 刷新流对象,将流中缓冲的数据写入到文件中
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		try{
			// 关闭资源,在关闭之前会调用flush()方法将内部缓存中的数据写入到文件中去
			// 调用close()方法之后,不能在往流里面继续写入数据;
			if(fw != null)
				fw.close();    
		}catch(IOException e){
			throw new RuntimeException("关闭资源失败");
		}
	}	
}


//使用FileReader读取文件并打印到控制台上
public static void fileReaderDemo(){
	FileReader fr = null;
	try{
		// 创建文件读取流对象,和指定名称的文件关联,如果文件不存在,则抛出FileNotFoundException
		fr = new FileReader("demo.txt"); 
		// 使用read()方法读取数据;如果读到流末尾时候返回-1;
		int len = 0;
		char[] buf = new char[1024];
		while((len=fr.read(buf)) != -1){   // 将读入的数据存入到字符数组中,返回读取数据的长度,当到达文件末尾时返回-1
			System.out.println(new String(buf , 0 ,len));
		}
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		try{
			if(fr != null)
				fr.close();
		}catch(IOException e){
			throw new RuntimeException("resource closed fail");
		}
	}	
}


// 完整的copy文件
public static void copyFileDemo(){
	FileReader fr = null;
	FileWriter fw = null;
	try{
		fr = new FileReader("demo.txt");      // 创建输入流对象,与文件demo.txt关联
		fw = new FileWriter("demo_copy.txt"); // 创建输出流对象,
		int len = 0;
		char[] buf = new char[1024];
		while((len=fr.read(buf)) != -1){    // 读取数据到字符数组缓冲区中,返回值为读取字符的长度,当返回-1时,到达文件末尾
			fw.write(buf, 0, len);       // 将字符数组中的数据写入到输出流中
			fw.flush();                // 通过flush()将流中的数据写入到文件中;
		}
	}catch(IOException e){
		throw new RuntimeException("copy failed");
	}finally{
		close(fr);
		close(fw);
	}
}

3).字符流缓冲区来提高读写效率(BufferedWriter ,BufferedReader 的使用)

//BufferedReader 读取数据
public static void bufferReaderDemo(){
	FileReader fr = null;
	BufferedReader br = null;
	try{
		//1.创建一个字符读取流对象
		fr = new FileReader("buf.txt");
		//2.创建BufferedReader对象
		br = new BufferedReader(fr);
		//3.读取数据
		String line = null;
		while((line=br.readLine()) != null){  //当读取到文件末尾时,readLine方法返回null
			System.out.println(line);
		}
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		close(fr);
		close(br);
	}
}

//BufferedWriter 写入数据
public static void bufferWriterDemo(){
	FileWriter fw = null;
	BufferedWriter bw = null;
	try{
		//1. 创建一个字符写入流对象
		fw = new FileWriter("buf.txt");
		//2.为了提高字符写入效率,加入缓冲技术,
		bw = new BufferedWriter(fw);
		//3.写入数据
		bw.write("bufferedWriter Demo....");
		bw.newLine();  //换行
		//4.刷新数据
		bw.flush();
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		close(fw);
	}
}

//使用BufferedReader,BufferedWriter复制文件
public static void copyFileByBuffer(){
	BufferedReader br = null;
	BufferedWriter bw = null;
	try{
		br = new BufferedReader(new FileReader("buf.txt"));
		bw = new BufferedWriter(new FileWriter("buf_copy.txt"));
		String line = null;
		while((line=br.readLine()) != null){   // readLine()方法不包含换行符
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		close(br);
		close(bw);
使用的的工具类

 

/**
 * 关闭资源
 * @param obj
 */
public static void close(Closeable obj){
	try{
		if(obj !=null)
			obj.close();
	}catch(IOException e){
		e.printStackTrace();
	}
}  


	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值