文件操作和IO

Java中的文件操作

Java中通过java.io.File类来对一个文件进行抽象的描述。File对象并不代表真实存在的该文件。
对文件的一些元信息、路径的操作
具体查看java.io.File

文件内容的读写----流

InputStream

public class Demo9 {
    public static void main(String[] args) throws IOException {
        try(InputStream is = new FileInputStream("test.txt")){
            while(true){
                int b = is.read();
                if(b == -1){
                    break;
                }
                System.out.printf("%c",b);
            }
        }
    }
}

is.read()返回的是读取字节的ASCII码
给输入流添加缓冲区

public class Demo9 {
    public static void main(String[] args) throws IOException {
        try(InputStream is = new FileInputStream("test.txt")){
            byte[] buf = new byte[1024];
            int len;
            while(true){
                len = is.read(buf);
                if(len == -1){
                    break;
                }
                for (int i = 0; i < len; i++) {//缓冲区后面有许多无用的元素
                    System.out.printf("%c",buf[i]);
                }
            }
        }
    }
}

使用Scanner进行字符读取

public class Demo9 {
    public static void main(String[] args) throws IOException {
        try(InputStream is = new FileInputStream("hello.txt")){
            try(Scanner scanner = new Scanner(is,"UTF-8")){
                while(scanner.hasNext()){
                    String s = scanner.next();
                    System.out.println(s);
                }
            }
        }
    }
}

OutputStream

public class Demo10 {
    public static void main(String[] args) throws IOException {
        try(OutputStream os = new FileOutputStream("output.txt")){
            String s = "你好 dabb";
            byte[] buf = s.getBytes("utf-8");
            os.write(buf);
            os.flush();
        }
    }
    public static void main2(String[] args) throws IOException {
        try(OutputStream os = new FileOutputStream("output.txt")){
            byte[] buf= new byte[]{
                    'g','o','o','d','m','y','t','h'
            };

            os.write(buf,0,4);
            os.flush();
        }
    }
    public static void main1(String[] args) throws IOException {
        try(OutputStream os = new FileOutputStream("output.txt")){
            os.write('G');
            os.write('G');
            os.write('G');
            os.write('G');
            os.write('G');

            os.flush();
        }
    }
}

利用PrintWriter

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
    OutputStream os = new FileOutputStream("output1.txt");
    OutputStreamWriter osWriter = new OutputStreamWriter(os,"utf-8");
    PrintWriter writer = new PrintWriter(osWriter);

    writer.print("hello world");


    writer.flush();
}

小练习

删除指定文件

扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且询问用户是否要删除该文件。

public class Demo11 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("要扫描的文件夹:");
        String rootDirPath = scanner.next();
        File rootDir = new File(rootDirPath);
        if(!rootDir.isDirectory()){
            System.out.println("输入的目录不存在");
            return;
        }
        System.out.println("请输入要删除文件的特征字符:");
        String feature = scanner.next();
        scanDir(rootDir,feature);
    }
    public static void scanDir(File dir,String feature){
        File[] files = dir.listFiles();
        for (File file:
             files) {
            System.out.println(file.getAbsolutePath());
            if(file.isFile()){
                tryDelete(file,feature);
            }else{
                scanDir(file,feature);
            }
        }
    }

    public static void tryDelete(File file,String feature){
        Scanner scanner = new Scanner(System.in);
        if(file.getName().contains(feature)){
            System.out.println("是否要删除"+file.getAbsoluteFile());
            String yes = scanner.next();
            while(true){
                if(yes.equals("yes")){
                    file.delete();
                    break;
                }else if(yes.equals("no")){
                    break;
                }else{
                    System.out.println("请输入yes or no");
                    yes = scanner.next();
                }
            }
        }
    }
}

普通文件的复制

public class Demo12 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("源文件:");
        String source = scanner.next();
        File sourceFile = new File(source);
        if(!sourceFile.exists()){
            System.out.println("文件不存在");
            return;
        }
        if(!sourceFile.isFile()){
            System.out.println("要复制的文件不是普通文件");
            return;
        }
        System.out.println("目标文件:");
        String des = scanner.next();
        File desFile = new File(des);
        if(desFile.exists()){
            System.out.println("是否覆盖(yes/no)");
            String yes = scanner.next();
            while(true){
                if(yes.equals("yes")){
                    break;
                }else if(yes.equals("no")){
                    return;
                }else{
                    System.out.println("输入yes or no");
                    yes = scanner.next();
                }
            }
        }

        try(InputStream is = new FileInputStream(source)){
            try(OutputStream os = new FileOutputStream(des)){
                byte[] buf = new byte[1024];
                int len;
                while(true){
                    len = is.read(buf);
                    if(len == -1){
                        break;
                    }
                    os.write(buf,0,len);
                }
                os.flush();
            }
        }
        System.out.println("复制完成");
    }
}

查找指定文件

扫描指定目录,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)

public class Demo13 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的目录");
        String rootDirPath = scanner.next();
        File dir =new File(rootDirPath);
        if(!dir.exists()){
            System.out.println("文件不存在");
            return;
        }
        if(!dir.isDirectory()){
            System.out.println("文件不是目录文件");
        }
        System.out.println("请输入要找的文件中存在的字符");
        String token = scanner.next();
        scanDir(dir,token);
    }

    public static void scanDir(File dir,String token){
        File[] files = dir.listFiles();
        for(File file:files){
            if(file.isDirectory()){
                scanDir(file,token);
            }
            if(file.isFile()){
                if(file.getName().contains(token)){
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值