File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Eric Xin Zhang
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Xin Zhang
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic