Skip to content

Latest commit

 

History

History
1251 lines (986 loc) · 30.9 KB

22. IO.md

File metadata and controls

1251 lines (986 loc) · 30.9 KB

22. IO

1. 绝对路径/相对路径

package com.alexanderli95.file;

import java.io.File;

/**
 * 文件分隔符 \(Windows)  /(Linux and Others)
 * 路径分隔符 ;
 */
public class Demo01 {
    public static void main(String[] args) {
        System.out.println(File.pathSeparator);  //打印路径分隔符
        System.out.println(File.separator);      //文件分隔符

        //路径表示形式
        String path="E:\\db.xml";  //不推荐使用
        String path2="E:"+File.separator+"db.xml";  //适合动态生成时使用,可以跨平台使用
        String path3="E:/db.xml";  //推荐使用

        //相对路径构建文件
        String parentPath="E:";
        String fileName="db.xml";
        File src=new File(parentPath,fileName);      //第一种方式
        src=new File(new File(parentPath),fileName); //第二种方式

        System.out.println(src.getName());
        System.out.println(src.getAbsolutePath());  //绝对路径
        System.out.println(src.getPath());          //输出相同

        //绝对路径构建文件
        src=new File("E:/db.xml");
        System.out.println(src.getName());
        System.out.println(src.getAbsolutePath());  //绝对路径
        System.out.println(src.getPath());          //输出相同

        //没有盘符的情况
        src=new File("test.txt");
        System.out.println(src.getAbsolutePath());  //绝对路径
        System.out.println(src.getPath());          //输出不同
    }
}

2.常用方法

==1==

package com.alexanderli95.file;

import java.io.File;
import java.io.IOException;

/**
 * 常用方法:
 * 1.文件名
 * 2.判断信息
 * 3.长度(字节数)
 * 4.创建和删除文件
 */
public class Demo02 {
    public static void main(String[] args) {
        test5();
    }

    public static void test(){   //文件名及路径操作
        File src=new File("E:/db.xml");
        System.out.println(src.getName());
        System.out.println(src.getAbsolutePath());  //绝对路径
        System.out.println(src.getPath());          //输出相同
        System.out.println(src.getParent());        //返回父目录,如果构建时是相对路径(没有父目录),返回空
    }

    public static void test2(){  //文件基本信息操作
        String path="E:/db.xml";
        File src=new File(path);
        System.out.println("文件基本信息:");
        if(true==src.exists()){
            System.out.println("文件存在");
            System.out.println("文件大小:"+src.length()+"字节");   //只有文件才能读取长度,文件夹无法读取
        }else{
            System.out.println("文件不存在");
        }

        if(true==src.canRead()){
            System.out.println("文件可读");
        }else{
            System.out.println("文件不可读");
        }

        if(true==src.canWrite()){
            System.out.println("文件可写");
        }else{
            System.out.println("文件不可写");
        }

        if(src.isFile()){
            System.out.println("文件");
        }else if(src.isDirectory()){
            System.out.println("文件夹");
        }
    }

