java IO 流 读写文件 以及复制

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
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;


public class IOpractic01 {
	
	public static void main(String[] args) {
		//文件路径
		File file = new File("D:\\example.txt");
		
		//字节流写入文件
		outPut(file);
		//字节流读取文件
		input(file);
		//字符流写入文件
		writeOut(file);
		//字符流读取文件
		readerIn(file);
		//文件coppy
		copyFile("D:\\example.txt","D:\\example1.txt");
		bufferOutPut(file);
		writeOutPut(file);
		
		
		
	}
	
	/**
	 * 字节输出流
	 * @param file
	 */
	static void outPut(File file){
		String str="亲爱的  我的头有点晕乎";
		FileOutputStream out =null;
		try {
			out= new FileOutputStream(file);
			byte[] byt = str.getBytes();
			out.write(byt);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	  /**
	   * 字节输出流
	   * @param file
	   */
	static void input(File file){
		FileInputStream in =null;
	   try {
		in = new FileInputStream(file);
		byte[] bte = new byte[1024];
		 int len =in.read(bte);
		System.out.println("文件中的信息是:"+ new String(bte,0,len));
	}
	 catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
	}
	
	/**
	 * 字符输入流
	 * @param file
	 */
	static void readerIn(File file){
		FileReader read =null;
		try {
			 read = new FileReader(file);
			 char[] car = new char[1024];
			 int len = read.read(car);
			 System.out.println("输出的文件结果为:"+new String(car,0,len));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 字符输出流
	 */
	static void writeOut(File file){
		String str ="十年之前,你不认识我,我不认识你";
		FileWriter write =null;
		try {
			write = new FileWriter(file);
			 write.write(str);
			 write.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 文件复制
	 * @param dest
	 * @param path
	 */
	static void copyFile(String path,String  dest){
		File file = new File(dest);
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			//如果文件不存在,则创建
			if(!file.exists()){
				file.createNewFile();
			}
			in = new FileInputStream(path);
			out = new FileOutputStream(file);
			byte[] byt = new byte[1024];
			int len;
			while((len=in.read(byt))!=-1){
				for(int i =0;i<len;i++){
					out.write(byt[i]);
				}
			}
			in.close();
			out.close();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
/*____________________________下面是加入缓冲区_______________________________*/
	
	static void bufferOutPut(File file){
		String str="我是一个小例子";
		FileOutputStream out =null;
		try {
			//这里第二个参数true,是为了不覆盖原有文件,在原有文件后继续添加
			out = new FileOutputStream(file,true);
			BufferedOutputStream buffer = new BufferedOutputStream(out);
			byte[] byt =str.getBytes();
			buffer.write(byt);
			//清空缓冲区
			buffer.flush();
			//缓冲区关闭
			buffer.close();
			//输出流关闭
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		}	
	
	//字符流
	static void writeOutPut(File file){
		String str="我只是一个简单的例子1111";
		FileWriter out = null;
		try {
			out = new FileWriter(file,true);
			BufferedWriter buffer = new BufferedWriter(out);
			buffer.write(str);
			buffer.flush();
			buffer.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
	}




	
	
	

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值