java 输入输出流概览

今天突然心血来潮想写点java输入输出流,于是花了个把小时动了下手。把输入输出流大体写了下。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

/**
 * 
 * @author AB
 *
 */
public class testInputOutput {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		String path_h = "E:\\testinputoutput\\";
		String path_b = ".txt";
		testInputOutput t = new testInputOutput();
		String s ="我是中国人,钓鱼岛中国的";
		String s1 = null,s2 = null,s3 = null;
		try {
			s1 = new String(s.getBytes(),"UTF-8");
			s2 = new String(s.getBytes(),"GBk");
			s3 = new String(s.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		// write by dataoutputstream
		t.writeByDataOutputStream(path_h+"datainputstream"+path_b, s);  //在文件里结果为:			
		t.writeByDataOutputStream(path_h+"datainputstream_utf"+path_b, s1);  //在文件里结果为:		
		t.writeByDataOutputStream(path_h+"datainputstream_gbk"+path_b, s2);  //在文件里结果为:		
		t.writeByDataOutputStream(path_h+"datainputstream_iso"+path_b, s3);  //在文件里结果为:
		
		//write by fileoutputstream
		
		t.writeByFileOutputStream(path_h+"fileoutputstream"+path_b, s);
		t.writeByFileOutputStream(path_h+"fileoutputstream_utf"+path_b, s1);
		t.writeByFileOutputStream(path_h+"fileoutputstream_gbk"+path_b, s2);
		t.writeByFileOutputStream(path_h+"fileoutputstream_iso"+path_b, s3);
		
		//write by filewriter
		t.writeByFileWriter(path_h+"filewriter"+path_b, s);
		t.writeByFileWriter(path_h+"filewriter_utf"+path_b, s1);
		t.writeByFileWriter(path_h+"filewriter_gbk"+path_b, s2);
		t.writeByFileWriter(path_h+"filewriter_iso"+path_b, s3);
		
		//read
		String path_test = path_h+"test"+path_b;
		System.out.println("datainputstream is beginning:");
		t.readByDataInputStream(path_test);
		System.out.println("fileinputstream is beginning:");
		t.readByFileInputStream(path_test);
		System.out.println("filereader is beginning:");
		t.readByFileReader(path_test);
	}

	//创建文件
	public void creatFIleIfNoExist(String path){
		File f = new File(path);
		if(!f.exists()){
			try {
				new File(path.substring(0, path.lastIndexOf(File.separator))).mkdirs();
				f.createNewFile();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
		
	}
	
	//得到文件
	public File getFile(String path){
		creatFIleIfNoExist(path);
		File f = new File(path);
		return f;
	}
	
	//得到FileInputStream
	public InputStream getFileInputStream(String path){
		File f = getFile(path);
		FileInputStream  fileinputStream = null;
		try {
			fileinputStream = new FileInputStream(f);
			
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		return fileinputStream;
	}
	
	//得到BufferedInputStream
	public InputStream getBufferInputStream(String path){
		File f = getFile(path);
		BufferedInputStream bufferedinputstream = null;
		InputStream in = getFileInputStream(path);
		bufferedinputstream = new BufferedInputStream(in);
		return bufferedinputstream;
	}
	
	//字符流,得到FileReader
	public Reader getFileReader(String path){
		File f = getFile(path);
		FileReader filereader = null;
		try {
			filereader = new FileReader(f);
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return filereader;
	}
	
	//得到InputSreamReader
	public Reader getInputStreamReader(String path){		
		InputStreamReader inputstreamreader = new InputStreamReader(getFileInputStream(path));
		return inputstreamreader;
	}
	
	//得到BufferedReader
	public Reader getBufferedReader(String path){
		BufferedReader bufferedreader = new BufferedReader(getInputStreamReader(path));
		return bufferedreader;
	}
	
	public DataInputStream getDataInputStream(String path){
		DataInputStream datainputstream = new DataInputStream(getFileInputStream(path));
		return datainputstream;
	}
	
	//输出流,得到FileOutputStream
	public OutputStream getFileOutputStream(String path){
		File f = getFile(path);
		OutputStream outputstream = null;
		try {
			outputstream = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return outputstream;
	}
	
	//输出流,得到BufferedOutputStream
	public OutputStream getBufferedOutputStream(String path){
		BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(getFileOutputStream(path));
		return bufferedoutputstream;
	}
	
	//得到DataOutputStream
	public DataOutputStream getDataOutputStream(String path){
		DataOutputStream dataoutputstream = new DataOutputStream(getFileOutputStream(path));
		return dataoutputstream;
	}
	
	//得到FileWriter
	public Writer getFlieWriter(String path){
		File f = getFile(path);
		FileWriter filewriter = null;
		try {
			filewriter = new FileWriter(f);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return filewriter;
	}

	//得到OutputStreamWriter
	public Writer getOutputStreamWriter(String path){
		OutputStreamWriter outputstreamwriter = new OutputStreamWriter(getFileOutputStream(path));
		return outputstreamwriter;
	}
	
	//得到BufferedWriter
	public Writer getBufferedWriter(String path){
		BufferedWriter bufferedwriter = new BufferedWriter(getFlieWriter(path));
		return bufferedwriter;
	}
	
	
	/** 
	* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 
	* 一次读一个字节
	*/ 
	public void readByFileInputStream(String path){
		InputStream in = getFileInputStream(path);
		byte[] buf = new byte[1024];
		try {
			int len = in.read(buf);
			System.out.write(buf, 0, len);
			System.out.println();
			System.out.println(new String(buf,0,len));
			//in.close();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	
	public void writeByFileOutputStream(String path,String s){
		OutputStream out = getFileOutputStream(path);
		byte[] buf;
		buf = s.getBytes();
//		try {
//			buf = s.getBytes("UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO 自动生成的 catch 块
//			e.printStackTrace();
//		}
		try {
			out.write(buf);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	
	public void readByFileReader(String path){
		Reader reader = getFileReader(path);
		char[] buf = new char[1024];
		try {
			int len = reader.read(buf);
			System.out.println(new String(buf,0,len));
			
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
		
	}
	
	public void writeByFileWriter(String path,String s){
		Writer writer = getFlieWriter(path);
		try {
			writer.write(s);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally{
			try {
				writer.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	
	public void readByDataInputStream(String path){
		DataInputStream datainputstream = getDataInputStream(path);
		String tag = "this is datainputstream";
		try {
			byte[] b = new byte[1024];
			int l = datainputstream.read(b);
			System.out.println(new String(b,0,l));
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally{
			try {
				datainputstream.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	
	public void writeByDataOutputStream(String path,String s){
		DataOutputStream dataoutputstream = getDataOutputStream(path);
		try {
			dataoutputstream.writeUTF(s);
			dataoutputstream.writeChars(s);
			dataoutputstream.writeBytes(s);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally{
			try {
				dataoutputstream.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
		
	}
	
	
}

文件输出 为:

文件内容 为(自己对照啊):


这里我还是得先吐槽下CSDN博客的图片真是难弄啊,所以我这里说个结果吧,自己运行去玩玩吧。

以下在文件存的是正确的,其他的都是不可辨认的:

fileoutputstream.txt,fileoutputstream_gbk.txt,filewriter_gbk.txt,filewriter.txt。


在test.txt文件中的内容是:

我是中国人啊,钓鱼岛是中国的。

然后在控制台输出为:

datainputstream is beginning:
我是中国人啊,钓鱼岛是中国的。
fileinputstream is beginning:
我是中国人啊,钓鱼岛是中国的。
我是中国人啊,钓鱼岛是中国的。
filereader is beginning:
我是中国人啊,钓鱼岛是中国的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值