Java IO 文件流的读,文件流写Writer,Reader,InputStream,OutputStream封装

package com.app.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;

/**
 *@DEMO:napp
 *@Author:jilongliang
 *@Date:2013-7-25
 *
 *实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件。
 *
 *字符流和字节流是根据处理数据的不同来区分的。字节流按照8位传输,字符流按照16位传输
 *由于字符流使用Unicode字符集,支持多国文字,因此若流要跨越多种平台传输,应使用字符流。
 *
 *char型是字符型,占2个字节,默认数值'\u0000',取值范围'\u0000'~'\uffff'
 *byte是字节型,占1个字节,默认数值0,取值范围-128~127
 *boolean 无符号 8  位 
 *byte  无符号,8  位 
 *char  无符号,16 位
 *short 有符号,16 位
 *int \ 有符号,32 位
 *long 有符号,64 位 
 *float  32 位 
 *double  64 位
 *
 *字节流输出--->>程序-->>字节流---->文件(直接操作文件)
 *
 *字符流输出--->>程序-->>字符流---->>缓存--->>文件(数据线放在缓存,之后再从缓存写入文件)
 *
 *
 *@Description字节流
 *@InputStream
 *	  FileInputStream, FilterInputStream,BufferedInputStream, CheckedInputStream, CipherInputStream,
 *    DataInputStream, DeflaterInputStream, DigestInputStream, InflaterInputStream, LineNumberInputStream,
 *    ProgressMonitorInputStream, PushbackInputStream ,AudioInputStream, ByteArrayInputStream, FileInputStream,
 *    ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream 
--------------------------------------------------------------------------------
 *@OutputStream
 *	  FileOutputStream, FilterOutputStream,BufferedOutputStream, CheckedOutputStream, CipherOutputStream,
 *    DataOutputStream, DeflaterOutputStream, DigestOutputStream, InflaterOutputStream, LineNumberOutputStream,
 *    ProgressMonitorOutputStream, PushbackOutputStream ,AudioOutputStream, ByteArrayOutputStream, FileOutputStream,
 *    ObjectOutputStream, PipedOutputStream, SequenceOutputStream, StringBufferOutputStream 
 *
--------------------------------------------------------------------------------
 *
 *@Description字符流
 *@Writer
 *   BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter 
 *
 *@Reader
 *
 *BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader 
 *
 */
public class IO {
	private static String content = "",line=System.getProperty("line.separator");//换行相当于\n
	private static File f=new File("");
	public static void main(String[] args) {
		//String  separator=f.separator;//与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串
		String path = "D:/test/a.txt";
		//String text = reader1(path);
		//writer1(path, "D:/test/a1.txt");
		//writer2(path, "D:/test/c.txt");
		//writer3(path, "D:/test/d.txt");
		//writer4(path, "D:/test/d.txt");
		//char  c=reader2(path);
		//System.out.print(c);
		//String text=reader2(path);
		//System.out.println(text);
		//writer5(path,"d:/test/f.txt");
		//writer6(path,"d:/test/e.txt");
		String s=reader3(path);
		System.out.println(s);

	}

	/**
	 * 读文件流
	 * @param formPath从哪里读取的文件路径
	 * @return
	 */
	public static String reader1(String formPath) {
		FileReader read = null;
		BufferedReader reader = null;
		try {
			read = new FileReader(new File(formPath));
			reader = new BufferedReader(read);
			StringBuffer buffer = new StringBuffer("");
			content = reader.readLine();
			while (content != null) {
				buffer.append(content).append(line);
				content = reader.readLine();
			}
			return content = buffer.toString();//返回
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)reader.close();
				if (read != null)read.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "";//没值就返回空
	}
	
