IO流面试题

题目一:
在磁盘中新建一个文件(如果目录结构不存在,则创建目录)
文件名:data.txt
文件日录:C:\demo\test\files (盘符不限) linux目录~/demo/test/files

题二
在新建的data.txt中添加如下内容:
张三,测试,2019-02-18 02-22-00
李四,测试,2019-02-19 02-22-00
王二,测试,2019-02-20 02-22-00

题三:
递归遍历demo目录 读取以txt 结尾的文件,并将文件的内容以一下格式在控制台输出
注意时间的格式
2019/02/18 02:22:00,测试,张三
2019/02/19 02:22:00,测试,李四
2019/02/20 02:22:00,测试,王二


本题主要考察

  1. 对 IO 流的使用
  2. 对API的熟悉程度
  3. 对递归算法的熟悉程度
  4. 工作中常见的功能点

代码实现

public class FileWriteTest {
    public static void main(String[] args) throws IOException {
        String url = "D:\\demo\\test\\file";
        File fileDir = new File(url);
        if (!fileDir.exists()){
            fileDir.mkdirs();
        }
        FileWriter fw = null;
        BufferedWriter bw = null;

        StringBuffer sb = new StringBuffer();
        sb.append("张三,测试,2019-02-18 02-22-00" + System.getProperty("line.separator"));
        sb.append("李四,测试,2019-02-19 02-22-00" + System.getProperty("line.separator"));
        sb.append("王二,测试,2019-02-20 02-22-00" + System.getProperty("line.separator"));

       try {
           File file = new File(String.format("%s/%s.%s", url, "data", "txt"));
           if (file.createNewFile()) {
               System.out.println("文件创建成功");
               fw = new FileWriter(file);
               bw = new BufferedWriter(fw);
               bw.write(sb.toString());
           } else {
               System.out.println("文件已经存在");
           }
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
            if (bw != null) {
                bw.close();
            }
           if (fw != null) {
               fw.close();
           }
       }
    }
}

package com.siro.datastructures.mianshi;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author sea
 * @date 2023-08-18
 */
public class ReadFromFile {
   /**
     * 以行为单位读取文件,常用于读面向行的格式化文件
     */
    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                //原始数据
//                System.out.println(tempString);
                String[] split = tempString.split(",");
                SimpleDateFormat sdfOld = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                Date parse = sdfOld.parse(split[2]);
                SimpleDateFormat sdfNew = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                String format = sdfNew.format(parse);
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(format + ",");
                stringBuffer.append(split[1] + ",");
                stringBuffer.append(split[0]);
                //格式变化后的数据
                System.out.println(stringBuffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {

                }
            }
        }
    }

    public static void main(String[] args) {
        String fileDir = "D:/demo/test/file";
        File floder = new File(fileDir);
        if (!floder.isDirectory()) {
            System.out.print("请输入文件夹的正确路径");
        } else {
            File[] files = floder.listFiles();
            for (File f : files) {
                if (f.getName().endsWith(".txt")) {
                    ReadFromFile.readFileByLines(f.getPath());
                }
            }
        }
    }
}


如果有收获! 希望老铁们来个三连,点赞、收藏、转发。
创作不易,别忘点个赞,可以让更多的人看到这篇文章,顺便鼓励我写出更好的博客
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值