Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I find a text file is Unix or Dos format via Java API?

 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

How could I find out a text file is unix or dos format? is there anyway to do it via java api or methods?


thanks in advance

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could count how many newline characters it contains, and how many carriage return characters, and code up a rule of thumb based on their ratio.
 
Rob Spoor
Sheriff
Posts: 20827
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In DOS format they should be equal, in Unix format the number of carriage return should be 0. You'll need to find out what to do if:
  • the number of carriage returns is between 0 and the number of line breaks
  • the number of carriage returns is larger than the number of line breaks, with special subcase if the number of carriage returns is larger than 0 and the number of line breaks is 0 (basically an old Mac OS file)
  •  
    What are you doing? You are supposed to be reading this tiny ad!
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!