	/**
	 * 读文件
	 * @param formPath
	 * @return
	 */
	public static String reader2(String formPath){
		try {
			InputStream is=new FileInputStream(new File(formPath));
			InputStreamReader reader=new InputStreamReader(is);
			//不够大的时候读不出来全部内容来的,他不像available拿多所就读多少
			char c[] = new char[1024*1024];//1M=1024*1024,根据需求定
			int len = reader.read(c); 
			content=new String(c, 0, len);
			if(reader!=null)reader.close();
			//if(is!=null)is.close();
			return content;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * InputStreamReader
	 * ByteArrayOutputStream
	 * ByteArrayInputStream
	 * @param formPath
	 * @return
	 */
	public static String reader3(String formPath){
		String newName="";
		try {
			InputStream is=new FileInputStream(new File(formPath));
			InputStreamReader reader=new InputStreamReader(is);
			//不够大的时候读不出来全部内容来的,他不像available拿多所就读多少
			char c[] = new char[is.available()];//1M=1024*1024,根据需求定
			int len = reader.read(c); 
			content=new String(c, 0, len);
			
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			DataOutputStream dout = new DataOutputStream(bout);
			dout.writeUTF(content);
			
			byte[] buff = bout.toByteArray();
			ByteArrayInputStream bin = new ByteArrayInputStream(buff);
			DataInputStream dis = new DataInputStream(bin);
			newName = dis.readUTF();
			
			bout.flush();
			dout.flush();
			bout.close();
			dout.close();
			dis.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newName;
	}
	
	/**
	 * 写文件
	 * BufferedWriter
	 * BufferedReader
	 * @param formPath 
	 * @param toPath
	 */
	public static boolean writer1(String formPath,String toPath){
		Reader reader=null;
		Writer writer=null;
		boolean flag=true;
		BufferedWriter buffWriter=null;
		BufferedReader buffReader=null;
		try {
			reader=new FileReader(new File(formPath));
			writer = new FileWriter(new File(toPath));//writer不能关闭
			
			buffWriter=new BufferedWriter(writer);//这个写完可以关闭
			buffReader=new BufferedReader(reader);
			
			content=buffReader.readLine();
			while(content!=null){
				buffWriter.write(line+content);
				content=buffReader.readLine();
				buffWriter.flush();//只要用到缓冲区就flush//其实关闭了缓冲区,就是关闭缓冲区中流的对象. //写一次flush是为了防止停电就挂了~
			}
			reader.close();
			buffReader.close();
			buffWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
		return flag;
	}
	

	/**
	 * 写文件
	 * PrintWriter
	 * Writer
	 * Reader
	 * BufferedReader
	 * @param formPath 
	 * @param toPath
	 */
	public static boolean writer2(String formPath,String toPath){
		Reader reader=null;//FileReader reader=null;
		PrintWriter writer=null;//PrintWriter
		boolean flag=true;
		BufferedWriter buffWriter=null;//此对象有换行newLine
		BufferedReader buffReader=null;//为了提高效率,使用字符缓冲流BufferedReader  
		try {
			reader=new FileReader(new File(formPath));
			writer = new PrintWriter(new File(toPath));//writer不能关闭
			
			buffWriter=new BufferedWriter(writer);//这个写完可以关闭
			buffReader=new BufferedReader(reader);
			
			content=buffReader.readLine();
			while(content!=null){
				buffWriter.write(line+content);
				content=buffReader.readLine();
			}
			reader.close();
			buffReader.close();
			buffWriter.close();
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
		return flag;
	}
	
	/**
	 * 写文件
	 * InputStream
	 * OutputStream
	 * FileInputStream
	 * FileOutputStream
	 * @param from
	 * @param to
	 */
	public static void writer3(String from, String to) {  
        try {  
            InputStream in = new FileInputStream(new File(from));  
            OutputStream out = new FileOutputStream(new File(to)); 
			byte[] buff = new byte[in.available()];  
            //byte[] buff = new byte[1024 * 1024];  
            int len = 0;  
            while ((len = in.read(buff)) != -1) {  
                out.write(buff, 0, len);  
            }  
            in.close();  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
	/**
	 * 写文件
	 * @param from
	 * @param to
	 */
	public static void writer4(String from, String to) {  
		try {  
			InputStream in = new FileInputStream(new File(from));  
			OutputStream out = new FileOutputStream(new File(to));  
			byte[] buff = new byte[in.available()];  
			in.read(buff);
			out.write(buff);
			in.close();  
			out.close();  
		} catch (Exception e) {  
			e.printStackTrace();  
		}  
	}  
	/**
	 * 写文件
	 * @param formPath
	 * @param toPath
	 * @return
	 */
	public static boolean writer5(String formPath,String toPath) {
		try {
			InputStream input=new FileInputStream(new File(formPath));
			OutputStream output=new FileOutputStream(new File(toPath));
			byte [] byf=new byte[1024*1024];//这里就不能填那么大啦 byte不同char,byte
			int len=0;
			while((len=input.read())!=-1)
			{
				//input.read(byf);//读
				output.write(byf, 0, len); //output.write(byf);//写
			}
			if(output!=null)output.close();
			if(input!=null)input.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
		return true; 
	}
	/**
	 * 文件处理
	 * Writer
	 * OutputStreamWriter
	 * @param content
	 * OutputStreamWriter
	 * @param toPath写到那个路径下
	 * @return
	 */
	public static boolean writer6(String content, String toPath) {  
        boolean flag = true;  
        try {  
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toPath), "utf-8"));//设置编码  
            out.write(line + content);  
            out.close();  
        } catch (Exception ex) {  
            ex.printStackTrace();  
            return false;  
        }  
        return flag;  
    }  
	/**
	 * BufferedOutputStream 套DataOutputStream套FileOutputStream
	 * 据说管道套管道效率效率比较高,具体没测试过!
	 * BufferedInputStream bis = new BufferedInputStream(new DataInputStream(new FileInputStream(new File(formPath))));
	 * 
	 * @param formPath
	 * @param toPath写到那个路径下
	 * @return
	 */
	public static boolean writer7(String formPath, String toPath) {  
		boolean flag = true;  
		/**
		 *这里就不能填那么大啦 byte不同char,byte
		 */
		byte[] byt = new byte[1];
		try {
			//BufferedInputStream bis = new BufferedInputStream(new FileInputStream(formPath));  
			BufferedInputStream bis = new BufferedInputStream(new DataInputStream(new FileInputStream(new File(formPath))));  
			DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(toPath)));
		    BufferedOutputStream bos=new BufferedOutputStream(dos);
		   while(bis.read(byt)!=-1)  
            {  
			   bos.write(byt); 
			   System.out.println("文件已经写完!");
            }  
		   bos.flush();
		   bos.close();
		} catch (Exception ex) {  
			ex.printStackTrace();  
			return false;  
		}  
		return flag;  
	}  
	/**
	 * 
	 * BufferedInputStream
	 * ByteArrayOutputStream
	 * @param formPath
	 * @param toPath
	 * @return
	 */
	public static boolean writer8(String formPath,String toPath){
		try {
			BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File(formPath)));

			ByteArrayOutputStream baos=new ByteArrayOutputStream();

			int c=bis.read();//读取bis流中的下一个字节
			while(c!=-1){
			     baos.write(c);
			     c=bis.read();
			}
			bis.close();
			byte byt[]=baos.toByteArray();
			
			FileOutputStream out=new FileOutputStream(new File(toPath));
			out.write(byt);
			out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的蹭蹭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值