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

j13ufse2  于 2021-06-02  发布在  Hadoop
关注(0)|答案(3)|浏览(450)

我正在尝试检查文件内容是否为空。我有一个内容为空的源文件。我试过不同的选择,但都不管用。
这是我的密码:

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

而不是new file()等。
如果我哪里出错了,请给我一些建议。
编辑
再弄清楚一点。如果我有一个只有空格或没有空格的文件,我希望我的结果是空的。

z31licg0

z31licg01#

你可以打电话 File.length() (它返回由这个抽象路径名表示的文件的长度)并检查它是否正确 0 . 像这样的

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

    }
}

忽略空白(同样是空的)

你可以用 Files.readAllLines(Path) 像这样的

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;
}
muk1a3rh

muk1a3rh2#

您可以尝试以下方法:
用于处理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);
    }

}

类来测试实用程序

package com.stackoverflow.answers.mapreduce;

import java.io.IOException;

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

public class HDFSOperationsTest {

    public static void main(String[] args) {
        String fileName = "D:/tmp/source/expected.txt";
        try {
            Configuration configuration = new Configuration();
            Path filePath = new Path(fileName);
            System.out.println("isEmptyFile: "
                    + HDFSOperations.isEmptyFile(configuration, filePath));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

}
2hh7jdfx

2hh7jdfx3#

InputStream is = new FileInputStream("myfile.txt");
if (is.read() == -1) {
    // The file is empty!
} else {
    // The file is NOT empty!
}

当然你需要关闭 is 接住 IOException

相关问题