Java 如何检查文件内容是否为空

Java 如何检查文件内容是否为空,java,file,hadoop,mapreduce,bufferedreader,Java,File,Hadoop,Mapreduce,Bufferedreader,我试图检查文件内容是否为空。我有一个内容为空的源文件。 我尝试了不同的选择,但都不管用 这是我的密码: Path in = new Path(source); /* * Check if source is empty */ BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(fs.open(in))); } catch

我试图检查文件内容是否为空。我有一个内容为空的源文件。 我尝试了不同的选择,但都不管用

这是我的密码:

  Path in = new Path(source);
    /*
     * Check if source is empty
     */
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(fs.open(in)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (br.readLine().length() == 0) {
            /*
             * Empty file
             */
            System.out.println("In empty");
            System.exit(0);

        }
        else{
            System.out.println("not empty");
        }
    } catch (IOException e) {
        e.printStackTrace();

    }
我试过使用-

1. br.readLine().length() == 0
2. br.readLine() == null
3. br.readLine().isEmpty()
以上所有的一切都是给予,不是空的。我需要使用-

BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(fs.open(in)));
        } catch (IOException e) {
            e.printStackTrace();
        }
而不是新文件()等

如果我出了什么差错,请给我建议

编辑

再清楚不过了。如果我有一个只有空格或 如果没有空格,我希望结果为空

您可以调用(返回此抽象路径名表示的文件长度)并检查它是否为
0
。差不多

File f = new File(source);
if (f.isFile()) {
    long size = f.length();
    if (size != 0) {

    }
}
static boolean isEmptyFile(String source) {
    try {
        for (String line : Files.readAllLines(Paths.get(source))) {
            if (line != null && !line.trim().isEmpty()) {
                return false;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Default to true.
    return true;
}
忽略空白(同样为空) 你可以使用

File f = new File(source);
if (f.isFile()) {
    long size = f.length();
    if (size != 0) {

    }
}
static boolean isEmptyFile(String source) {
    try {
        for (String line : Files.readAllLines(Paths.get(source))) {
            if (line != null && !line.trim().isEmpty()) {
                return false;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Default to true.
    return true;
}

当然,您需要关闭
is
并捕获
IOException

您可以尝试以下操作:

处理isEmptyFile检查的实用程序类

package com.stackoverflow.answers.mapreduce;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HDFSOperations {

    private HDFSOperations() {}

    public static boolean isEmptyFile(Configuration configuration, Path filePath)
            throws IOException {
        FileSystem fileSystem = FileSystem.get(configuration);
        if (hasNoLength(fileSystem, filePath))
            return false;
        return isEmptyFile(fileSystem, filePath);
    }

    public static boolean isEmptyFile(FileSystem fileSystem, Path filePath)
            throws IOException {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(fileSystem.open(filePath)));
        String line = bufferedReader.readLine();
        while (line != null) {
            if (isNotWhitespace(line))
                return false;
            line = bufferedReader.readLine();
        }
        return true;
    }

    public static boolean hasNoLength(FileSystem fileSystem, Path filePath)
            throws IOException {
        return fileSystem.getFileStatus(filePath).getLen() == 0;
    }

    public static boolean isWhitespace(String str) {
        if (str == null) {
            return false;
        }
        int length = str.length();
        for (int i = 0; i < length; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotWhitespace(String str) {
        return !isWhitespace(str);
    }

}

相关:@UnknownOctopus:是的,我也查过了。但它不起作用。我必须使用新的BufferedReader(新的InputStreamReader(fs.open(in));也。我不想使用new File()
System.out.println(“>”+br.readline()+“@Sleafar:output:not empty>null<使用上述代码(如果(br.readline().length()==0){)你为什么不想使用
new File()
这不应该发生。因为第一次读取应该返回文件中的第一个字节值(从0到255的值),如果它返回-1,则表示read已返回enddis。read返回一个整数。我将它们包含在if else中。请您编辑您的答案,将if elsees包含在相同的代码中,我得到的代码为not empty。这对Mee不起作用,即使我的文件是空的。它显示有使用所有这些代码的内容。我仍然怀疑e空白长度为0字节,或者也是空白“空“?即使我的文件仅包含空格或0字节,也应显示文件为空。我的文件包含空格编辑您的问题以包含该小细节。请注意,您可能必须阅读整个文件以确定该定义是否为空。一百万个空格为空,一百万个空格后跟字母“A”不是空。如果我的文件为empty,它并没有显示文件为空。如果文件中有空格,它会显示文件为空