您可以使用trim()方法去除字符串中的空字符。trim()方法会返回一个去除了字符串前后空格的新字符串。以下是一个示例代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileReadExample {
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                // 去除行中的空字符
                line = line.trim();
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在上述代码中,我们使用BufferedReader和FileReader来读取文件中的内容。在每个读取的行中,我们使用trim()方法去除行中的空字符,并打印出结果

java代码中 文件读取流如何去除空字符

原文地址: https://www.cveoy.top/t/topic/inmK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录