1-File类的使用

package com.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import org.junit.Test;

public class FileTest {
    /**
     * 1、File类对象,对应着物理中的一个文件
     * 2、File类中的方法,仅涉及到如何创建、修改、删除等等对文件的操作,操作不了文件内容
     * 操作文件内容需要io
     * 3、File类的对象,常常作为io流具体类的形参
     */
    @Test
    public void fileTest(){
        File file = new File("hello1.txt");//把物理中的一个文件表示成java里面的一个对象
        
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getParent());
        System.out.println(file.getPath());
        System.out.println(file.getName());
        
        File file1 = new File("hello2.txt");
        /**
         * 重命名
         * 要求file必须存在,file1必须不存在,才能重命名成功。
         * file和file1都必须是文件或都是目录
         * 在同一盘符
         */
        boolean flag = file.renameTo(file1);
        System.out.println("重命名返回"+flag);
        
        /**
         * 文件最后修改日期
         */
        System.out.println(new Date(file1.lastModified()));
        file.list();//返回String数组,仅能获取名字
        file.listFiles();//返回file数组,可获取file对象,对file操作
    }
    
    
    

    public static void createFile(String fileDirParam, String fileName)
            throws IOException {
        File fileDir = new File(fileDirParam);
        boolean fileDirFlag = fileDir.exists();
        
        System.out.println("is fileDir exists "+fileDirFlag);
        if(!fileDirFlag){
            boolean createDirFlag = fileDir.mkdirs();
            System.out.println("is create filedir success " + createDirFlag);
        }
        
        File file = new File(fileDirParam + fileName);
        boolean fileExistFlag = file.exists();
        System.out.println("is fileExist " + fileExistFlag);
        if(!fileExistFlag){
            boolean flag = file.createNewFile();
            System.out.println("is file created " + flag);
        }
        
        
        System.out.println("该分区大小"+file.getTotalSpace()/(1024*1024*1024)+"G"); 
    }
    
    public static Long getFileLength(String fileDirParam, String fileName) throws IOException{
        Long fileLength = 0l;
        InputStream ins = null;//输入流
        
        ins = new FileInputStream(new File(fileDirParam+fileName));
        while(ins.read() != -1){
            fileLength ++;
        }
        System.out.println("file length is "+fileLength);
        
        ins.close();//FileInputStream是有缓冲区的,所以用完之后必须关闭,否则可能导致内存占满,数据丢失
                
        return fileLength;
    }
    
    public static Long getFileLengthNew(String fileDirParam, String fileName) throws IOException{
        Long fileLength = 0l;
        List<Object> objList  = new ArrayList<Object>();
        HashMap ht;
        Hashtable hs;
        objList.hashCode();
        InputStream ins = null;//输入流
        byte[] buffer = new byte[1024];//在栈内存是定义1024个连续的byte单位
        ins = new FileInputStream(new File(fileDirParam+fileName));
        while(ins.read(buffer) != -1){
             
        }
        System.out.println("file length is "+fileLength);
        
        ins.close();//FileInputStream是有缓冲区的,所以用完之后必须关闭,否则可能导致内存占满,数据丢失
                
        return fileLength;
    }

    public static void main(String[] args) {
        try {
            getFileLengthNew("d:/a/d/e/c/", "test.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/fubaizhaizhuren/p/5026109.html