/* 本地文件 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. 通用shellcode

    所有 win_32 程序都会加载 ntdll.dll 和 kernel32.dll 这两个最基础的动态链接库.如果想要 在 win_32 平台下定位 kernel32.dll 中的 API 地址,可以 ...

  2. Python中 list, numpy.array, torch.Tensor 格式相互转化

    1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...

  3. Hyperledger Fabric网络节点架构

    Fabric区块链网络的组成  区块链网络结构图 区块链网络组成 组成区块链网络相关的节点 节点是区块链的通信主体,和区块链网络相关的节点有多种类型:客户端(应用).Peer节点.排序服务(Orde ...

  4. django反向解析URL和URL命名空间

    django反向解析URL和URL命名空间 首先明确几个概念: 1.在html页面上的内容特别是向用户展示的url地址,比如常见的超链接,图片链接等,最好能动态生成,而不要固定. 2.一个django ...

  5. Javascript中Base64编码解码的使用实例

    Javascript为我们提供了一个简单的方法来实现字符串的Base64编码和解码,分别是window.btoa()函数和window.atob()函数. 1 var encodedStr = win ...

  6. SDN交换机迁移2

    关于迁移过程中迁移目标(被迁移的交换机和目标控制器)的选择 SDN中基于过程优化的交换机竞争迁移算法 通信学报 交换机:请求速率大于域内平均请求速率的交换机集合: 控制器:综合网络中时延.流量和控制器 ...

  7. Day Ten

    站立式会议 站立式会议内容总结 331 今天:话题单选对话框 遇到问题:无 442 今天:数据库交互,解决timepicker问题 遇到的问题:无 439 今天:测试模块功能 遇到问题:无 会议照片 ...

  8. Win 2008 r2 远程桌面多用户登陆,一用户多登陆配置

    Windows 2008 R2远程桌面,设置最大连接数,一个登录后另一个就被踢掉等问题 Windows 2008 R2配置如图: 1.打开远程桌面回话主机配置 2.右键RDP-Tcp,属性,可设置最大 ...

  9. ElasticSearch 2 (23) - 语言处理系列之词根提取

    ElasticSearch 2 (23) - 语言处理系列之词根提取 摘要 世界上大多数语言都是屈折变化的,意思是词语可以通过变形来表达不同的含义: 数(Number): fox, foxes 时态( ...

  10. final 评论 II

    第二次评论内容: 1.Nice!小组的约跑app: 项目内容足够丰富,在展示时也很好的体现了app的功能,可以满足所提出的需求.在展示的过程中表述所占比例较小,希望能够以更多的讲述过程完善用户理解的功 ...