aspose file tools*
The moose likes I/O and Streams and the fly likes How to validate a file is a CSV file, or at least a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to validate a file is a CSV file, or at least a text file?" Watch "How to validate a file is a CSV file, or at least a text file?" New topic
Author

How to validate a file is a CSV file, or at least a text file?

Eric Xin Zhang
Greenhorn

Joined: May 21, 2009
Posts: 6
Dear all,

I'm using SuperCSV to parse CSV files in my GUI application. I find that SuperCSV just does not do any validation whether the file is a CSV file, or at least a text file. However, users hope the program could do such validation in case they select a wrong file (for example, an xls file) by mistake.

Could somebody advise how I can check a file is a CSV file or at least text file easily? Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
I don't know that there's any surefire way to test this, but you could probably come up with some rules of thumb. If a file name doesn't end with *.csv" or at least ".txt", chances are it's not a CSV file. Same if it doesn't contain a certain number of actual commas between each newline character. (Note that in some locales CSVs use semicolons instead of commas.)


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

There are classes that try to figure out the MIME type of files (just Google a bit). These are not full proof, and require reading part to all of the file, but at least it's something.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eric Xin Zhang
Greenhorn

Joined: May 21, 2009
Posts: 6
Gosh! I can reply at last.

Last few days I kept hitting error when logging into the forum, either got error page after login or system could not remember my login status.

At the moment I used the way provided by Ulf. Thanks Ulf and Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to validate a file is a CSV file, or at least a text file?