首页 > 编程语言 > 详细

java写文件读写操作(IO流,字节流)

时间:2014-12-15 23:19:57      阅读:301      评论:0      收藏:0      [点我收藏+]
package copyfile;

import java.io.*;

public class copy {
	public static void main(String[] args) throws IOException {
		 copyFile("d:/new/a.txt","d:/new/b.txt",true);//oldpath,newpath,是否不覆盖前文
	}
	public static void copyFile(String oldpth,String newpath,boolean add) throws IOException{
		FileInputStream in = null;
		FileOutputStream fs = null;
		try {
			//实例化文件,并判断文件是否存在
			File oldfile=new File(oldpth);
			if(oldfile.exists()){
					//初始化文件输入与输出流
					in=new FileInputStream(oldpth);
					fs=new FileOutputStream(newpath,add);
					//定义存放读取数据的数组
					byte[] buffer=new byte[10];
					int length;
					while(true){
						int len=in.read(buffer);//当文件读完,返回-1,否则返回读取文件长度
												/*注:输出读取的当前文件内容方法
												 * String s=new String(buffer);
												 * s.trim();(去除字符串前后两端的空格)
												*/
						if(len==-1)break;
						fs.write(buffer);
					}
					System.out.println("OK");
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			in.close();
			fs.close();
		}
	}
}

  

java写文件读写操作(IO流,字节流)

原文:http://www.cnblogs.com/zxxiaoxia/p/4165998.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!