Java实现本地 fileCopy
前言:
Java中流是重要的内容,基础的文件读写与拷贝知识点是很多面试的考点。故通过本文进行简单测试总结。
2.图展示【文本IO/二进制IO】(这是参考自网上的一张总结图,相当经典,方便对比记忆)

3.文本复制的实验Java实现code:
package com.gdufe.io; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class TestInputOutputStream { // private final static String SOURCE="t.txt";
// private final static String TARGET="tt.txt";
// private final static String SOURCE="p.png";
// private final static String TARGET="pp.png";
private final static String SOURCE="D:\\Tool_Software\\mysql-installer-community-5.6.23.0.msi";
private final static String TARGET="mysql-installer-community-5.6.23.0.msi";
//D:\Tool_Software\Ryuyan.zip public static void main(String[] args) {
// TestInputOutputStream.copy1(SOURCE, TARGET);
// TestInputOutputStream.copy2(SOURCE, TARGET);
TestInputOutputStream.copy3(SOURCE, TARGET);
System.out.println("--End---");
} public static void copy1(String src,String tar){ InputStream input = null;
OutputStream output = null;
try{
input = new FileInputStream(src);
output = new FileOutputStream(tar);
int r;
while((r=input.read())!=-1){
output.write((byte)r);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
public static void copy2(String src,String tar){
InputStream input = null;
OutputStream output = null;
try{
input = new FileInputStream(src);
output = new FileOutputStream(tar);
int length=0;
byte []b = new byte[1024];
while((length=input.read(b))!=-1){
output.write(b,0,length);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} public static void copy3(String src,String tar){
InputStream input = null;
OutputStream output = null;
try{
input = new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tar)));
/***经过亲测,buffer缓冲流读取时确实更快一些****/ int length=0;
byte []b = new byte[1024]; //先将stream读入字节数组
while((length=input.read(b))!=-1){
output.write(b,0,length);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(附:参考代码时注意准备好测试文件,不然出现异常“file can't be found”!
文件拷贝到Java工程的直接目录下,刷新project可查看!
)
Java实现本地 fileCopy的更多相关文章
- java调用本地方法的时候报错 could not find the main class:xx.program will exit
如图所示,当在java调用本地方法的时候报错 我的解决办法是把dll文件放到System.out.println(System.getProperty("java.library.path& ...
- Android使用JNI(从java调用本地函数)
当编写一个混合有本地C代码和Java的应用程序时,需要使用Java本地接口(JNI)作为连接桥梁.JNI作为一个软件层和API,允许使用本地代码调用Java对象的方法,同时也允许在Java方法中调用本 ...
- [转载]java调用本地dos命令
在社区看到java调用本地dos命令的代码,特贴出来 String command = "ipconfig"; Runtime run = Runtime.getRuntime() ...
- Java调用本地方法又是怎么一回事
JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ...
- java获取本地计算机MAC地址
java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...
- 纯Java获得本地MAC地址
import java.net.*; public class Ipconfig{ public static void main(String[] arguments) throws Ex ...
- Java获取本地IP地址
import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...
- Java读取本地文件,并显示在JSP文件中
当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...
- 使用JAVA打开本地应用程序相关的文件
在该项目中需要运行本地文件或应用程序,JDK6添加后Desktop类别.可以直接使用.这使得有可能在程序中无论什么应用程序可以打开的.例:打开pdf文件,当地福昕是默认打开.执行程序将使用福昕开放pd ...
随机推荐
- noip模拟赛(10.4) 背包(pack)
[题目描述] 蛤布斯有n种商品,第i种物品的价格为ai,价值为bi.有m个人来向蛤布斯购买商品,每个人每种物品只能购买一个.第j个人有cj的钱,他会不停选择一个能买得起的价格最高的商品买走(如果有多个 ...
- 'gulp'不是内部或者外部命令,也不是可运行的程序或批处理文件
1,在用户变量里新建变量 PATH: %USERPROFILE%\AppData\Roaming\npm(如果已有path变量,则在后面直接加上即可) 2,在系统环境变量里的path加上node.js ...
- ie6-ie8中不支持opacity透明度的解决方法
ie6-ie8中是不支持的,需要加上下面这句话:filter: alpha(opacity=70);此外这种效果不能用ietester中的ie6测试,因为ietester的ie6这样写也是不透明的,但 ...
- od破解实例
百度经验: http://jingyan.baidu.com/article/636f38bb4091e4d6b84610a8.html pc6 http://www.pc6.com/edu/6278 ...
- Centos5.8 安装 MySQL5.6.19
查看已经安装的mysql: sudo yum list installed |grep mysql 删除 sudo yum remove mysql 安装 sudo rpm -ivh MySQL-se ...
- mysql 判断 字段是否为空
SB.Append("select "); SB.Append("mpe.EVIDENCE_ID "); SB.Append("left join m ...
- UOJ #150 【NOIP2015】 运输计划
题目描述 公元 \(2044\) 年,人类进入了宇宙纪元. \(L\) 国有 \(n\) 个星球,还有 \(n-1\) 条双向航道,每条航道建立在两个星球之间,这 \(n-1\) 条航道连通了 \(L ...
- 探索Windows 8.1 Update 新功能点
Windows 8.1 Update 已经使用一段时间了,整体感觉比Windows 8.1 方便了不少,尤其是对鼠标用户来说更是进行了很多优化. 应用磁贴尺寸 在应用磁贴点击鼠标右键,有小.中.宽.大 ...
- HTML5添加 video 视频标签后仍然无法播放的解决方法 IIS添加MIEI类型
现象:插入如下代码后仍然无法看视频(注:视频已确认为浏览器支持格式) <video controls="controls" width="500px" h ...
- 常用 redis 命令(for php)
Redis 主要能存储 5 种数据结构,分别是 strings,hashes,lists,sets 以及 sorted sets. 新建一个 redis 数据库 $redis = new Redis( ...