    public static void test3(){  //文件的创建
        String path="E:/db.txt";
        File src=new File(path);
        if(!src.exists()){
            try {
                System.out.println("该文件不存在");
                boolean token=src.createNewFile();
                System.out.println(token?"文件创建成功":"文件创建失败");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("文件已存在");
        }
    }

    public static void test4(){  //文件的删除
        String path="E:/db.txt";
        File src=new File(path);
        if(src.exists()){
            boolean token=src.delete();
            System.out.println(token?"删除成功":"删除失败");
        }else{
            System.out.println("文件不存在,删除失败");
        }
    }

    public static void test5(){  //创建临时文件
        try {
            File temp=File.createTempFile("tes",".temp",new File("E:"));
            Thread.sleep(10000);
            temp.deleteOnExit();  //程序结束时删除临时文件
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

==2==

package com.alexanderli95.file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.StringTokenizer;

/**
 * 5.目录操作
 * 6.查找文件
 *
 */
public class Demo03 {
    public static void main(String[] args) {
        test4();
    }

    public static void test1(){     //目录操作
        String path="E:/zzzzz/zz";
        File src=new File(path);
        boolean token=src.mkdir();  //创建目录,如果父目录不存在则创建失败
        if(token){
            System.out.println("创建目录成功");
        }else{
            System.out.println("创建目录失败");
        }
        token=src.mkdirs(); //创建目录,如果父目录不存在则一同创建
        if(token){
            System.out.println("创建目录成功");
        }else{
            System.out.println("创建目录失败");
        }
    }

    public static void test2(){     //输出指定目录下的文件列表
        String path="E:/Java";
        File src=new File(path);
        if(src.isDirectory()){
            String[] fileNames=src.list();
            for(String temp:fileNames){
                System.out.println(temp);
            }
        }
    }

    public static void test3(){  //子目录文件file对象
        String path="E:/Java";
        File src=new File(path);
        if(src.isDirectory()){
            File[] files=src.listFiles();
            for(File temp:files){
                System.out.println(temp.getAbsolutePath());
            }
        }
    }

    public static void test4(){  //子文件.jar对象(过滤出所有后缀为.jar的文件对象)
        //命令设计模式
        String path="E:/Java";
        File src=new File(path);
        if(src.isDirectory()){
            File[] files=src.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    //System.out.println(dir.getAbsolutePath());
                    return new File(dir,name).isFile()&&name.endsWith(".jar");  //进一步过滤掉.jar结尾的文件夹
                }
            });

            System.out.println("目录"+path+"中后缀为.jar的文件列表:");
            for(File temp:files){
                System.out.println(temp.getAbsolutePath());
            }
        }
    }
}

==3==

package com.alexanderli95.file;

import java.io.File;
import java.util.Arrays;

/**
 * 输出指定目录下所有文件和文件夹的信息
 * 1.递归
 */
public class Demo04 {
    public static void main(String[] args) {
        String path="E:/Checker";
        File parent=new File(path);

        printFiles(parent);

        File[] roots=File.listRoots(); //根目录
        System.out.println(Arrays.toString(roots));
        
        //可以制作资源管理器,会输出本计算机所有文件和文件夹
//        for(File sub:roots){
//            printFiles(sub);
//        }
    }

