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());
                }
            }
        }
    }
}

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

相关推荐
lllsure33 分钟前
【快速入门】MyBatis
java·后端·mybatis
Exclusive_Cat1 小时前
失败的面试经历(ʘ̥∧ʘ̥)
面试·职场和发展
爱学习的学姐1 小时前
【精品源码】Java宠物领养网站+SpringBoot+VUE+前后端分离
java·spring boot·宠物
网际游侠1 小时前
一份C#的笔试题及答案
面试·c#·笔试
字节源流2 小时前
【SpringMVC】常用注解:@SessionAttributes
java·服务器·前端
贫道绝缘子2 小时前
Leetcode-132.Palindrome Partitioning II [C++][Java]
java·c++·算法·leetcode
信徒_3 小时前
java 中判断对象是否可以被回收和 GCROOT
java·开发语言·jvm
多多*4 小时前
浅谈Mysql数据库事务操作 用mybatis操作mysql事务 再在Springboot中使用Spring事务控制mysql事务回滚
java·数据库·windows·github·mybatis
Ttang234 小时前
SpringBoot(4)——SpringBoot自动配置原理
java·开发语言·spring boot·后端·spring·自动配置·原理
苏雨流丰4 小时前
Java中按照不同字段进行排序
java·开发语言