码迷,mamicode.com
首页 > 编程语言 > 详细

Java中File类重修

时间:2019-05-13 22:55:48      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:sys   io流   输出流   扩展名   sre   stack   根据   try   import   

IO流

概述

io流:输入输出流(input/output)。流是一组有顺序的,有起点和终点的字节集合,是对各种数据传输的总称或抽象。即数据在两设备之间的传输称为流。流的本质是数据传输。

  • InputStream:所有输入流的超类
  • OutputSream:所有输出流的超类

1、在指定的目录中查找文件后缀为.txt的文件

import java.io.File;  

//在指定的目录中查找文件  
public class FindFile {

    public static void main(String[] args) {
        // 在F盘查找文件后缀名为.txt的文件
        findFile(new File("F:/"), ".txt");
    }

    /**
     *查找文件的方法
     *@param target 查找目标
     *@param ext    文件扩展名
     */
    public static void findFile(File target, String ext) {
        if (target == null) {
            return;
        }

        if (target.isDirectory()) {
            File[] files = target.listFiles();

            // 若访问c盘,有些是系统文件,可能返回空
            if (files != null) {
                for (File file : files) {
                    findFile(file, ".txt");// 递归调用
                }
            }
        } else {
            // 此处表示File是一个文件
            String name = target.getName().toLowerCase();
            if (name.endsWith(ext)) {
                System.out.println(target.getAbsolutePath());
            }
        }
    }// findFile

}

2、从文件读取内容和向文件写入内容输

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;

public class InputAndOutput {
    
    public static void main(String[] args) {
        in();
        out();
    }

    // 从文件读取内容
    private static void in() {
        File file = new File("F:/test.txt");
        // 构建一个文件输入对象
        try {
            InputStream in = new FileInputStream(file);

            StringBuilder sb = new StringBuilder();
            byte[] flush = new byte[1024];
            int len = -1;// 表示每次读取的字节长度

            // 把数据读入到数组中,并返回读取的字节数,当不等于-1时,表示读取到了数据,等于-1时表示读取完成
            while ((len = in.read(flush)) != -1) {
                // 根据读到的字节数组,在转换为字符串内容,追加到StringBuilder中
                sb.append(new String(flush));
            }
            System.out.println(sb);

            // 关闭输入流
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }// in

    // 将内容输出到文件
    private static void out() {
        // 确定目标文件
        File file = new File("F:/test.txt");
        // 构建一个文件输出流对象
        try {
            OutputStream out = new FileOutputStream(file, true);// true 为追加标识
            // 要写入文件的数据
            String info = "小河流水哗啦啦";

            out.write(info.getBytes());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }// out

}

Java中File类重修

标签:sys   io流   输出流   扩展名   sre   stack   根据   try   import   

原文地址:https://www.cnblogs.com/zxfei/p/10859373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!