IO流技术:文件夹的创建、遍历、统计 | 编码解码实验
API | 说明 |
---|---|
mkdir() 、 mkdirs() | 创建目录,如果父目录链不存在一同创建 |
list() | 下级名称 |
listFiles() | 下级File |
listRoots() | 根路径 |
一、创建目录
- mkdir() 确保上级目录存在
- mkdirs() 上级目录可以不存在
代码
public class DirDemo1 {
public static void main(String[] args) {
File dir = new File("E:/Java/IO_Study01/dir/test");
//创建目录
boolean flag = dir.mkdirs();
System.out.println(flag);
}
}
结果
由于我之前创建过,所以返回值为 false
二、列出下一级
- list() 列出下级名称
- listFiles() 列出下级File对象
- listRoots() 列出所有盘符
代码
public class DirDemo2 {
public static void main(String[] args) {
File dir = new File("E:/Java/IO_Study01");
//下级名称
String[] subNames = dir.list();
for(String s:subNames){
System.out.println(s);
}
//下级对象
File[] subFiles = dir.listFiles();
for(File s:subFiles){
System.out.println(s.getAbsolutePath());
}
//列出盘符
File[] roots = dir.listRoots();
for(File r:roots){
System.out.println(r.getAbsolutePath());
}
}
}
结果
三、使用递归
- 方法自己调用自己
- 打印子孙级目录和文件的名称
代码
public class DirDemo4 {
public static void main(String[] args) {
File src = new File("E:/Java/IO_Study01/src");
printName(src,0);
}
//打印子孙级目录和文件的名称
public static void printName(File src,int deep){
System.out.println("第"+deep+"层"+src.getName());
if(null==src||!src.exists())return;
else if(src.isDirectory()){ //判断是目录继续打印
for(File s:src.listFiles()) printName(s,deep+1);
}
}
}
结果
四、统计文件夹的大小
方法一
代码
public class DirDemo5 {
public static void main(String[] args) {
File src = new File("E:/Java/IO_Study01");
count(src);
System.out.println(len);
}
private static long len=0;
//统计文件夹的大小
public static void count(File src){
//获取大小
if(null!=src && src.exists()){
if(src.isFile()){
len+=src.length();
}else{ //子孙级
for(File s:src.listFiles()){
count(s);
}
}
}
}
}
结果
方法二
使用面向对象,统计文件夹的大小
代码
public class DirCount {
//大小
private static long len;
//文件夹路径
String path;
//文件个数
private static int fileSize;
//文件夹个数
private static int dirSize;
//源
private File src;
//构造器
public DirCount(String path){
this.path=path;
this.src=new File(path);
count(this.src);
}
//主方法
public static void main(String[] args) {
DirCount dir = new DirCount("E:/Java/IO_Study01");
System.out.println("大小(bytes):"+dir.getlen());
System.out.println("包含文件个数:"+dir.getFileSize());
System.out.println("包含文件夹个数(含自身):"+dir.getDirSize());
}
//统计大小
private static void count(File src){
//获取大小
if(null!=src && src.exists()){
if(src.isFile()){
len+=src.length();
fileSize++;
}else{ //子孙级
dirSize++;
for(File s:src.listFiles()){
count(s);
}
}
}
}
public int getFileSize() {
return fileSize;
}
public int getDirSize() {
return dirSize;
}
public long getlen(){
return len;
}
}
结果
五、统计文件夹的大小
- 字符集:Java字符使用16位的双字节存储。但是在实际文件存储的数据有各种字符集,需要正确操作,否则就有乱码的发生。
- 编码:encode,将字符转为字节
- 解码:decode,将字节转为字符
1、编码
代码
public class ContentEncode {
public static void main(String[] args) throws Exception {
String msg = "周百青a";
//编码:转为字节数组
//默认GBK:一个汉字占两个字节,一个字母占一个字节
byte[] datas = msg.getBytes();
System.out.println(datas.length);
//编码:转为其他字符集
//UTF-16LE:一个汉字占两个字节,一个字母占两个字节
datas = msg.getBytes("UTF-16LE");
System.out.println(datas.length);
//UTF-8:一个汉字占三个字节,一个字母占一个字节
datas = msg.getBytes("UTF-8");
System.out.println(datas.length);
}
}
结果
2、解码
代码
public class ContentDecode {
public static void main(String[] args) throws Exception {
String msg = "周百青a";
//编码:转为字节数组
byte[] datas = msg.getBytes();
//解码:转成字符串
//String(byte[] bytes, int offset,
//int length, String charsetName)
msg = new String(datas,0,datas.length,"gbk");
System.out.println(msg);
//乱码:
//1、字节数不够
msg = new String(datas,0,datas.length-2,"gbk");
System.out.println(msg);
//2、字符集不统一
msg = new String(datas,0,datas.length,"utf8");
System.out.println(msg);
}
}