java之 ------ 文件拷贝
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class FileCopyStream { public static void main(String[] args) {
//fileCopy0("b.dat","d:/ex/a/","d:/ex/b/");
//fileCopy1("b.dat","d:/ex/a/","d:/ex/b/");
//fileCopy2("b.dat","d:/ex/a/","d:/ex/b/");
fileCopy2("1.mp3","d:/ex/a/","d:/ex/b/");
//fileCopy3("c.mp3","d:/ex/a/","d:/ex/b/");
//fileCopy3("d.txt","d:/ex/a/","d:/ex/b/"); }
private static void fileCopy0(String fileName, String dir1,String dir2){
try {
FileInputStream in = new FileInputStream(dir1+fileName);
FileOutputStream out = new FileOutputStream(dir2+fileName);
byte[] buffer = new byte[512];
in.read(buffer);
out.write(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
}
} //学习关流
private static void fileCopy1(String fileName, String dir1,String dir2){
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(dir1+fileName);
out = new FileOutputStream(dir2+fileName);
byte[] buffer = new byte[512];
in.read(buffer);
out.write(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
throw new RuntimeException("文件无法关闭");
}
}
}
//可以拷贝大文件
private static void fileCopy2(String fileName, String dir1,String dir2){
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(dir1+fileName);
out = new FileOutputStream(dir2+fileName);
byte[] buffer = new byte[512];
int num = 0;
do{
num = in.read(buffer);
out.write(buffer,0,num);
}while(num>=0); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
throw new RuntimeException("文件无法关闭");
}
}
} //可以拷贝大文件
private static void fileCopy3(String fileName, String dir1,String dir2){
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(dir1+fileName);
out = new FileOutputStream(dir2+fileName);
byte[] buffer = new byte[512];
int num=0;
while(in.available()>0){
num = in.read(buffer); //最简单的加密
for(int i=0;i<num;i++){
buffer[i] = (byte)(buffer[i]+1);
} out.write(buffer,0,num);
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
throw new RuntimeException("文件无法关闭");
}
}
} }
java之 ------ 文件拷贝的更多相关文章
- Java实现文件拷贝的4种方法.
原文地址:http://blog.csdn.net/ta8210/article/details/2073817 使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢? 最近看了看N ...
- java对文件拷贝的简单操作
package fileInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...
- 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇
本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...
- java Servlet文件拷贝的模板代码
//通过response对象获得一个输出流对象 ServletOutputStream os = response.getOutputStream(); //获得要拷贝文件的绝对路径 String r ...
- Java学习-045-目录中文件拷贝
挺晚的了,直接上码.敬请各位小主参阅,若有不足之处,敬请指正,非常感谢! 目录文件拷贝源码: /** * <strong>目录拷贝</strong><br> * & ...
- Java IO和Java NIO在文件拷贝上的性能差异分析
1. 在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...
- (java)从零开始之--异常处理(以文件拷贝为例)
开发过程中避免不了对异常的处理,但是异常的处理又不能乱throw 下面是简单的抛异常处理 public static void CopyFile(String souFile,String dirFi ...
- Java基础IO文件拷贝练习题
/** * 编写一个程序,把指定目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt. */ 1.我们看到这个题还是用大化小的思想来做 分析:1.拷贝 & ...
- Java IO和Java NIO 和通道 在文件拷贝上的性能差异分析
1. 在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...
随机推荐
- CodeForces Contest #1110: Global Round 1
比赛传送门:CF #1110. 比赛记录:点我. 涨了挺多分,希望下次还能涨. [A]Parity 题意简述: 问 \(k\) 位 \(b\) 进制数 \(\overline{a_1a_2\cdots ...
- 修改history记录数门限
你的 Bash 命令历史保存的历史命令的数量可以在 ~/.bashrc 文件里设置.在这个文件里,你可以找到下面两行: HISTSIZE=1000 HISTFILESIZE=2000 HISTSIZE ...
- mysqlbinlog 查看mysql bin 日志 mysqlbinlog: unknown variable 'default-character-set=utf8'
mysqlbinlog mysql-bin.000036 | less 查询包含几个字段的语句: mysqlbinlog mysql-bin.000036| egrep '(201103061000 ...
- linux系统时间不同步解决办法(同步本地时间)
改变/etc/目录下的localtime文件,既可以改变当前的时区 1.方法是到/usr/share/zoneinfo目录下找到你要相对应的时区文件,例如上海在/usr/share/zoneinfo/ ...
- 不能访问本地服务器场。没有注册带有FeatureDependencyId 的 Cmdlet
不能访问本地服务器场.没有注册带有FeatureDependencyId 的 Cmdlet. 原因: 我有两个域管理员账号,分别:sp\administrator,sp\spadmin 其中后者才 ...
- JavaEE之JavaWeb简介
- js字符串操作之substr与substring
substr和substring两个都是截取字符串的. 两者有相同点,如果只是写一个参数,两者的作用都是一样的:就是截取字符串当前下标以后直到字符串最后的字符串片段. 例如: `var a=" ...
- kafka 查看队列信息
https://blog.csdn.net/getyouwant/article/details/81209286?utm_source=blogxgwz8
- vue.js阻止事件冒泡和默认事件
首先我们来看原生JS取消事件冒泡方法: e.stopPropagation(); //非IE浏览器window.event.cancelBubble = true; //IE浏览器 原生JS阻止默认事 ...
- conda设置Python虚拟环境
conda设置Python虚拟环境 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Co_zy/article/details/7741261 ...