【IO流操作之】File使用详解

  1. 使用面向对象方式统计文件夹大小
package com.yf.cn.app.io;

import java.io.File;

/**
 *  使用面向对象: 统计文件夹的大小
 * @author 裴新
 *
 */
public class DirCount {
    
    
	//大小
	private long len;
	//文件夹路径
	private String path;
	//文件的个数
	private int fileSize;
	//文件夹的个数
	private int dirSize;
	//源
	private File src;
	public DirCount(String path) {
    
    
		this.path = path;
		this.src = new File(path);
		count(this.src);
	}	
	
	//统计大小
	private  void count(File src) {
    
    	
		//获取大小
		if(null!=src && src.exists()) {
    
    
			if(src.isFile()) {
    
      //大小
				len+=src.length();
				this.fileSize++;
			}else {
    
     //子孙级
				this.dirSize++;
				for(File s:src.listFiles()) {
    
    
						count(s);
				}
			}
		}
	}	
	
	public long getLen() {
    
    
		return len;
	}

	public int getFileSize() {
    
    
		return fileSize;
	}

	public int getDirSize() {
    
    
		return dirSize;
	}
	
	public static void main(String[] args) {
    
    
		DirCount dir = new DirCount("D:/spb11");
		System.out.println(dir.getLen()+"-->"+dir.getFileSize()+"-->"+dir.getDirSize());
		
		DirCount dir2 = new DirCount("H:/linux运维/01-第1阶段-运维基本功/day01-运维概述与Linux系统安装");
		System.out.println(dir2.getLen()+"-->"+dir2.getFileSize()+"-->"+dir2.getDirSize());

	}	


	
	
	
}

  1. File 字符集乱码处理方式
    2.1编码和解码概念
    在这里插入图片描述
    2.2编码
    2.3字节读取
    在这里插入图片描述
package com.yf.cn.app.test;

import java.io.UnsupportedEncodingException;

/**
 * @author yangfeng
 * @version 1.0
 * @date 2022-09-13 14:26
 */
public class ContentEncode {
    
    

  public static void main(String[] args) throws UnsupportedEncodingException {
    
    
    String msg = "性命生命使命a";
    byte[] bytes = msg.getBytes();//默认使用工程字符集
    System.out.println(bytes.length);

    //编码为其他字符集
    bytes = msg.getBytes("UTF-16LE");
    System.out.println(bytes.length);

    bytes = msg.getBytes("GBK");
    System.out.println(bytes.length);



  }

}

2.3解码

package com.yf.cn.app.test;

import java.io.UnsupportedEncodingException;

/**
 * @author yangfeng
 * @version 1.0
 * @date 2022-09-13 14:26
 */
public class ContentDecode {
    
    

  public static void main(String[] args) throws UnsupportedEncodingException {
    
    
    String msg = "性命生命使命a";
    byte[] bytes = msg.getBytes();//默认使用工程字符集


    msg = new String(bytes);
    System.out.println(msg);
    /***
     * @describe 乱码;
     * 原因:1.字节数不够 2字符集不统一
     * @author yangfeng
     * @date 2022-09-13 14:38:52
     * @param args
     * @return
     **/
    msg = new String(bytes,0,bytes.length,"utf-8");
    System.out.println(msg);
    System.out.println("----字节数不够---");
    msg = new String(bytes,0,bytes.length-2,"utf-8");
    System.out.println(msg);

    System.out.println("----字符集不符---");
    msg = new String(bytes,0,bytes.length-2,"gbk");
    System.out.println(msg);



  }

}

  1. 文件拷贝
package com.yf.cn.app.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author yangfeng
 * @version 1.0
 * @date 2022-09-14 14:18
 */
public class FileCopy {
    
    

  public static void main(String[] args) {
    
    
    copy("D:/images/framework.PNG","D:/images/jiagou.png");

  }


  public static  void copy(String sourcePath,String destPath){
    
    
    //源
    File src = new File(sourcePath);
    //目标文件
    File dp = new File(destPath);
    InputStream is = null;
    OutputStream os = null;
    try {
    
    
      //2.选择流
      is = new FileInputStream(src);
      os = new FileOutputStream(dp);
      byte[] bytes = new byte[1024];
      int len = -1;
      while((len=is.read(bytes))!=-1){
    
    
        //按照读出的字节大小进行写入
        os.write(bytes,0,len);
      }
      //输出流刷新
      os.flush();

    } catch (FileNotFoundException e) {
    
    
      e.printStackTrace();
    } catch (IOException e) {
    
    
      e.printStackTrace();
    }finally {
    
    
      //释放资源,分别关闭输入流和输出流,先打开的后关闭
      try {
    
    
        if(null!=os){
    
    
          os.close();
        }
      } catch (IOException e) {
    
    
        e.printStackTrace();
      }
      //释放输入流
      try {
    
    
        if(null!=is){
    
    
          is.close();
        }
      } catch (IOException e) {
    
    
        e.printStackTrace();
      }
    }

  }

}

  1. 字符流操作
    5.1输入字符流操作
package com.yf.cn.app.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

/**
 * IO 流操作步骤
 * 1.创建源
 * 2.选择流
 * 3.操作
 * 4.释放资源
 * @author yangfeng
 * @version 1.0
 * @date 2022-09-13 15:39
 */
public class FileReaderTest {
    
    

  public static void main(String[] args)  {
    
    

    File file = new File("test.txt");
    Reader reader = null;
    try {
    
    
      reader = new FileReader(file);
      char[] chars = new char[1024];
      int len = -1;
      if((len=reader.read(chars))!=-1){
    
    
        System.out.println(chars);
      }
      reader.close();
    } catch (FileNotFoundException e) {
    
    
      e.printStackTrace();
    } catch (IOException e) {
    
    
      e.printStackTrace();
    }

  }

}

5.2输出字符流操作

package com.yf.cn.app.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * IO 流操作步骤
 * 1.创建源
 * 2.选择流
 * 3.操作
 * 4.释放资源
 * @author yangfeng
 * @version 1.0
 * @date 2022-09-13 15:39
 */
public class FileWriterTest {
    
    

  public static void main(String[] args)  {
    
    

    File file = new File("readme.txt");
    Writer writer = null;
    try {
    
    
      writer = new FileWriter(file,true);
      String msg = "低调是一种美德";
      writer.write(msg);
      writer.flush();

    } catch (IOException e) {
    
    
      e.printStackTrace();
    }finally {
    
    
      try {
    
    
        if(null!=writer){
    
    
          writer.close();
        }
      } catch (IOException e) {
    
    
        e.printStackTrace();
      }
    }


  }

}

  1. 节点流
    6.1节点流定义
    直接用来访问数据源(文件、内存、管道)的流,一般以File和Byte开头的流均为节点流
    6.2架构图
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yangfenggh/article/details/126831938
今日推荐