LeoQns
LeoQns

Reputation: 1322

What is the best way to check a .TXT extension file for CSV format data?

I need to Export & Import TXT file fill-up with CSV format data. I need want to do it in MVC4. What is the best approach to do this ?

Txt file can contain a large number of CSV format data,

Upvotes: 1

Views: 656

Answers (2)

Orel Eraki
Orel Eraki

Reputation: 12196

If all you want is to check the file extension, then using lastIndexOf or Split will pretty much do an excellent trick for you.

Using endsWith

String myFile = "some.file.txt";
        System.out.println(myFile.endsWith(".txt"));

Using split

String myFile = "some.file.txt";
String[] myFileArray = myFile.split("\\.(?=[^\\.]+$)");
if (myFileArray[myFileArray.length - 1].equalsIgnoreCase("txt")) {
    System.out.println("Ends with .txt");
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062865

Just run it through a CSV parser (I've used this one in the past - worked fine) and check that it makes semantic sense, and has the same number of columns on each row. That would be very unlikely if it wasn't CSV data. Note: columns != commas - you need to watch out for quoted data "like, this", and line-breaks - both of which a parser will help you with. You cannot just Split by ',' or use line-endings to detect rows - CSV is more complex than that.

Upvotes: 2

Related Questions