/* 本地文件 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. http to https

    https://www.cnblogs.com/powertoolsteam/p/http2https.html

  2. Asp.Net_获取IP地址

    //方法一 HttpContext.Current.Request.UserHostAddress; //方法二 HttpContext.Current.Request.ServerVariables ...

  3. Bitcoin挖矿

    目录 为什么要挖矿? 比特币挖矿 为什么要挖矿? 增加恶意行为的成本 增加记账权力,获取相应的奖励 比特币挖矿 每开采210000个区块,挖矿奖励减半 2009年1月-2012年11月,奖励50BTC ...

  4. Git多人协作工作流程

    前言 之前一直把Git当做个人版本控制的工具使用,现在由于工作需要,需要多人协作维护文档,所以去简单了解了下Git多人协作的工作流程,发现还真的很多讲解的,而且大神也已经讲解得很清楚了,这里就做一个简 ...

  5. 词频统计 SPEC 20160911

    本文档随时可能修改,并且没有另行通知. 请确保每一次在开始修改你的代码前,读标题中的日期,如果晚于你上次阅读, 请重读一次. 老五在寝室吹牛他熟读过<鲁滨逊漂流记>,在女生面前吹牛热爱&l ...

  6. Daily Scrumming* 2015.12.12(Day 4)

    一.团队scrum meeting照片 二.今日总结 姓名 WorkItem ID 工作内容 签入链接以及备注说明  江昊 任务1036 进行界面开发,明日准备开发第一个界面,社团展示界面 工作暂未完 ...

  7. 《Linux内核分析》第二周学习报告

    <Linux内核分析>第二周学习报告 ——操作系统是如何工作的 姓名:王玮怡  学号:20135116 第一节 函数调用堆栈 一.三个法宝 二.深入理解函数调用堆栈 三.参数传递与局部变量 ...

  8. c++中队列queue和栈stack的基本操作

    1.queue 模板类的定义在<queue>头文件中. 定义queue 对象的示例代码如下:queue<int> q1;queue<double> q2; queu ...

  9. php排序学习之-冒泡排序

    原理:对一组数据,比较相邻数据的大小,将值小数据在前面,值大的数据放在后面.   (以下都是升序排列,即从小到大排列) 举例说明: $arr = array(6, 3, 8, 2, 9, 1); $a ...

  10. 【BZOJ3733】[Pa2013]Iloczyn (搜索)

    [BZOJ3733][Pa2013]Iloczyn (搜索) 题面 BZOJ 题解 把约数筛出来之后,直接爆搜,再随便剪枝就过了. 最近一句话题解倾向比较严重 #include<iostream ...