IO流

本文深入探讨Java IO流的概念、分类及应用,包括字符流、字节流、缓冲流等,通过实例演示如何实现文件的复制、读取与写入。

IO流概述

操作硬盘或者是网络,等待的文件

流的分类

按照流向分

       输出流-----java写文件

       输入流-----java读取文件

按照操作文件来分

       字符流,操作文本文件(只要用记事本打开的),字符流中内置了编码表,查表

       字符输入流(读取文件)

              抽象基类 Reader

       字符输入流(写入文件)

              抽象基类 OutputStream

字符输出流(写文本文件)

writer

    fileWriter

              建立对象的时候FileWriter(String fileName)字符串格式文件名

              调用write方法,写数据

              将内存中的数据,刷新到目的地(硬盘文件),调用flush

              close关闭资源

import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
	public static void main(String[] args) throws IOException {
		FileWriter fw = new FileWriter("D:\\Demo.txt");
		/*
		 * 1.在堆内存新建了一个对象 new FileWriter
		 * 2.抛出异常
		 * 3.在硬盘上创建了一个文件,如果有这个文件,直接覆盖
		 * 4.JVM调用windows中的资源,才创建的文件
		 */
		fw.write("我是一个大好人");
		fw.flush();
		fw.close();
	}
}

字符输出流(读文本文件)

       Reader

        FileReader

                     建立对象的时候FileReader(String fileName)字符串格式文件名

                     调用read方法,读数据,单个字符,返回int,read方法,一个一个往下读,读到结尾返回-1

                     read(字符数组)read方法读取字符的时候,将读取的字符存储在了数组中

                     返回值,表示的是,数组中存储字符的有效个数

