/* 本地文件 URL 文件拷贝 */
/*文本文件拷贝 可以通过 字符流,也可以通过字节流*/
/*二进制文件拷贝 只可以通过字节流*/
/* 希望这个例子能帮助搞懂 字符流与字节流的区别 */
import java.io.*; //in order to utilize stream object
import java.util.*; // in order to utilize ArrayList
import java.net.*;
class Copy{
public static void main(String [] args){
boolean flag=false;
String src="C:\\Users\\Ghc\\Desktop\\extractzip.bat";
String dest="C:\\Users\\Ghc\\Desktop\\Test\\extractzip_copy.bat";
System.out.println("copy textDoc: from "+src+" to "+dest+(copyTextDoc(src,dest) ? "Successfully!":"Failed")); src="C:\\Users\\Ghc\\Desktop\\psb.jpg";
dest="C:\\Users\\Ghc\\Desktop\\Test\\psb.jpg";
System.out.println("copy textDoc: from "+src+" to "+dest+(copyBinFile(src,dest) ? "Successfully!":"Failed")); String url="http://qiniuuwmp3.changba.com/852316795.mp3";
String destPath="C:\\Users\\Ghc\\Desktop\\Test\\music.mp3"; // downLoad mp3 from URL
downLoadMP3FromURL(url,destPath);
} public static void downLoadMP3FromURL(String url,String destPath){
InputStream urlInpts=null;
BufferedInputStream bufinpts=null;
BufferedOutputStream bufoutpts=null;
try{
urlInpts=new URL(url).openStream();
bufinpts=new BufferedInputStream(urlInpts);
bufoutpts=new BufferedOutputStream(new FileOutputStream(destPath)); byte[] musicBuf=new byte[8192];
int len=-1;
while((len=bufinpts.read(musicBuf))!=-1){
bufoutpts.write(musicBuf,0,len);
bufoutpts.flush();
}
}
catch(MalformedURLException mfurle){
mfurle.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
} public static boolean copyTextDoc(String src,String dest){
boolean flag=true; BufferedReader bufr=null; BufferedWriter bufw=null;
ArrayList<String> lineList=null; try{
//read text file //bufr=new BufferedReader(new InputStreamReader(new FileInputStream(src)));
//等价于
bufr=new BufferedReader(new FileReader(src)); //bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)));
//等价于
bufw=new BufferedWriter(new FileWriter(dest));
lineList=new ArrayList<String>(); String line=null; while((line=bufr.readLine())!=null){
lineList.add(line);
bufw.write(line,0,line.length());
bufw.newLine();
bufw.flush(); // must to flush!!! attention!!!
} Iterator<String> it=lineList.iterator();
while(it.hasNext()){
line=it.next();
System.out.println(line+" from iterator");
}
}
catch(IOException ioe){
ioe.printStackTrace();
flag=false; //write text file }
finally{
if(bufr!=null)
try{
bufr.close();
bufr=null;
}
catch(IOException ioe){
ioe.printStackTrace();
}
if(bufw!=null)
try{
bufw.close();
bufw=null;
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
return flag;
}
public static boolean copyBinFile(String src,String dest){
boolean flag=true;
BufferedInputStream bufinpts=null; BufferedOutputStream bufoutpts=null;
byte [] buf=new byte[1024];
int len=-1;
try{
bufinpts=new BufferedInputStream(new FileInputStream(src));
bufoutpts=new BufferedOutputStream(new FileOutputStream(dest)); while((len=bufinpts.read(buf))!=-1){
bufoutpts.write(buf,0,len); // not need to flush, but the last buf may not been copied
// print binary data see see , it's funny!!!
/* for(int i=0;i<len;i++){
System.out.print(buf[i]);
} */
bufoutpts.flush();
}
}
catch(IOException ioe){
ioe.printStackTrace();
flag=false;
}
finally{
if(bufinpts!=null)
try{
bufinpts.close();
bufinpts=null;
}
catch(IOException ioe){
ioe.printStackTrace();
}
if(bufoutpts!=null)
try{
bufoutpts.close();
bufoutpts=null;
}
catch(IOException ioe){
ioe.printStackTrace();
}
} return flag;
}
}

  

IO 复习字节流字符流拷贝文件的更多相关文章

  1. IO—》字节流&字符流

    字节流 一.字节输出流OutputStream OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法. FileOutputStre ...

  2. Java之字符流读写文件、文件的拷贝

    字符流读数据 – 按单个字符读取 创建字符流读文件对象: Reader reader = new FileReader("readme.txt"); 调用方法读取数据: int d ...

  3. 第31天学习打卡(File类。字符流读写文件)

    File类 概念 文件,文件夹,一个file对象代表磁盘上的某个文件或者文件夹 构造方法  File(String pathname) File(String parent,String child) ...

  4. -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流

      File类 •文件和目录路径名的抽象表示形式 构造方法 •public File(String pathname) •public File(String parent,Stringchild) ...

  5. java io流(字符流) 文件打开、读取文件、关闭文件

    java io流(字符流) 文件打开 读取文件 关闭文件 //打开文件 //读取文件内容 //关闭文件 import java.io.*; public class Index{ public sta ...

  6. Java基础小知识1——分别使用字节流和字符流复制文件

    在日常使用计算机过程中经常会涉及文件的复制,今天我们就从Java代码的角度,看看在Java程序中文件复制的过程是如何实现的. 1.使用字节流缓冲区复制文件 示例代码如下: import java.io ...

  7. IO流(字节流,字符流,缓冲流)

    一:IO流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流   这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图:   二:字符字节 ...

  8. Java中字节流和字符流复制文件

    字节流和字符流复制文件的过程: 1.建立两个流对象 绑定数据源和目的地 2.遍历出需复制的文件写入复制过后的新文件中(只不过是遍历的时候是区分字节和字符的) 3.访问结束后关闭资源 字节流复制文件: ...

  9. IO字 节流/字符流 读取/写入文件

    流是指一连串流动的数据信号,以先进,先出的方式发送和接收的通道 流的分类根据方向分为输入流所有接收,获得,读取的操作都是属于输入流所有的输入流名字都带有input或Reader 输出流所有发送,写的操 ...

随机推荐

  1. Css_button样式对不齐

    发现了是按钮的vertical-align式样,统一显示的设置为middle,搞定.

  2. ctf入门常见类别

    原视频在这里:实验吧-名师指导http://www.shiyanbar.com/course-video/watch-video/cid/419/vid/2000网络安全从业者尝试介绍 web应用渗透 ...

  3. FTP地址

    访问不了FTP的同学可以试试用IPv6 地址2001:da8:203:ed5:CEB2:55FF:FE8B:ED1来访问,用户名密码不变.

  4. 每日scrum(4)

    今天是冲刺第4天,大家都忙着找大二的学弟学妹来点评来支持我们的软件. 遇到的问题主要是如何劝说学弟学妹选择我们的软件然后继续往下做. 任务看板: 燃尽图:

  5. 电梯V1.0

    电梯V1.0 GitHub仓库地址 Problem 一栋3层的大楼(楼层编号0-2),设有一台无限载重的电梯,初始时电梯停在0层.电梯移动1层的耗时为1,在某一层停靠的耗时为1(时间初始为0).电梯不 ...

  6. PowerDesigner16工具学习笔记-建立CDM

    1.基本术语 1.1.实体和属性 实体(entity):指现实世界中客观存在,并可相互区别的事物或者事件. 属性(attribute):一组用来描述实体特征的属性. 实体集(entity set):具 ...

  7. 软工结对项目之词频统计update

    队友 胡展瑞 031602215 作业页面 GitHub 具体分工 111500206 赵畅:负责WordCount的升级,添加新的命令行参数支持(自定义输入输出文件,权重词频统计,词组统计等所有新功 ...

  8. git如何删除已经 add 的文件 (如何撤销已放入缓存区文件的修改)

    使用 git rm 命令即可,有两种选择, 一种是 git rm –cached “文件路径”,不删除物理文件,仅将该文件从缓存中删除: 一种是 git rm –f “文件路径”,不仅将该文件从缓存中 ...

  9. Linux命令(九)比较文件差异 diff

    diff 命令介绍 diff 命令的功能为逐行比较两个文本文件,列出其不同之处.对给出的文件进行系统的检查,并显示出两个文件中所有不同的行.如果 diff 命令后跟的是目录,则会对该目录中的同名文件进 ...

  10. Windows 设置开机自动登录

    1. 自己一些windows的虚拟机 有时候开机之后 输入用户名密码时间特别长. 需要等待很久, 如果能够设置开机自动登录的话 能够节约很多时间. 2. 最简单的办法  运行输入 control us ...