Java文件夹统计案例
递归打印子孙级目录和文件的名称:
public static void main(final String[] args) throws IOException { File src = new File("E:/java"); printName(src,0); //递归打印子孙级目录和文件的名称 } public static void printName(File src,int deep) { //控制前面-,增加层次感 for(int i = 0;i<deep;i++) { System.out.print("-"); } //打印名称 System.out.println(src.getName()); if(src == null||!src.exists()) { //递归头 return; }else if(src.isDirectory()){ //目录 for(File s:src.listFiles()) { printName(s, deep+1); //递归体 } } }
统计文件夹大小、文件个数、文件夹个数:
public static void main(final String[] args) throws IOException { File src = new File("E:/java"); cout(src); System.out.println("文件字节大小:"+len); System.out.println("文件个数:"+file); System.out.println("文件夹个数:"+directory); } private static long len = 0; private static long file = 0; private static long directory = -1; //算文件夹个数会包含自身 public static void cout(File src) { //获取大小 if(src.exists()||src!=null) { if(src.isFile()) { //大小 len+=src.length(); //所有文件大小进行累加 file++; //文件个数 }else { //子孙级 for(File s:src.listFiles()) { cout(s); } directory++; //文件夹个数 } } }
文件字节流
文件字节输入流:
/*文件字节输入流*/ //1、创建源 File src =new File("C:\\Users\\lenovo\\Desktop\\新建文本文档.txt"); //2、选择流 InputStream is = new FileInputStream(src); //3、操作(分段读取) byte[] flush = new byte[1024]; //缓冲容器(1024个字节=1k,1k1k的读) int len; //接收长度 while((len=is.read(flush))!=-1) { //字节数组-->字符串(解码) String str = new String(flush,0,len,"UTF-8"); System.out.println(str); } //4、释放资源 is.close();
文件输出流:
/*文件字节输出流*/ //1、创建源(系统会给你自动创建文件) /*File dest = new File("C:\\Users\\lenovo\\Desktop\\新建ws.txt"); //2、选择流 OutputStream os = new FileOutputStream(dest,true); //加上true为追加,加上false为覆盖(和不加效果一样) //3、操作(写出) String msg = "IO is so 4242I am ws\n"; byte[] datas = msg.getBytes(); //字符串-->字节数组(编码) os.write(datas, 0, datas.length); //4、释放资源 os.flush(); //清除 os.close();*/
文件拷贝(注:输入流、输出流需分别关闭,先打开的后关闭):
//文件拷贝:文件字节输入流、输出流 //1、创建源 File src1 =new File("E:\\党课\\计算机与信息表技术学院第二十期党课初级班安排课程表.xlsx"); //源头 File src2 =new File("E:\\党课\\王烁.xlsx"); //目的地 //2、选择流 InputStream is = new FileInputStream(src1); OutputStream os = new FileOutputStream(src2); //3、操作(分段读取) byte[] flush = new byte[1024]; //缓冲容器(1024个字节=1k,1k1k的读) int len; //接收长度 while((len=is.read(flush))!=-1) { os.write(flush,0,len); } os.flush(); //4、释放资源。分别关闭,先打开的后关闭 os.close(); is.close();
文件字符流
文件拷贝:
//文件拷贝:文件字符输入流、输出流 //1、创建源 File src1 =new File("D:\\杞欢\\王烁.txt"); //源头 File src2 =new File("D:\\杞欢\\100.txt"); //目的地 //2、选择流 Reader reader = new FileReader(src1); Writer writer = new FileWriter(src2); //3、操作(分段读取) char[] flush = new char[1024]; //缓冲容器 int len; //接收长度 while((len=reader.read(flush))!=-1) { writer.write(flush,0,len); } writer.flush(); //4、释放资源。分别关闭,先打开的后关闭 writer.close(); reader.close();
字节数组流
字节数组输入流:
//字节数组输入流 //1、创建源:字节数组,不要太大 byte[] src = "hello ws".getBytes(); //2、选择流 InputStream is = new ByteArrayInputStream(src); //3、操作(分段读取) byte[] flush = new byte[3]; //缓冲容器 int len; //接收长度 while((len=is.read(flush))!=-1) { //字节数组-->字符串(解码) String str = new String(flush,0,len); System.out.println(str); } //4、释放资源(可以不用处理)
字节数组输出流:
//字节数组输出流(获取数据:toByteArray()) //1、创建源:内部维护 byte[] dest = null; //2、选择流:不关联源 ByteArrayOutputStream baos = new ByteArrayOutputStream(); //3、操作(写出内容) String msg = "show me singing!!"; byte[] datas = msg.getBytes(); //字符串-->字节数组(编码) baos.write(datas,0,datas.length); baos.flush(); //获取数据 dest = baos.toByteArray(); System.out.println(dest.length+"-->"+new String(dest,0,baos.size())); //4、释放资源(可以不用处理)
IO综合-对接流:
public static void main(final String[] args) throws IOException {
//图片转为字节数组
byte[] datas = fileToByteArray("E:\\c语言\\`@7ZP)Q8`MH2F6ER~6]MNSM.png");
System.out.println(datas.length);
byteArrayToFile(datas, "E:\\c语言\\`MNSM.png");
}
//1、图片读取到字节数组
/**
* 1)、图片到程序 FileInputStream
* 2)、程序到字节数组 ByteArrayOutputStream
* @throws IOException
*/
public static byte[] fileToByteArray(String filePath) throws IOException {
//1、创建源
File src = new File(filePath);
//2、选择流
InputStream is = new FileInputStream(src);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//3、操作(分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len; //接收长度
while((len=is.read(flush))!=-1){
baos.write(flush,0,len); //写出到字节数组当中
}
baos.flush();
is.close();
return baos.toByteArray();
}
//1、字节数组到文件
/**
* 1)、字节数组到程序 ByteArrayInputStream
* 2)、程序到文件 FileOutputStream
* @throws IOException
*
*/
public static void byteArrayToFile(byte[] src,String filePath) throws IOException {
//1、创建源
File dest =new File(filePath);
//2、选择流
ByteArrayInputStream is =new ByteArrayInputStream(src);
OutputStream os =new FileOutputStream(dest);
//3、操作(分段读取)
byte[] flush = new byte[5]; //缓冲容器
int len; //接收长度
while((len=is.read(flush))!=-1) {
os.write(flush,0,len);
}
//4、释放资源
os.close();
}
************************************************************************************************************