Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 893815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:16:51+00:00 2026-05-15T14:16:51+00:00

I wrote the following method to see whether particular file contains ASCII text characters

  • 0

I wrote the following method to see whether particular file contains ASCII text characters only or control characters in addition to that. Could you glance at this code, suggest improvements and point out oversights?

The logic is as follows: “If first 500 bytes of a file contain 5 or more Control characters – report it as binary file”

thank you.

public boolean isAsciiText(String fileName) throws IOException {

    InputStream in = new FileInputStream(fileName);
    byte[] bytes = new byte[500];

    in.read(bytes, 0, bytes.length);
    int x = 0;
    short bin = 0;

    for (byte thisByte : bytes) {
        char it = (char) thisByte;
        if (!Character.isWhitespace(it) && Character.isISOControl(it)) {

            bin++;
        }
        if (bin >= 5) {
            return false;
        }
        x++;
    }
    in.close();
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:16:52+00:00Added an answer on May 15, 2026 at 2:16 pm

    Since you call this class “isASCIIText”, you know exactly what you’re looking for. In other words, it’s not “isTextInCurrentLocaleEncoding”. Thus you can be more accurate with:

    if (thisByte < 32 || thisByte > 127) bin++;
    

    edit, a long time later — it’s pointed out in a comment that this simple check would be tripped up by a text file that started with a lot of newlines. It’d probably be better to use a table of “ok” bytes, and include printable characters (including carriage return, newline, and tab, and possibly form feed though I don’t think many modern documents use those), and then check the table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following code for text file encryption and decryption. The encryption process
I've enabled ARC, In my didFinishLaunchingWithOptions method, I wrote the following code: AppDelegate.h: @interface
We are using the following method to write the log to log file. The
I wrote a javascript method to display an image dynamically depending on whether or
I have to text file in the following format : Word [tab][tab] Word [Carriage
I'm trying to run an example task that I wrote. I've placed the file
I wrote the following code in a JSP file: <c:catch var=e> <% int accountNumber
All, I wrote the following wrapper that extends Zend_Db class to provide db connectivity
I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
Is there a better/shorter way to write the whoAmI method in the following code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.