Java Path File Check nio isAsciiText(Path p)

Here you can find the source of isAsciiText(Path p)

Description

Guess whether a file is ASCII text format, using a default threshold where the number of non-ASCII characters found must be < 30%

License

Open Source License

Parameter

Parameter Description
p the file to determine if it is ASCII

Exception

Parameter Description
IOException an exception

Return

true if the file has > 70% ASCII characters, false otherwise

Declaration

public static boolean isAsciiText(Path p) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static final int DEFAULT_BLOCK_SIZE = 4096;

    /**//from  ww w .  j  a  v  a  2 s  . c  o  m
     * Guess whether a file is ASCII text format, using a default threshold where the number
     * of non-ASCII characters found must be < 30%
     * @param p the file to determine if it is ASCII
     * @return true if the file has > 70% ASCII characters, false otherwise
     * @throws IOException
     */
    public static boolean isAsciiText(Path p) throws IOException {
        char[] buf = new char[DEFAULT_BLOCK_SIZE];
        try (BufferedReader reader = Files.newBufferedReader(p, Charset.forName("US-ASCII"))) {
            reader.read(buf);
        } catch (MalformedInputException e) {
            return false;
        }

        return true;
    }
}

Related

  1. isAbsolute(String path)
  2. isAbsolutePath(String path)
  3. isBallerinaProject(Path path)
  4. isBinary(Path file)
  5. isContained(Path contained, Path container)
  6. isDirectory(Path fileOrDir, LinkOption... options)