import java.io.*;
public class FileReaderDemo {
	public static void main(String[] args) {
		
	}
	public static void method_2(){
		FileReader fr = null;
		try {
			fr = new FileReader("D:\\hahaha.txt");
			int len = 0;
			char[] ch = new char[1024];	
			while((len=fr.read(ch))!=-1){
				System.out.println(new String(ch,0,len));
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			try {
				if(fr!=null)
					fr.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
	public static void method_1(){
		FileReader fr = null;
		try {
			fr = new FileReader("D:\\hahaha.txt");
			int len = 0;
			while((len=fr.read())!=-1){
				System.out.print((char)len);
			}		
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			try {
				if(fr!=null)
					fr.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
}

通过字符流,复制文本文件

       FileReader读取文件

       FileWriter 写入文件

拷贝文件1

import java.io.*;
public class CopyText1 {
	public static void main(String[] args) {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fr = new FileReader("D:\\haha.txt");
			fw = new FileWriter("D:\\hehe.txt");
			int len = 0;
			while((len = fr.read())!=-1){
				fw.write(len);
				fw.flush();
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException("文件拷贝异常");
		}
		finally{
			try {
				if(fw!=null)
					fw.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				System.out.println("fw关闭文件异常");
			}
			
			try {
				if(fr!=null)
					fr.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				System.out.println("fr关闭文件异常");
			}
		}
	}
}

拷贝文件2

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyText2 {
	public static void main(String[] args) {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fr = new FileReader("D:\\haha.txt");
			fw = new FileWriter("D:\\heihei.txt");
			char[] ch = new char[1024];
			int len = 0;
			while((len = fr.read(ch))!=-1){
				//fw.write(new String(ch,0,len));
				fw.write(ch, 0, len) ;
				fw.flush();
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("文件拷贝异常");
		}
		finally{
			try {
				if(fw!=null)
					fw.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				System.out.println("fw关闭异常");
			}
			try {
				if(fr!=null)
					fr.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				System.out.println("fr关闭异常");
			}
		}
	}
}
在字符流中,java工程师,提供2个字符流缓冲区对象,提供文件的操作

BufferedWriter

       构造方法,传递的Writer的子类对象,FileWriter

       newLine() 写一个换行,跨平台,JVM是linux版本 \r JVM是windows \r\n

       在linux下,\r \r\n写在linux\n多一个字符

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
	public static void main(String[] args) {
		FileWriter fw = null;
		BufferedWriter bw = null;
		try{
			fw = new FileWriter("D:\\xixi.txt");
			bw = new BufferedWriter(fw);
			bw.write("sadkljfa;sgljalksjgalksgjasg" +
					"sfdajfkla;fjkaslkjfsaldk;gjaksl");
			bw.newLine();
			bw.write("sdjkkfasdfas");
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try {
				if(bw!=null)
				bw.close();
			} catch (IOException e2) {
				// TODO: handle exception
			}
		}
	}
}

BufferedReader

       构造方法,传递的子类对象,FileReader

       String readLine()读取文本一行


import java.io.*;

public class BufferedReaderDemo {
	public static void main(String[] args) {
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader("D:\\hahaha.txt");
			br = new BufferedReader(fr);
			String s = null;
			while((s=br.readLine())!=null){
				System.out.print(s);
			}
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
}

拷贝文件3

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyText3 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FileReader fr = null;
		FileWriter fw = null;
		BufferedReader br = null;
		BufferedWriter bw = null;
		try{
			fr = new FileReader("D:\\hahaha.txt");
			br = new BufferedReader(fr);
			fw = new FileWriter("D:\\heiheihei.txt");
			bw = new BufferedWriter(fw);
			String line = null;
			while((line = br.readLine())!=null){
				 bw.write(line);
				 bw.newLine();
				 bw.flush();
			}
			
		}catch(IOException e){
			e.printStackTrace();
			System.out.println("文件拷贝异常");
		}finally{
			try {
				if(bw!=null)
					bw.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				System.out.println("bw关闭流异常");
			}
			try {
				if(br!=null)
					br.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				System.out.println("br关闭流异常");
			}
		}
	}
}

装饰设计(装饰类)

为了解决现实中的问题,增强原有对象的功能而出现的,原来对象中的功能不用强大

增强对象中的功能,装饰模式出现,增强原有对象的功能

热水器,加热水,水管,进,出水管

装饰模式在IO中的应用,写入文件用的FileWriter,发现这个类的功能不够强大

         装饰类出现了BufferedWriter,增强原有FileWriter的功能,在原有FileWriter的基础上增加一个newLine(),但是BufferedWriter写一行的方法,底层用的还是FileWriter的方法

BufferedWriter构造方法BufferedWriter(Writer out)

 

继承原有类,覆盖方法,和采用装饰类来增强原有对象中的功能,有区别么? 区别在哪?

继承是面向对象三大特征之一,装饰设计模式,解决实际问题的

采用继承的方式,来增强对象中的功能,过于庞大,臃肿

例如:

文件写入

      |-- 文本写入类 TextWriter

         |-- extends TextWriter

 

      |-- 音频写入类 SoundWriter

         |-- extends SoundWriter

 

      |-- 可执行文件写入类 EXEWriter

         |-- extends EXEWriter

 

      |-- 镜像文件写入类 ISOWriter

         |-- extends ISOWriter

 

      |-- 数据库文件写入类 DatabaseWriter

         |-- extends DatabaseWriter

 

 

文件写入--Writer类

     |-- 文本写入类 TextWriter

     |-- 音频写入类 SoundWriter

     |-- 可执行文件写入类 EXEWriter

     |-- 镜像文件写入类 ISOWriter

     |-- 数据库文件写入类 DatabaseWriter

     |-- 装饰类(Writer out),增强它们功能

        |-- BufferedWriter(Writer out)extends Writer{

                              out.write()

}

                   文件的写入体系,简单,又清楚,方便使用,和学习

                        BufferedReader(Reader in)

自定义类实现readLine()方法

import java.io.IOException;
import java.io.Reader;

/*
 * 实现文本一行读取,返回字符串
 */
public class MyReadLine {
	private Reader r;
	public MyReadLine(Reader r){
		this.r = r ;
	}
	
	public String myReaderLine() throws IOException{
		StringBuffer sb = new StringBuffer();
		int len = 0;
		while((len=r.read())!=-1){
			if(len=='\r')
				continue;
			if(len=='\n')
				return sb.toString();
			else
				sb.append((char)len);
		}
		if(sb.length()!=0)
			return sb.toString();
		return null;
		
	}
	public void myclose() throws IOException{
		r.close();
	}
}
import java.io.FileReader;
import java.io.IOException;
public class MyReadLineTest {
	public static void main(String[] args) {
		MyReadLine mrl = null;
		try {
			mrl = new MyReadLine(new FileReader("D:\\demo.txt"));
			String line = null;
			while((line=mrl.myReaderLine())!=null){
				System.out.println(line);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException("文件读取失败");
		} finally{
			try {
				if(mrl!=null)
					mrl.myclose();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
				System.out.println("文件关闭异常");
			}
		}
				
	}
}

字节流

读和写用的都是字节数组

字节输入流 InputStream

         FileInputStream

字节输出流 OutputStream

         FileOutputStream

 

字节流复制任意文件

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StreamCopyFile {
	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("D:demo.txt");
			fos = new FileOutputStream("D:demo1.txt");
			int b = 0;
			while((b=fis.read())!=-1){
				fos.write(b);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException("文件复制异常");
		} finally{
			
			try {
				if(fos!=null)
				fos.close();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
				throw new RuntimeException("文件关闭异常");
			}
			
			try {
				if(fis!=null)
				fis.close();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
				throw new RuntimeException("文件关闭异常");
			}
		}
		
		
	}
}

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StreamCopyFile1 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("D:\\WLAN_Azurewave_GE781_Win7_32_z800316.zip");
			fos = new FileOutputStream("D:\\haha.zip");
			byte[] b = new byte[1024];
			int len = 0;
			while((len=fis.read(b))!=-1){
				fos.write(b);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException("文件复制异常");
		} finally{
			try {
				if(fos!=null)
					fos.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				throw new RuntimeException("文件关闭异常");
			}
			
			try {
				if(fis!=null)
					fis.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				throw new RuntimeException("文件关闭异常");
			}
		}
	}
}

import java.io.*;
public class StreamCopyFile2 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			fis = new FileInputStream("D:\\WLAN_Azurewave_GE781_Win7_32_z800316.zip");
			fos = new FileOutputStream("D:\\haha.zip");
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			byte[] b = new byte[bis.available()];
			int len = 0;
			while((len=bis.read(b))!=-1){
				bos.write(b, 0, len);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException("复制文件失败");
		} finally{
			try {
				if(bos!=null)
					bos.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				throw new RuntimeException("关闭文件失败");
			}
			
			try {
				if(bis!=null)
					bis.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
				throw new RuntimeException("关闭文件失败");
			}
		}
	}
}

-----------android培训java培训、java学习型技术博客、期待与您交流!------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值