Here you can find the source of isAsciiText(Path p)
Parameter | Description |
---|---|
p | the file to determine if it is ASCII |
Parameter | Description |
---|---|
IOException | an exception |
public static boolean isAsciiText(Path p) throws IOException
//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; } }