io流 输入流 输出流 字符流

package pro13;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
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.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.Buffer;

import org.junit.Test;

public class FileUtil文件流io流 {

	public void test() {
		
	}
	
	/**
	 * 字節流 inputstream outputstream
	 * @throws IOException 
	 * 
	 */
	
	public  void  testInputStream() throws IOException {
		File file=new File("src/pro13/temp/test");
		InputStream is=null;
		//StringBuffer sbf = new StringBuffer();
		try {
			is=new FileInputStream(file);
			byte[] bytes=new byte[1];
			int length=0;
			length=is.read(bytes);
			System.out.println(length);
			while (length!=-1) {
				for (int i = 0; i < bytes.length; i++) {
					System.out.println((char)bytes[i]);
					//sbf.append((char)bytes);
				}
				length=is.read(bytes);
			}
			System.out.println(length);
			//System.out.println(sbf);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (is!=null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/**
	 * 字节输出流 outputstream
	 * @throws IOException 
	 * 
	 */
	
	public void testOutputStream() throws IOException{
		String path="src/pro13/temp/test"; 
		OutputStream os=null;
		 try {
			os=new FileOutputStream(path, true);
			String words="\rabcde";
			os.write(words.getBytes());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 把文件读入到inputstream 也可以文件的输出了outputstream
	 * 把test文件拷贝到一份新的文件当中去testcopy文件
	 * 1读文件 inputstream 2写文件outputstream
	 * @throws IOException 
	 * 
	 * 
	 */
	
	public void testCopyByte() throws IOException{
		String inpath="src/pro13/temp/test";
		String outpath="src/pro13/temp/testcopy";
		//1 读inpath 2定义一个byte数组作为中转 3将中转的数组数据写到outpath
		InputStream is=null;
		OutputStream os=null;
		byte[] bytes=new byte[1];
		try {
			is=new FileInputStream(inpath);
			os=new FileOutputStream(outpath);
			int length=0;
			while ((length=is.read(bytes))!=-1) {
				os.write(bytes, 0, length);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (is!=null) is.close();
			if (os!=null) os.close();			
		}
	}
	
	
	
	/**
	 * 字符流 针对文本文件提高了效率
	 * @throws IOException 
	 * 
	 */
	@Test
	public void testreader() throws IOException{
			Reader rd=null;
			char[] ch=new char[4];
			StringBuffer sbf =new StringBuffer();
			try {
				rd=new FileReader("src/pro13/temp/test");
				rd.read(ch);
				int length=0;
				while((length=rd.read(ch))!=-1){
					sbf.append(ch, 0, length);
				}
				System.out.println(sbf);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(rd!=null) rd.close();
			}
	}
	
	
	/**
	 * writer
	 * @throws Exception 
	 * 
	 */
	public void testwriter() throws Exception{
		Writer wr=null;
		try {
			wr=new FileWriter("src/pro13/temp/test",true);
			String words="im writer";
			wr.write(words);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (wr!=null) {
				wr.close();
			}
		}
	}
	
	public void copyFileWriter() throws IOException {
		Reader rd=null;
		Writer wr=null;
		char[] ch=new char[2];
		String rdpath="src/pro13/temp/1.jpg";
		String wrpath="src/pro13/temp/1charcopy.jpg";
		int length=0;
		try {
			rd=new FileReader(rdpath);
			wr=new FileWriter(wrpath);
			while((length=rd.read(ch))!=-1){
				wr.write(ch, 0, length);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(rd!=null) rd.close();
			if(wr!=null) wr.close();
		}
		
	}
	
	@Test
	public void testbufferreader() throws Exception{
		Reader rd=null;
		BufferedReader bfr=null;
		StringBuffer sbf=new StringBuffer();
		try {
			rd=new FileReader("src/pro13/temp/test");
			bfr=new BufferedReader(rd);
			String line="";
			while((line=bfr.readLine())!=null){
				sbf.append(line);
			}
			System.out.println(sbf);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			rd.close();
			bfr.close();
		}
	}
	public void testbufferwriter() throws IOException{
		Writer wr=null;
		BufferedWriter bfw=null;
		try {
			wr=new FileWriter("src/pro13/temp/test");
			bfw=new BufferedWriter(wr);
			String words="im bufferwrider";
			bfw.write(words);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			wr.close();
			bfw.close();
		}
	}
}

你可能感兴趣的:(io流 输入流 输出流 字符流)