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. 工作中常见的功能点

代码实现

java 复制代码
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();
           }
       }
    }
}
java 复制代码
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());
                }
            }
        }
    }
}

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

相关推荐
栈溢出了9 分钟前
Java Lambda 表达式笔记
java·开发语言·intellij-idea
会周易的程序员10 分钟前
使用 pybind11 封装 C++ 日志库 microLog 为 Python 模块
java·c++·python·物联网·架构·日志·aiot
zhixingheyi_tian19 分钟前
一份GC日志的解读
java·开发语言
风止何安啊21 分钟前
🚦 前端并发请求 “交通管制”:别让你的接口堵成早高峰
前端·javascript·面试
ywl47081208729 分钟前
ArrayList和LinkedList内部的实现原理和区别
java·java基础
Fox爱分享39 分钟前
字节二面:1000瓶酒,有一瓶是毒药,多少只老鼠可以查出来?
算法·面试·程序员
沉静的小伙40 分钟前
Spring事务
java·数据库·spring
apihz1 小时前
随机驾考题目(C 照科一 / 科四 2000+ 题)免费API调用教程
android·java·c语言·开发语言·网络协议·tcp/ip
Fox爱分享1 小时前
字节二面智力题:100只老虎和1只羊关在一起,这只羊会不会被吃?
算法·面试·程序员
AIGS0011 小时前
企业AI落地:Agent OS 治理框架实践
java·大数据·人工智能·机器学习·人工智能ai大模型应用