I/O 流
输入流的几个常用方法:
1,复制一个文件;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class copyDeno { public static void copy(String src, String dest) {//src 原文件,dest 待复制的文件
//这儿需要加下判断src是否为文件和是否存在,
InputStream in = null;
OutputStream out = null;
byte[] b = new byte[1024];
int len;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dest);
while ((len=in.read(b) )!= -1) {
out.write(b,0,len);
} } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
public static void copyWithBuffer(String src, String dest) {//以上加入缓冲流
InputStream in = null;
OutputStream out = null;
byte[] b = new byte[20];
int len;
try {
in = new BufferedInputStream(new FileInputStream(src));
out =new BufferedOutputStream(new FileOutputStream(dest));
while ((len=in.read(b) )!= -1) {
out.write(b,0,len);
}
//out.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Release.free(in, out); }
}
public static void main(String[] args) throws IOException { copy("F:\\Word\\eclipseword\\a4\\file\\a.txt", "F:\\Word\\eclipseword\\a4\\copyfile\\a1.txt");
copyWithBuffer("F:\\Word\\eclipseword\\a4\\file\\b.txt", "F:\\Word\\eclipseword\\a4\\copyfile\\b1.txt"); }
}
封装一个关闭流(释放资源)方法:
package IO; import java.io.Closeable;
import java.io.IOException;
public class Release {
public static void free(Closeable...stream){
for(Closeable st:stream){
if(st != null){
try {
st.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
}
2,复制一个文件夹的所有内容到另外一个文件夹中
package IOfuxi;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import IO.Release;
/*复制多级文件夹
* A:封装数据源File
* B:封装目的地File
* C:判断该File是文件夹还是文件
* a:是文件夹
* 就在目的地目录下创建该文件夹
* 获取该File对象下的所有文件或者文件夹File对象
* 遍历得到每一个File对象
* 回到C
* b:是文件
* 就复制(字节流)
* */
public class CopyFolderduo {
public static void main(String[] args) throws IOException {
copyfolder("F:\\Word\\eclipseword\\a4\\file",
"D:\\Java视频\\day21\\day21");
}
private static void copyfolder(String src,String des) throws IOException{
File file=new File(src);
File newfile=new File(des);
//判断文件是否存在
if(!file.exists()||!newfile.exists()){
return;
}else{
fuzhi(file,newfile);
}
}
private static void fuzhi(File file, File newfile) throws IOException {
// 判断该File是文件夹还是文件
if(file.isDirectory()){
File newFloder=new File(newfile,file.getName());
//创建目标路径文件夹
newFloder.mkdir();
//// 获取该File对象下的所有文件或者文件夹File对象
File[] arrfile=file.listFiles();
for(File f:arrfile){
//递归调用
fuzhi(f,newFloder);
}
}else{
File newf=new File(newfile,file.getName());
copyfile(file,newf);
}
}
//复制文件的方法
private static void copyfile(File file, File newf) throws IOException {
BufferedInputStream in= new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(newf));
byte[] arr=new byte[1024];
int len;
while((len=in.read(arr))!=-1){
out.write(arr, 0, len);
out.flush();
}
// out.flush();
Release.free(in,out);
}
}
3.压缩一个文件夹
package zy823; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; public class zipFolder {
public static void zipFile(String src,String dest){
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));//
File fileSrc = new File(src);
if(!fileSrc.exists()){
throw new FileNotFoundException();
}else{
zip(fileSrc.getName(),zos,fileSrc);
zos.flush();
zos.close();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
private static void zip(String dest,ZipOutputStream zos,File src)throws Exception{
byte[] b = new byte[1024];
InputStream is = null;
if(src.isFile()){
zos.putNextEntry(new ZipEntry(dest));
is = new BufferedInputStream(new FileInputStream(src));
int len;
while((len = is.read(b)) != -1){
zos.write(b, 0, len);
}
zos.flush();
is.close();
zos.closeEntry();
}else{
File[] files = src.listFiles();
zos.putNextEntry(new ZipEntry(dest+"/"));
for(File temp:files){
zip(dest+"/"+temp.getName(),zos,temp);
}
}
} }
I/O 流的更多相关文章
- 使用C#处理基于比特流的数据
使用C#处理基于比特流的数据 0x00 起因 最近需要处理一些基于比特流的数据,计算机处理数据一般都是以byte(8bit)为单位的,使用BinaryReader读取的数据也是如此,即使读取bool型 ...
- HTML 事件(三) 事件流与事件委托
本篇主要介绍HTML DOM中的事件流和事件委托. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4 ...
- FILE文件流的中fopen、fread、fseek、fclose的使用
FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- java 字节流与字符流的区别
字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢?实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作 ...
- BZOJ 3504: [Cqoi2014]危桥 [最大流]
3504: [Cqoi2014]危桥 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1407 Solved: 703[Submit][Status] ...
- java I/O流
输入流(读取数据的流) BufferedInputStream---继承--->FileInputStream--继承--->InputStream------> (1)字节流操作中 ...
- Ford-Fulkerson 最大流算法
流网络(Flow Networks)指的是一个有向图 G = (V, E),其中每条边 (u, v) ∈ E 均有一非负容量 c(u, v) ≥ 0.如果 (u, v) ∉ E 则可以规定 c(u, ...
- .NET基础拾遗(3)字符串、集合和流
Index: (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基础 ...
- C#开源实现MJPEG流传输
本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 许久以前写了篇文章<基于.NET打造IP智能网络视频监控系统>,记录和介绍了自己几年来积 ...
随机推荐
- js 获取随机数 Math.random()
js 获取随机数 Math.random() // 结果为0-1间的一个随机数(包括0,不包括1) var randomNum1 = Math.random(); //console.log(rand ...
- C# Winform ListView控件
一.ListView: 1.视图改为为Detalis: 2.编辑列,每添加一个添加一列,右侧属性Text改列名,停靠位置,列头的长度等等: 3.右侧属性,点开Iteme,添加ListViewItem集 ...
- 洛谷P4316 绿豆蛙的归宿
一眼看去,这不是高斯消元吗? 然后发现数据范围是100000... 然后发现是DAG...直接拓扑序递推即可. 边(x, y,z)的贡献是P(x) * z / out[x] #include < ...
- 梯度提升树(GBDT)原理小结(转载)
在集成学习值Adaboost算法原理和代码小结(转载)中,我们对Boosting家族的Adaboost算法做了总结,本文就对Boosting家族中另一个重要的算法梯度提升树(Gradient Boos ...
- SCU-4527 NightMare2(Dijkstra+BFS) !!!错误题解!!!
错解警告!!! 描述 可怜的RunningPhoton又做噩梦了..但是这次跟上次不大一样,虽然他又被困在迷宫里,又被装上了一个定时炸弹,但是值得高兴的是,他发现他身边有数不清的财宝,所以他如果能带着 ...
- linux 进程创建clone、fork与vfork
目录: 1.clone.fork与vfork介绍 2.fork说明 3.vfork说明 4.clone说明5.fork,vfork,clone的区别 内容: 1.clone.fork与vfork介绍 ...
- bzoj2434 fail树 + dfs序 + 树状数组
https://www.lydsy.com/JudgeOnline/problem.php?id=2434 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...
- Tensorflow object detection API 搭建物体识别模型(四)
四.模型测试 1)下载文件 在已经阅读并且实践过前3篇文章的情况下,读者会有一些文件夹.因为每个读者的实际操作不同,则文件夹中的内容不同.为了保持本篇文章的独立性,制作了可以独立运行的文件夹目标检测. ...
- Hadoop记录-Hive调优:让任务并行执行
set mapred.job.queue.name=pms; //设置队列set hive.exec.reducers.max=8; //设置最大的reducersset mapred.redu ...
- 端口与进程-----Window cmd命令
******************** windows 篇 ********************** cmd命令: services.msc 打开本地服务页面 一.查看windows系统 ...