    public static void printFiles(File src){
        if(null==src || !src.exists()){
            return ;
        }
        System.out.println(src.getAbsolutePath());
        if(src.isDirectory()){
            for(File sub:src.listFiles()){
                printFiles(sub);  //递归
            }
        }
    }
}

3.IO 流

==IO流分类==:

1.输入与输出流;

2.字节流:二进制,可以处理所有文件; 字符流:只能处理文本文件;

3.功能:节点:包裹源头;处理:增强功能,提高性能;

==字符流与字节流==:

1.字节流:

输入流:InputStream read(byte[] b)、read()+close()

​ FileInputStream()

输出流:OutputStream write()、write()+flush()+close()

​ FileOutputStream()

2.字符流:

输入流:Reader read(char[] cbuf)、read()+close()

​ FileReader()

输出流:Writer write()、write()+flush()+close()

​ FileWriter()

3.1 字节流

==读取文件==:

package com.alexanderli95.IO;

import java.io.*;

public class byteIO {
    public static void main(String[] args) {
        //1.建立联系,File对象
        File src=new File("E:/a.txt");
        //2.选择流
        InputStream is=null;
        try {
            is=new FileInputStream(src);
            //3.不断读取,缓冲数组
            byte[] car=new byte[10];
            int len=0;  //接收实际读取大小
            while (-1!=(len=is.read(car))){
                String info=new String(car,0,len);
                System.out.print(info);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件不存在");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("无法读取文件");
        }finally {
            //4.释放资源
            if(null!=is){
                try {
                    is.close();
                } catch (IOException e) {
                    System.out.println("文件关闭失败");
                }
            }
        }
    }
}

==写入文件==:

package com.alexanderli95.IO;

import java.io.*;

public class FileWriteDemo {
    public static void main(String[] args) {
        //1.建立联系
        File file=new File("E:/a.txt");
        //2.选择流 文件输出流
        OutputStream os=null;
        try {
            os=new FileOutputStream(file,true);  //true:以追加的方式写文件,默认为false(覆盖)
            //3.操作
            String str="Alexander file.\r\n";
            byte[] bytes=str.getBytes();  //转为字节数组
            os.write(bytes,0,bytes.length);

            os.flush();  //强制刷新出去
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件未找到");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件写入失败");
        }finally {
            if(null!=os){
                try {
                    //4.释放文件
                    os.close();
                } catch (IOException e) {
                    System.out.println("文件关闭失败");
                }
            }
        }

    }
} 

==拷贝文件==:

package com.alexanderli95.IO;

import java.io.*;

public class FileCopyDemo {
    public static void main(String[] args) {
        File path01=new File("E:/a.txt");
        File path02=new File("E:/b.txt");
        InputStream is=null;
        OutputStream os=null;

        if(path01.isFile() && path02.isFile()) {
            try {
                is = new FileInputStream(path01);
                os = new FileOutputStream(path02, true);

                byte[] bytes = new byte[10];
                int len = 0;
                while (-1 != (len = is.read(bytes))) {
                    os.write(bytes, 0, len);
                }
                os.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("文件未找到");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件读取失败");
            } finally {

                //先打开的后关闭
                if (null != os) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

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


            }

            System.out.println("文件拷贝成功");
        }else{
            System.out.println("目录不可操作");
        }

    }
}

==文件处理工具类==:

package com.alexanderli95.IO;

import java.io.*;

/**
 * 文件操作工具类
 */
public class FileUtils {
    /**
     * 单一文件的拷贝
     * @param orignFile
     * @param copyedFile
     */
    public static void copyFile(File orignFile,File copyedFile) throws IOException {
        if(!orignFile.isFile()){
            System.out.println("只能拷贝文件");
            throw new IOException("只能拷贝文件");
        }
        if(!copyedFile.isDirectory()){
            System.out.println("不能建立与文件夹同名的文件");
            throw new IOException("不能建立与文件夹同名的文件");
        }

        InputStream is=null;
        OutputStream os=null;

        if(orignFile.isFile()) {
            try {
                is = new FileInputStream(orignFile);
                os = new FileOutputStream(copyedFile, true);

                byte[] bytes = new byte[10];
                int len = 0;
                while (-1 != (len = is.read(bytes))) {
                    os.write(bytes, 0, len);
                }
                os.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("文件未找到");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件读取失败");
            } finally {

                //先打开的后关闭
                if (null != os) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

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


            }

            System.out.println("文件拷贝成功");
        }else{
            System.out.println("目录不可操作");
        }

    }

    /**
     * 单一文件的拷贝
     * @param orignPath
     * @param copyPath
     */
    public static void copyFile(String orignPath,String copyPath) throws IOException {
        File orignFile=new File(orignPath);
        File copyedFile=new File(copyPath);

        copyFile(orignFile,copyedFile);
    }

    /**
     * 文件夹的拷贝(基于文件对象)
     * @param src
     * @param dest
     */
    public static void copyDir(File src,File dest) throws IOException {
        if(src.isDirectory()){
            dest=new File(dest,src.getName());
        }
        copyDirDetails(src,dest);
    }

    /**
     * 文件夹的拷贝(基于路径)
     * @param srcPath
     * @param destPath
     */
    public static void copyDir(String srcPath,String destPath) throws IOException {
        File src=new File(srcPath);
        File dest=new File(destPath);
        copyDir(src,dest);
    }

