/* 本地文件 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. 在CentOS上搭建PHP服务器环境(可用)

    原文:https://www.cnblogs.com/zy2009/p/7047828.html 1,先安装apache: yum install httpd 配置ServerName vi /etc ...

  2. Scrum Meeting NO.1

    Scrum Meeting No.1 1.会议内容 不出所料地,组员们都在忙着写编译.编译大作业的进度已经接近尾声,码农们已经磨刀霍霍向软工-- 在上一周,bugphobia和我们组决定共同使用一套后 ...

  3. BFS和DFS算法

    昨晚刚昨晚华为笔试题,用到了BFS和DFS,可惜自己学艺不精,忘记了实现原理,现在借用大佬写的内容给自己做个提高 转自:https://www.jianshu.com/p/70952b51f0c8 图 ...

  4. Thymeleaf 学习笔记

    (一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引 ...

  5. word count程序,以及困扰人的宽字符与字符

    一个Word Count程序,由c++完成,有行数.词数.能完成路径下文件的遍历. 遍历文件部分的代码如下: void FindeFile(wchar_t *pFilePath) { CFileFin ...

  6. VS2013安装及测试练习

    一.安装过程 任务:安装VS2010以上的版本. 其实很闹心,因为看了一下VS的安装包,都很大.以学校的网速,得下到什么时候?这是第一想法. 挺麻烦,也挺周折,最终下好了安装包.但是,还是出了问题,在 ...

  7. 使用Samba服务程序,让linux系统之间共享文件

    yum  install -y   cifs-utils mkdir  /database    创建挂载目录 在root家目录创建认证文件(依次为SMB用户名.SMB用户密码.SMB共享域)   v ...

  8. PAT 甲级 1096 Consecutive Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...

  9. 关于查询报表总是"超时已过期"的问题解决

    "超时已过期" 的问题一直在烦扰着我, 在查一些数据量比较大的表或者运行一些复杂存储过程的时候就会出现这个提示, 一开始是按下面的来设,有一些报表是可以正常查出来 a.在企业管理器 ...

  10. 一条sql语句搞定基于mysql的sql执行顺序的基本理解

    对数据库基本操作是每个程序员基本功,如何理解并快速记住sql执行的顺序呢,其实一条复杂的sql就能搞定: SELECT DISTINCT <select_list> FROM <le ...