java输入输出流I/O

15 篇文章 1 订阅
13 篇文章 0 订阅

按行读取文件studentInfo.txt的内容,获取学生姓名并写入到studentName.txt文件中,且给每一行按序加上行号;获取studentName.txt文件的长度及修改时间。

studentInfo.txt文件内容格式如下:
王珊珊,王珊珊,20132213806,2015-10-10 14:30:56
张国辉,张国辉,20132213944,2015-10-10 14:31:05
蒋宇宙,蒋宇宙,20132213906,2015-10-10 14:31:24
王燕萍,王燕萍,20132213847,2015-10-10 14:31:16

public class FlleIo {
    public static void main(String[] args){
        try{
            File file = new File("studentName.txt");
            if(!file.exists()){
                file.createNewFile();//创建新文件
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));//写入
            BufferedReader br = new BufferedReader(new FileReader("studentInfo.txt"));//读取
            String line;
            int number = 1;
            while((line = br.readLine())!= null){
                String[] sc = line.split(",");//逗号分割
                bw.write(number+sc[0].trim());
                bw.newLine();
                bw.flush();
                number++;
            }
            bw.close();
            br.close();

            java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");
            String dateTime = df.format(new Date(file.lastModified()));
            System.out.println("文件修改时间:"+dateTime);
            System.out.println("文件长度:"+file.length());
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

}

运行结果:
在这里插入图片描述
新文件内容:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java输入输出流(I/O)提供了在程序中读取和写入数据的方式。它们是通过处理流(或过滤器流)的方式工作的,这些流允许你对数据进行处理和转换。 常用的输入输出流有: 1. FileInputStream:用于从文件中读取字节流。 2. FileOutputStream:用于向文件中写入字节流。 3. FileReader:用于从文件中读取字符流。 4. FileWriter:用于向文件中写入字符流。 5. ByteArrayInputStream:用于从内存中读取字节流。 6. ByteArrayOutputStream:用于向内存中写入字节流。 7. CharArrayReader:用于从字符数组中读取字符流。 8. CharArrayWriter:用于向字符数组中写入字符流。 9. StringReader:用于从字符串中读取字符流。 10. StringWriter:用于向字符串中写入字符流。 下面是一个简单的示例,演示了如何使用FileInputStream和FileOutputStream来复制一个文件: ```java import java.io.*; public class FileCopyDemo { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("input.txt"); out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } } ``` 这个程序将从名为input.txt的文件中读取字节,并将它们写入名为output.txt的文件中。在读取和写入完成后,程序会关闭这两个流。 需要注意的是,在使用输入输出流时,要确保正确地处理异常和关闭流。可以使用try-with-resources语句来简化这个过程,如下所示: ```java try (FileInputStream in = new FileInputStream("input.txt"); FileOutputStream out = new FileOutputStream("output.txt")) { int c; while ((c = in.read()) != -1) { out.write(c); } } catch (IOException e) { e.printStackTrace(); } ``` 在此示例中,使用了try-with-resources语句来创建和管理输入输出流。当try块结束时,会自动关闭这两个流,从而避免了显式地关闭流的需要。 除了上述示例中使用的流之外,Java 还提供了许多其他的输入输出流,以满足不同的需求。你可以根据需要选择适合自己的流。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值