    public static void copyDirDetails(File src,File dest) throws IOException {
        if(src.isFile()){     //文件
            FileUtils.copyFile(src,dest);
        }else if(src.isDirectory()){    //文件夹
            //确保目标文件目录存在
            dest.mkdirs();
            //获取下一级目录
            for(File sub:src.listFiles()){
                copyDirDetails(sub,new File(dest,sub.getName()));
            }
        }
    }
}

3.2 字符流

==读取文件==:

package com.alexanderli95.IO.charIO;

import java.io.*;

/**
 * 文件读取(字符流)
 */
public class Demo01 {
    public static void main(String[] args) {
        //创建源
        File file=new File("E:/a.txt");
        //选择流
        Reader reader=null;
        try {
            reader=new FileReader(file);
            //读取操作
            char[] chars=new char[1024];
            int len=0;
            while (-1!=(len=reader.read(chars))){
                String str=new String(chars,0,len);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件不存在");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败");
        }finally {
            if(null!=reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

==写入文件==:

package com.alexanderli95.IO.charIO;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * 字符流文件输出
 */
public class Demo02 {
    public static void main(String[] args) {
        File file=new File("E:/c.txt");
        Writer wr=null;
        try {
            wr=new FileWriter(file,true);
            String str="Alexander file 02\r\n";
            wr.write(str);
            wr.append("123");

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

    }
}

3.3 处理流

==增强功能、提高性能、节点流之上==

3.3.1 字节缓冲流

BufferedInputStream

BufferedOutputStream

3.3.2 字符缓冲流

BufferedReader readLine()

BufferedWriter newLine()

==直接在节点流外面包裹一层缓冲流即可==

乱码问题:

package com.alexanderli95.IO.convert;

import java.io.UnsupportedEncodingException;

public class ConvertDemo {
    public static void main(String[] args) {
        String str="亚历克斯";
        byte[] bytes=str.getBytes();
        System.out.println(new String(bytes,0,4));  //字节数不完整也会造成乱码
    }

    public static void test1() throws UnsupportedEncodingException {
        /**
         * 编码和解码的字符集必须相同,否则会造成乱码
         */
        String str="亚历克斯";
        byte[] data=str.getBytes();
        System.out.println(new String(data));
        data=str.getBytes("GBK");
        System.out.println(new String(data));  //字符集不统一出现乱码

        //编码
        byte[] d="亚历克斯".getBytes("GBK");
        //解码
        str=new String(d,"GBK");
        System.out.println(str);
    }
}

3.4 转换流

package com.alexanderli95.IO.convert;

import java.io.*;

/**
 * 转换流:
 * 1.输出流:OutputStreamWriter
 * 2.输入流:InputStreamReader
 */
public class ConvertDemo02 {
    public static void main(String[] args) throws IOException {

        //指定解码字符集
        BufferedReader br=new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(new File("E:/aa.txt")),"UTF-8")
        );

        //指定编码字符集
        BufferedWriter bw=new BufferedWriter(
                new OutputStreamWriter(
                        new FileOutputStream(new File("E:/bb.txt")),"UTF-8"
                )
        );



        String info=null;
        while (null!=(info=br.readLine())){
            System.out.println(info);
            bw.write(info,0,info.length());
            bw.newLine();  //换行符
        }

        br.close();
        bw.close();
    }
}

3.5 字节数组流

1.节点流:

1)字节数组:输入流:ByteArrayInputStream read(byte[] b)+close();

​ 输出流:ByteArrayOutputStream toByteArray() 不使用多态

package com.alexanderli95.IO;

import java.io.*;

public class ByteArrayDemo {
    public static void main(String[] args) {
        try {
            read(write());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 输出流,操作与文件输出流不同,有新增方法,不能使用多态
     *
     */
    public static byte[] write() throws IOException {

        //目的地
        byte[] data;
        //选择流
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        //操作写出
        String msg="亚历克斯";
        byte[] info=msg.getBytes();
        byteArrayOutputStream.write(info,0,info.length);

        //获取数据(不同点)
        data=byteArrayOutputStream.toByteArray();

        byteArrayOutputStream.close();
        return data;
    }

    /**
     * 输入流,操作与文件输入流一致
     *
     */
    public static void read(byte[] data) throws IOException {


        //选择流
        InputStream is=new BufferedInputStream(
                new ByteArrayInputStream(
                        data
                )
        );

        //操作
        byte[] flush=new byte[1024];
        int len=0;
        while (-1!=(len=is.read(flush))){
            System.out.println(new String(flush,0,len));
        }

        is.close();
    }
}

==字节数组流与文件流对接==

package com.alexanderli95.IO;

import java.io.*;

public class ByteArrayDemo02 {
    public static void main(String[] args) throws IOException {
        byte[] dest=getBytesFromFile("E:/1.jpg");
        toFileFromByteArray(dest,"E:/cc");

        //System.out.println(new String(dest,0,dest.length));
    }

    /**
     * 文件->字节数组
     * @param strPath
     * @return
     * @throws IOException
     */
    public static byte[] getBytesFromFile(String strPath) throws IOException {
        File src=new File(strPath);
        byte[] dest=null;

        InputStream is=new BufferedInputStream(new FileInputStream(src));
        ByteArrayOutputStream bos=new ByteArrayOutputStream();

        byte[] flush=new byte[1024];
        int len=0;
        while (-1!=(len=is.read(flush))){
            bos.write(flush,0,len);
        }

        bos.flush();

        dest=bos.toByteArray();

        bos.close();
        is.close();

        return dest;
    }

    /**
     * 字节数组->文件
     * @param src
     * @param destPath
     * @throws IOException
     */
    public static void toFileFromByteArray(byte[] src,String destPath) throws IOException{
        File dest=new File(destPath);

        InputStream is=new BufferedInputStream(new ByteArrayInputStream(src));
        OutputStream os=new BufferedOutputStream(new FileOutputStream(dest));

        byte[] flush=new byte[1024];
        int len=0;
        while (-1!=(len=is.read(flush))){
            os.write(flush,0,len);
        }

        os.flush();

        os.close();
        is.close();
    }
}

2.处理流

1)基本类型+String 保留数据+类型

输入流:DataInputStream readXxx

输出流:DataOutputStream writeXxx

==新增方法不能发生多态==

package com.alexanderli95.IO;

import java.io.*;

/**
 * 常见错误
 * java.io.EOFException: 已经达到文件末尾,没有读取到内容
 */
public class DataDemo01 {
    public static void main(String[] args) {
        try {
            //dataWrite("E:/data.txt");
            dataRead("E:/data.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将数据和类型写入到文件
     * @param srcPath
     * @throws IOException
     */
    public static void dataWrite(String srcPath) throws IOException {
        double d=1.2;
        int i=1;
        char c='a';

        File src=new File(srcPath);
        DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(src)));

        dos.writeChar(c);
        dos.writeInt(i);
        dos.writeDouble(d);

        dos.flush();
        dos.close();
    }

    /**
     * 从文件中读取数据和类型
     */
    public static void dataRead(String srcPath) throws IOException {
        File src=new File(srcPath);
        DataInputStream dis=new DataInputStream(new BufferedInputStream(new FileInputStream(src)));

        char c=dis.readChar();
        int i=dis.readInt();
        double d=dis.readDouble();
        String str=c+" "+i+" "+d;

        System.out.println(str);

        dis.close();
    }
}

2)引用类型(对象)保留数据+类型

反序列化:输入流:ObjectInputStream readObject()

序列化:输出流:ObjectOutputStream writeObject()

==注==

1.先序列化再反序列化;反序列化顺序必须与序列化顺序一致

2.不是所有的对象都可以序列化,java.IO.Serializable

​ 不是所有的属性都需要序列化,triansient

==Employee.java==

package com.alexanderli95.IO.objectStream;


/**
 * Serializable是空接口,只起标识作用
 */
public class Employee implements java.io.Serializable{  //必须实现Serializable才能实现序列化
    private int ID;
    private transient String name;  //不想使某属性序列化需用transient描述
    private double salary;

    public Employee(int ID, String name, double salary) {
        this.ID = ID;
        this.name = name;
        this.salary = salary;
    }

    public Employee() {
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

==Demo01.java==

package com.alexanderli95.IO.objectStream;

import java.io.*;

public class Demo01 {
    public static void main(String[] args) {
        try {
            seri("E:/employee.txt");

            Employee emp=null;
            emp=readSeri("E:/employee.txt");

            System.out.println(emp.getID()+" "+emp.getSalary()+" "+emp.getName());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //序列化
    public static void seri(String destPath) throws IOException {
        Employee employee=new Employee(1,"alex",10000);
        File dest=new File(destPath);
        ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));

        oos.writeObject(employee);

        oos.close();

    }

    //反序列化
    public static Employee readSeri(String srcPath) throws IOException, ClassNotFoundException {
        File src=new File(srcPath);

        ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream(src)));

        Object obj=ois.readObject();
        Employee employee=null;
        if(obj instanceof Employee){
            employee=(Employee) obj;
        }
        ois.close();

        return employee;
    }
}

3.6 装饰设计模式

==类与类之间的关系==:

1.依赖:形参|局部变量

2.关联:属性

​ 聚合:属性整体与部分不一致的声明周期

​ 组合:属性整体与部分一致的生命周期

3.继承:父子类关系

4.实现:接口与实现类关系

扩音器:

package com.alexanderli95.IO.ampli;

public class Voice {
    private int voice=10;

    public Voice(int voice) {
        this.voice = voice;
    }

    public Voice() {
    }

    public int getVoice() {
        return voice;
    }

    public void setVoice(int voice) {
        this.voice = voice;
    }

    public void say(){
        System.out.println(voice);
    }
}
package com.alexanderli95.IO.ampli;

public class Amplifilter {
    private Voice voice;

    public Amplifilter(Voice voice) {
        this.voice = voice;
    }

    public Amplifilter() {
    }

    public void say(){
        System.out.println(voice.getVoice()*100);;
    }
}
package com.alexanderli95.IO.ampli;

public class Demo {


    public static void main(String[] args) {
        Voice voice=new Voice();
        voice.say();

        Amplifilter amplifilter=new Amplifilter(voice);
        amplifilter.say();
    }
}

3.7 文件分割与合并

package com.alexanderli95.IO.splitFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SplitFile {
    private String filePath;   //文件路径
    private String fileName;
    private int blockNum;      //文件块数量
    private List<String> blockPath;  //文件块路径
    private long blockSize;     //文件块大小

    public SplitFile(String filePath) {
        this(filePath,1024);
    }

    public SplitFile(String filePath, long blockSize) {
        this();
        this.filePath = filePath;
        this.blockSize = blockSize;
    }

    public SplitFile() {
        blockPath=new ArrayList<String>();
    }

    public void init(){
        File src=null;
        if(filePath==null || (src=new File(filePath)).exists()){
            return;
        }
        if(src.isDirectory()){
            return;
        }

        this.fileName=src.getName();

        //计算块数 实际大小与每块大小
        long length=src.length();
        if(length<this.blockSize){
            this.blockSize=length;
        }

        //确定块数
        blockNum=(int)Math.ceil(length*1.0/this.blockSize);

        initName();
    }

    private void initName(){
        for(int i=0;i<blockNum;i++){
            this.blockPath.add(this.fileName+"_part"+i);
        }
    }

    public String getFilePath() {
        return filePath;
    }

    public String getFileName() {
        return fileName;
    }

    public int getBlockNum() {
        return blockNum;
    }

    public List<String> getBlockPath() {
        return blockPath;
    }

    public long getBlockSize() {
        return blockSize;
    }
}