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

  • SEARCH
  • Home
  • 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 877207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:34:05+00:00 2026-05-15T11:34:05+00:00

I have written the following method to detemine whether file in question is formatted

  • 0

I have written the following method to detemine whether file in question is formatted with DOS/ MAC, or UNIX line endings.

I see at least 1 obvious issue:
1. i am hoping that i will get the EOL on the first run, say within first 1000 bytes. This may or may not happen.

I ask you to review this and suggest improvements which will lead to hardening the code and making it more generic.

THANK YOU.

new FileFormat().discover(fileName, 0, 1000);

and then

public void discover(String fileName, int offset, int depth) throws IOException {

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
    FileReader a = new FileReader(new File(fileName));

    byte[] bytes = new byte[(int) depth];
    in.read(bytes, offset, depth);

    a.close();
    in.close();
    int thisByte;
    int nextByte;

    boolean isDos = false;
    boolean isUnix = false;
    boolean isMac = false;

    for (int i = 0; i < (bytes.length - 1); i++) {
        thisByte = bytes[i];
        nextByte = bytes[i + 1];
    if (thisByte == 10 && nextByte != 13) {
            isDos = true;
            break;
        } else if (thisByte == 13) {
            isUnix = true;
            break;
        } else if (thisByte == 10) {
            isMac = true;
            break;
        }
    }
    if (!(isDos || isMac || isUnix)) {
            discover(fileName, offset + depth, depth + 1000);
    } else {
        // do something clever
    }
}
  • 1 1 Answer
  • 3 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-15T11:34:06+00:00Added an answer on May 15, 2026 at 11:34 am

    Your method seems unnecessarily complicated. Why not:

    public class FileFormat {
        public enum FileType { WINDOWS, UNIX, MAC, UNKNOWN }
    
        private static final char CR = '\r';
        private static final char LF = '\n';
    
        public static FileType discover(String fileName) throws IOException {    
    
            Reader reader = new BufferedReader(new FileReader(fileName));
            FileType result = discover(reader);
            reader.close();
            return result;
        }
    
        private static FileType discover(Reader reader) throws IOException {
            int c;
            while ((c = reader.read()) != -1) {
                switch(c) {        
                case LF: return FileType.UNIX;
                case CR: {
                    if (reader.read() == LF) return FileType.WINDOWS;
                    return FileType.MAC;
                }
                default: continue;
                }
            }
            return FileType.UNKNOWN;
        }
    }
    

    Which puts this in a static method that you can then call and use as:

    switch(FileFormat.discover(fileName) {
    case WINDOWS: ...
    case MAC: ...
    case UNKNOWN: ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a following code to get just the file name without extension
I have written the following line of code: NSMutableArray *array=[[NSMutableArray alloc]init]; This allocates some
I have written the following method to read data from a stream in general.
I have written the following method in a class that inherits from the Thread
I have written the following method that prevents all keys from being pressed: private
I have written the following method to return a list of Unserializable classes (LINQ
I have written following method with three sql queries in order to update a
I have written the following in a JavaScript method : <html> ... <script type=text/javascript
I have written the following scala code to download a file . The file
I have written the following method using the Repository mentioned in the following blog-post

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.