JAVA 二十六 IO流,字节流
标签: JAVA
字节流文件操作
import java.io.File;
import java.io.IOException;
public class FileDemo {
/*
* createNewFile 在指定目录下创建一个文件,如果该文件已经存在,创建失败
* mkdir 创建一个文件夹
* mkdirs 创建多级文件夹
* 2 判断
* isFile 判断文件是否存在
* isDirectory 判断文件夹是否存在
* 3 获取
* getName 获取文件的名字
* getAbsolutePath 获取文件的绝对路径
* getPath 获取文件的路径
* getParent 获取文件的上级目录
* length 获取文件的长度
* listFiles 获取指定文件夹下所有的文件列表
* 4 删除
* delete 删除对应的文件
* deleteOnExit 虚拟机退出的时候删除对应的文件
* */
public static void main(String[] args){
File file1 = new File("D:\\input\\a\\b\\c\\d");
File file2 = new File("D:\\output");
File file = new File("D:\\input");
try {
System.out.println(file2.createNewFile());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(file1.mkdirs());
System.out.println(file2.isFile());
System.out.println(file2.isDirectory());
System.out.println(file2.getName());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getPath());
System.out.println(file2.getParent());
System.out.println(file2.length());
File [] list=file.listFiles();
for(File f:list){
System.out.println(f.getAbsolutePath());
}
}
}
复制一个文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopZip {
public static void main(String[ ]args){
FileInputStream fi = null;
FileOutputStream fo = null;
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
try {
fi =new FileInputStream("D:\\input\\5EClient-2.1.9.zip");
fo =new FileOutputStream("D:\\input\\5EClient-2.1.9(2).zip");
bi = new BufferedInputStream(fi);
bo = new BufferedOutputStream(fo);
byte b [ ] = new byte[100];
int len = 0;
//这是带有缓冲区的拷贝 速度更快 效率更高
try {
while((len = bi.read(b))!=-1){
fo.write(b, 0, len);
bo.write(b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fi!=null){
try {
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bi!=null){
try {
bi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bo!=null){
try {
bo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
拷贝一个文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyPic {
/*
* 基类:
*/
public static void main(String[ ]args){
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream("D:\\input\\Copy.zip");
fo = new FileOutputStream("D:\\input\\Copy(2).zip");
// int len;
//这是一个字节一个字节拷贝,速度非常慢
// try {
// while((len=fi.read())!=-1){
// fo.write(len);
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
byte b [ ] = new byte[1024];
int len = 0 ;
//这是1024个字节一次性拿出传入数组,一次性拷贝,速度比之前一个一个字节拷贝要快
try {
while((len = fi.read(b)) != -1){
fo.write(b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fi!=null){
try {
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fo!=null){
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}