前言
java io 部分内容繁多,结合使用,决定选择FileInputStream & BufferedInputStream & Zip
原因:二进制使用的地方很多,zip可以作为一种扩展。有意思,了解一下。
说明:io部分,个人一直认为官方没有一个很简单明了的代码,每个人涉及到io部分的代码也都很繁杂,每次看到这个部分,都很头大,但是如果了解了原理,希望能有一个清晰的思路。
提前总结:建议使用bufferedInputStream & bufferedOutputStream,这种情况下,个人只与内存交互,与硬盘的交互由java来完成,避免了很多未知的麻烦。(自己的理解)
目录
1.FileInputStream & FileOutputStream
2.BufferedInputStream & BufferedOutputStream
3.ZipInputStream & ZipOutputStream
一.copy操作-----FileInputStream & FileOutputStream
提前知识铺垫:InputStream&OutputStream 是abstract类,不能实例化。FileInputStream & FileOutPutStream是它可实例化的子类。
示例图(个人理解)
无标题.png
步骤:
1.声明变量,并new对象(打开流)
2.一边读一边写。
3.flush一下,防止read完了,但是里面还有内容write写完。
4.关闭流
无标题.png
public static boolean copy(String fromPath, String toPath){
long start = System.currentTimeMillis();
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(fromPath);
out = new FileOutputStream(toPath);
byte[] bytes = new byte[1024];
int a;
while ((a = in.read(bytes)) > 0){
out.write(bytes, 0, a);
}
out.flush();
System.out.println("没有buffer用时=" + (System.currentTimeMillis() - start));
return true;
} catch (FileNotFoundException e) {
System.out.println("error:文件不存在");
return false;
}catch (IOException e){
System.out.println("error:io出错" + e.getMessage());
return false;
}finally {
try{
if (in != null) {
in.close();
}
if (out != null){
out.close();
}
}catch (IOException e){
System.out.println();
}
}
}
二.copy操作-----BufferedInputStream & BufferedOutputStream
提前知识铺垫:BufferedInputStream & BufferedOutputStream是辅助类,作用是更快的读取磁盘内容,原理是提前读入到内存中,再从内存中读取,但是它的限制是8Mb,所以如果文件过大,或者过小,如果过于大(测试4Gb文件),甚至会出现更慢的情况。
示例图(个人理解)
无标题1.png
步骤:
1.声明input&output变量,并new对象(打开流)
2.声明bufferedInput&bufferedOutput对象,把硬盘数据读入内存中。
3.一边读一边写。
4.flush一下,防止read完了,但是里面还有内容write写完。
5.关闭流
无标题.png
public static boolean copy1(String fromPath, String toPath){
long start = System.currentTimeMillis();
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(fromPath));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(toPath));
byte[] bytes = new byte[1024];
int a;
while ((a = bufferedInputStream.read(bytes)) > 0){
bufferedOutputStream.write(bytes, 0, a);
}
bufferedOutputStream.flush();
System.out.println("使用buffer用时=" + (System.currentTimeMillis() - start));
return true;
} catch (FileNotFoundException e) {
System.out.println("error:文件不存在");
return false;
}catch (IOException e){
System.out.println("error:io出错");
return false;
}finally {
try {
if (bufferedInputStream != null)
bufferedInputStream.close();
if (bufferedOutputStream != null)
bufferedOutputStream.close();
}catch (IOException e){ }
}
}
三:ZipInputStream & ZipOutputStream
上面是简单演示,下面自己写的压缩&解压zip文件及文件夹演示
压缩
无标题.png
解压缩
无标题.png
实例(压缩与解压)
压缩思路:递归添加文件夹中所有文件到压缩文件中。
解压思路:遍历zipFile的所有ZipEntry,如果是文件,就读取文件(如果文件的文件夹还没有创建,就创建文件夹),如果是文件夹,并且文件夹还没有创建,创建文件夹。
public class Zip {
public static void main(String[] args){
inZip("C:\\Users\\rtyui\\Desktop\\info", "C:/Users/rtyui/Desktop/info.zip");
outZip("C:\\Users\\rtyui\\Desktop\\info.zip", "C:/Users/rtyui/Desktop/ceshiceshi");
}
/**
* 压缩
* @param fromPath
* @param toDir
* @return
*/
public static boolean inZip(String fromPath, String toDir){
ZipOutputStream zipOutputStream = null;
File fileFrom = new File(fromPath);
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(toDir));
dealInZip(fileFrom, zipOutputStream, fromPath.substring(0, fromPath.lastIndexOf('\\') + 1));
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 解压缩
* @param fromPath
* @param toDir
* @return
*/
public static boolean outZip(String fromPath, String toDir){
ZipInputStream zipInputStream = null;
ZipFile zipFile = null;
File file = new File(toDir);
if (!file.exists()){
file.mkdirs();
}
try {
zipInputStream = new ZipInputStream(new FileInputStream(fromPath));
zipFile = new ZipFile(fromPath);
ZipEntry zipEntry = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null){
if (zipEntry.isDirectory()){
File filetemp = new File(toDir + zipEntry.getName());
if (!filetemp.exists()){
filetemp.mkdirs();
}
}else{
File filetemp = new File(new File(toDir + zipEntry.getName()).getParent());
if (!filetemp.exists()){
filetemp.mkdirs();
}
OutputStream outputStream = new FileOutputStream(toDir + zipEntry.getName());
InputStream inputStream = zipFile.getInputStream(zipEntry);
byte[] bytes = new byte[1024];
int a;
while ((a = inputStream.read(bytes)) > 0){
outputStream.write(bytes, 0, a);
}
outputStream.close();
inputStream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 递归处理压缩时候,文件夹层叠的情况,
* @param fileFrom
* @param zipOutputStream
* @param head
*/
public static void dealInZip(File fileFrom, ZipOutputStream zipOutputStream, String head){
try {
if (fileFrom.isDirectory()){
ZipEntry entry = new ZipEntry(fileFrom.getAbsolutePath().replace(head, "") + "/");
zipOutputStream.putNextEntry(entry);
File[] fileDirs = fileFrom.listFiles();
for (File file : fileDirs)
{
dealInZip(file, zipOutputStream, head);
}
}else{
InputStream inputStream = new FileInputStream(fileFrom.getAbsolutePath());
ZipEntry entry = new ZipEntry(fileFrom.getAbsolutePath().replace(head, ""));
zipOutputStream.putNextEntry(entry);
byte[] bytes = new byte[1024];
int a;
while ((a = inputStream.read(bytes)) > 0){
zipOutputStream.write(bytes, 0, a);
}
inputStream.close();
}
}catch (Exception e){
}
}
}
网友评论