java io


java io_第1张图片

package cn.zvc.io;

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 org.junit.Test;

public class TestFileInputOutputStream {
	
	/*
	 *只能适用文本流,不能适用图片、视频等 
	 */
	@Test
	public void testFileWriter(){
		File f1;
		File f2 ;
		FileWriter fw = null;
		FileReader fr = null;
		try {
			f1 = new File("L:/java/study/io_1/src/main/resources/config.properties");
			f2 = new File("L:/java/study/io_1/src/main/resources/config1.properties");
			fr = new FileReader(f1);
			char[] c = new char[24];
			int len;
			fw = new FileWriter(f2);
			while((len = fr.read(c)) != -1){
//				String s = new String(c,0,len);
//				System.out.println(s);
				fw.write(c,0,len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fr != null){
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

	@Test
	public void testFileReader(){
		File file ;
		FileReader fr = null;
		
			try {
				file = new File("L:/java/study/io_1/src/main/resources/config.properties");
				fr = new FileReader(file);
				char[] c = new char[24];
				int len;
				while((len = fr.read(c)) != -1){
					String s = new String(c,0,len);
					System.out.println(s);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				if(fr != null) {
					try {
						fr.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
	}
	
	@Test
	public void testFileInputOutputStream(){
		File f1 ;
		File f2 ;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		f1 = new File("L:/java/study/io_1/src/main/resources/hello.txt");
		f2 = new File("L:/java/study/io_1/src/main/resources/hello3.txt");
		try {
			fis = new FileInputStream(f1);
			byte[] b = new byte[20];
			int len;
			while((len = fis.read(b)) != -1){
				fos = new FileOutputStream(f2);
				fos.write(b, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	
	@Test
	public void testFileInputStream(){
		File file;
		FileInputStream fis = null;
		try {
			file = new File("L:/java/study/io_1/src/main/resources/hello.txt");
			fis  = new FileInputStream(file);
			byte[] b = new byte[5];
			int len;
			while ((len=fis.read(b)) != -1) {
				String s = new String(b);
				System.out.print(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	@Test
	public void testFileOutputStream(){
		File file ;
		FileOutputStream fos = null;
		
		try {
			file = new File("L:/java/study/io_1/src/main/resources/hello2.txt");
			fos = new FileOutputStream(file);
			fos.write(new String("i love java").getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


你可能感兴趣的:(IO)