读取文件(IO-File)

一:简单读取

try(var reader = new FileReader("user.txt")){
while(reader.ready()) {
System.out.print((char)reader.read());
}
} catch (IOException e) {
throw new RuntimeException(e);
}

二:读取一个具体Java文件的代码和行数

字节流读取

1.声明文件地址:

String f="文件地址名";

2.采用读取字节流FileInputStream 

var is = new FileInputStream(f)
try(var is = new FileInputStream(f)){

                }catch(Exception e){
                     e.printStackTrace();
                }

 3.在try下面输出文本代码和行数

  System.out.println(txt);//输出文本代码
  System.out.println(txt.lines().count());//行数

4.完整代码: 

/*
 * Copyright (c) 2020, 2023.
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

/**
 * <p>Project: File - F3</p>
 * <p>Powered by Cyy On 2023-07-20 16:48:20</p>
 * <p>描述:<p>
 *
 * @author Cyy [210585265@qq.com]
 * @version 1.0
 * @since 17
 */

  
public class F4 {
    public static void main(String[] args) {
       
        String f="";
        //字节流
        try(var is = new FileInputStream(f)){
            System.out.println(txt);//输出代码
            System.out.println(txt.lines().count());//行数

                }catch(Exception e){
                     e.printStackTrace();
                }
    }
}

字符流读取 

 1..采用读取字符流FileReader 

FileReader reader = new FileReader(f)

 2.实例化StringBuffer对象 

StringBuilder对象代表一个字符序列可变的字符串,当一个StringBuffer被创建以后,通过append()方法改变这个字符串对象的字符序列,最后通过调用它的toString()方法将其转换为一个String对象。

StringBuilder sbu = new StringBuilder(); 

3. 当在读取的时候进行追加,将指定的字符串追加到此字符序列 

sbu.append();追加

String .format();改变格式

 while(reader.ready()){
           //将用字符流读取的内容转为字符串
           sbu.append(String.format("%c",reader.read()));

           }

4.输出

 直接输出sbu获取内容

输出获取行数

 System.out.println(sbu);
 System.out.println(sbu.toString().lines().count());//行数

 5.完整代码

/*
 * Copyright (c) 2020, 2023.
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

/**
 * <p>Project: File - F3</p>
 * <p>Powered by Cyy On 2023-07-20 16:48:20</p>
 * <p>描述:<p>
 *
 * @author Cyy [210585265@qq.com]
 * @version 1.0
 * @since 17
 */

     
public class F4 {
    public static void main(String[] args){
        //字符流
        try(FileReader reader = new FileReader(f)){
            StringBuilder sbu = new StringBuilder();
           while(reader.ready()){
               sbu.append(String.format("%c",reader.read()));
           }
            System.out.println(sbu);
            System.out.println(sbu.toString().lines().count());//行数
        }catch(Exception e){

                     e.printStackTrace();
                }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用NIO(New IO)来读取文件,相比传统的IO,NIO可以提供更高的性能和更好的并发性能。 下面是将Java读取文件IO改造为NIO的示例代码: ```java import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class NIOFileReader { public static void main(String[] args) throws IOException { Path path = Paths.get("path/to/file"); FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); // 创建一个缓冲区 while (fileChannel.read(byteBuffer) > 0) { // 读取数据到缓冲区 byteBuffer.flip(); // 切换为读模式 while (byteBuffer.hasRemaining()) { System.out.print((char) byteBuffer.get()); // 输出数据 } byteBuffer.clear(); // 切换为写模式 } fileChannel.close(); // 关闭文件通道 } } ``` 在这个示例中,首先使用`FileChannel.open()`方法打开文件通道,并指定了文件的读取模式。然后创建一个ByteBuffer对象作为缓冲区,用于存储读取到的数据。在循环中,使用`FileChannel.read()`方法将数据读取到缓冲区中,然后切换为读模式,逐个输出数据。最后,切换为写模式,进行下一轮的读取。 需要注意的是,使用NIO读取文件需要手动管理缓冲区的大小和数据的读写,这需要开发者具备一定的NIO编程经验。同时,在实际应用中,需要根据具体的业务场景和硬件配置来确定缓存区的大小和并发性能,以达到最佳的性能表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值