前言:

  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的更多相关文章

  1. java调用本地方法的时候报错 could not find the main class:xx.program will exit

    如图所示,当在java调用本地方法的时候报错 我的解决办法是把dll文件放到System.out.println(System.getProperty("java.library.path& ...

  2. Android使用JNI(从java调用本地函数)

    当编写一个混合有本地C代码和Java的应用程序时,需要使用Java本地接口(JNI)作为连接桥梁.JNI作为一个软件层和API,允许使用本地代码调用Java对象的方法,同时也允许在Java方法中调用本 ...

  3. [转载]java调用本地dos命令

    在社区看到java调用本地dos命令的代码,特贴出来 String command = "ipconfig"; Runtime run = Runtime.getRuntime() ...

  4. Java调用本地方法又是怎么一回事

    JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ...

  5. java获取本地计算机MAC地址

    java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...

  6. 纯Java获得本地MAC地址

    import java.net.*; public class Ipconfig{      public static void main(String[] arguments) throws Ex ...

  7. Java获取本地IP地址

    import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...

  8. Java读取本地文件,并显示在JSP文件中

        当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...

  9. 使用JAVA打开本地应用程序相关的文件

    在该项目中需要运行本地文件或应用程序,JDK6添加后Desktop类别.可以直接使用.这使得有可能在程序中无论什么应用程序可以打开的.例:打开pdf文件,当地福昕是默认打开.执行程序将使用福昕开放pd ...

随机推荐

  1. 开启flask调试

    直接将app.debug = True时,程序出错并没有出现调试界面. 按照如下设置,flask+uwsgi情况下,python报错时会在浏览器中提示错误信息.方便调试. from werkzeug. ...

  2. http中关于缓存的那些header信息

    以前看过这方面的信息,这次用nginx做静态服务器又遇到了这个问题,所以在此总结下

  3. WIN7下强制分第四个主分区的方法

    通过磁盘管理的界面方式, 第四个分区会被分成扩展分区, 建议通过命令行 打开命令行运行diskpart, list disk 会列出所有磁盘, 选择要操作的磁盘序号如1,select disk 1 如 ...

  4. java多线程系类:基础篇:06线程让步

    本系类的知识点全部来源于http://www.cnblogs.com/skywang12345/p/3479243.html,我只是复制粘贴一下,特在此说明. 概要 本章,会对Thread中的线程让步 ...

  5. android:ToolBar详解(手把手教程)(转)

    来源 http://blog.mosil.biz/2014/10/android-toolbar/ 编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅 ...

  6. 前后端分离工具之ftl-server

    文章来源:https://www.npmjs.com/package/ftl-server 源代码可参考:https://github.com/szmtcjm/ftl-server/blob/mast ...

  7. Android酷炫实用的开源框架(UI框架)

    Android酷炫实用的开源框架(UI框架) 前言 忙碌的工作终于可以停息一段时间了,最近突然有一个想法,就是自己写一个app,所以找了一些合适开源控件,这样更加省时,再此分享给大家,希望能对大家有帮 ...

  8. 异常检测算法--Isolation Forest

    南大周志华老师在2010年提出一个异常检测算法Isolation Forest,在工业界很实用,算法效果好,时间效率高,能有效处理高维数据和海量数据,这里对这个算法进行简要总结. iTree 提到森林 ...

  9. EMV内核使用中的常见问题

    EMV内核在使用上会由于调用不当引起的许多问题,本文旨在基于内核LOG(也就是与IC卡交互的指令LOG)的基础上,对一些常见问题作初步的分析与解答,方便不熟悉EMV规范的同学参考. 本文的前提是你已经 ...

  10. 如何优化 FineUI 控件库的性能,减少 80% 的数据上传量!

    在开始正文之前,请帮忙为当前排名前 10 唯一的 .Net 开源软件 FineUI 投一票: 投票地址: https://code.csdn.net/2013OSSurvey/gitop/codevo ...