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 ...
随机推荐
- Js经典相册
Js经典相册 点击下载
- Entity Framework连接Mysql数据库并生成Model和DAL层
Entity Framework (EF,ADO.NET Entity Framework)是微软官方提供的.NET平台的ORM框架.相比于LINQ TO SQL,EF框架具有很明显的优势: EF框架 ...
- Gruntjs: grunt-contrib-jst
预编译Underscore模板到JST文件(Underscore:JS工具库) generate JavaScript template functions Gruntfile的配置实例: modul ...
- 解决.VS2012+EF5.0开发的网站在window server2003上无法部署的问题
(一)前 言 最近一个月使用VS2012(默认框架是.net f ...
- 产品列表页分类筛选、排序的算法实现(PHP)
一.简单的单条件查询 工作都是从简单的开始,先从最简单的单表查询开始,这个一般用在首页以及一些比较独立的页面,只需要查找几个符合条件的产品展示出来即可,可以使用分页或者不使用分页.下面这个是产品控制器 ...
- Debian8修改启动默认运行级别
Two things you need to know: 1) Systemd boots towards the target given by "default.target" ...
- Visual Studio 2015初体验——前端开发工作的问题
前言 因为后台项目开发适用的VS2015,为了跟后台开发配合,前端部门也统一从VS2013升级到了VS2015. 因为C盘空间不足要先卸载,这里就不说卸载2013时花了多长时间,只说安装2015时用了 ...
- JS导出Excel 代码笔记
var tableToExcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,', template = '< ...
- ModernUI教程:使用WPF4.0
Modern UI 同时支持WPF4.0和4.5.下载包中包含了这两个版本的程序集.当你使用Nuget下载时,会根据你选择的运行时版本自动选择对应的版本下载.而Visual Studio2012的模板 ...
- js中字符串和数组相互转化的方法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #e4af0a } p. ...