IO流文件工具包

package ioutil;

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

public class test1 {
    /**
     * 设置创建文件夹的方法:
     *
     * @param path
     * @param folder
     * @return
     */
    public String CreateFile(String path, String folder) {
        String s = "";
        s = path + "\\" + folder;
        File n = new File(s);
        n.mkdir();
        System.out.println("创建文件夹成功!");
        return s;
    }
    //2、设计判断是否为文件夹方法,入参为路径,返回值为是否为文件夹

    /**
     *
     * @param path
     * @return
     */
    public String isDirector(String path){
        String s="";
        File file=new File(path);
        if(file.isDirectory()){
            System.out.println(path+"是文件夹!");
        }else{
            System.out.println(path+"不是文件夹");
        }
        return s;
    }

    /**
     *
     * @param path
     * @return
     */
    //3、设计查询文件夹下所有文件方法,入参为路径,返回值为所有文件的集合(文件夹中有文件和文件夹)
    public List<String> chaxun(String path){
        List<String> l=new ArrayList<String>();
        File f=new File(path);
        File[] files = f.listFiles();
        for (File s:files){
            l.add(s.toString());
        }
        System.out.println(l);
        return l;
    }
    //4、设计查询文件夹下所有文件方法,入参为路径、扩展名,返回值为相同类型文件的集合
    public List<String> chaxun2(String path,String tname){
        List<String> s=new ArrayList<String>();
        File file=new File(path);
        File[] files = file.listFiles();
        for(File file1:files){
            int i=file1.getName().lastIndexOf(".");
            String f2= file1.getName().substring(i+1);//后缀名
            if(f2.equals(tname)){
                s.add(file1.toString());
            }
        }
        System.out.println(s);
        return s;
    }

    /**
     *
     * @param file
     * @param path
     * @return
     * @throws IOException
     */
    //5、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径,返回值判断对应路径下是否存在该文件
    public  Boolean kaobei(String file,String path) throws IOException {
        FileReader fir=new FileReader(file);
        BufferedReader bor=new BufferedReader(fir,1024);
        FileWriter fil=new FileWriter(path);
        BufferedWriter borr=new BufferedWriter(fil,1024);
        String i="";
        while((i=bor.readLine())!=null){
            fil.write(i);
        }
        File file1=new File(path);
        borr.close();
        fil.close();
        bor.close();
        fir.close();
        boolean b=file1.exists();
        if(b){
            System.out.println("存在该文件!");
        }else{
            System.out.println("不存在该文件!");
        }
        return  b;
    }

    /**
     *
     * @param file
     * @param path
     * @param tname
     * @return
     * @throws IOException
     */
    //6、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径、拷贝后的文件名,返回值判断对应路径下是否存在该文件
    public boolean kaobei2(String file,String path,String tname) throws IOException {
        FileReader fil=new FileReader(file);
        BufferedReader bor=new BufferedReader(fil,1024);
        FileWriter fir=new FileWriter(tname);
        BufferedWriter bol=new BufferedWriter(fir,1024);
        String i="";
        while((i=bor.readLine())!=null){
            bol.write(i);
        }
        bol.close();
        fir.close();
        bor.close();
        fil.close();
        File f=new File(path);
        boolean b=f.exists();
        if(b){
            System.out.println("存在该文件!");
        }else{
            System.out.println("不存在该文件!");
        }
        return  b;
    }

    /**
     *
     * @param path
     * @return
     */
    //7、设计文件删除方法,入参为被删除文件路径,返回值为是否删除成果
    public Boolean delelt(String path){
        File f=new File(path);
        Boolean b=f.delete();
        if(b){
            System.out.println("文件删除成功!");
        }else{
            System.out.println("文件删除失败!");
        }
        return b;
    }
}

测试类:

package ioutil;

import org.junit.Test;

import java.io.IOException;

public class test {
    //    public static void main(String[] args) {
    test1 t = new test1();

    @Test
    public void test01() {
        System.out.println("**********第一题********");
        t.CreateFile("D:\\IO流练习文件", "text1.txt");
    }
    @Test
    public void test02(){
        System.out.println("********第二题**********");
        t.isDirector("D:\\IO流练习文件\\text1.txt");
    }
    @Test
    public void test03(){
        System.out.println("********第三题**********");
        t.chaxun("D:\\IO流练习文件");
    }
    @Test
    public void test04(){
        System.out.println("********第四题**********");
        t.chaxun2("D:\\IO流练习文件", "psd");
    }
    @Test
    public void test05() throws IOException {
        //5、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径,返回值判断对应路径下是否存在该文件
        System.out.println("********第五题**********");
        t.kaobei("D:\\IO流练习文件\\HelloWorld.txt","D:\\IO流练习文件\\123.txt");
    }
    @Test
    public void test06() throws IOException {
        //6、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径、拷贝后的文件名,返回值判断对应路径下是否存在该文件
        System.out.println("********第五题**********");
        t.kaobei2("D:\\IO流练习文件\\123.txt","D:\\IO流练习文件","D:\\IO流练习文件\\456.txt");
    }
    @Test
    public void test07(){
        System.out.println("********第五题**********");
        t.delelt("D:\\IO流练习文件\\123.txt");
        t.delelt("D:\\IO流练习文件\\456.txt");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值