sakthi
sakthi

Reputation: 81

how to find file type in java

How do I find a file type, for example if .xls go to xlsconvertcsv method or .xlsx go to xlsscsv method, How to do that.?

I used getCanonicalPath() method,I found the file type, but I did not able to convert string to file.

public static void main(String[] args) throws IOException {

        File inputFile = new File("test.xls");

        File outputFile = new File("output1.csv");

        String out=inputFile .getCanonicalPath();

        if(out.endsWith(".xls"))
        {
          System.out.print("Text filei\n"+out);
          convertToXls(out, outputFile);
        }
    //System.out.println("out"+out);
    //convertToXls(inputFile, outputFile);
}

Upvotes: 0

Views: 153

Answers (5)

Paul A
Paul A

Reputation: 56

Try This:

public static void main(String[] args) throws IOException {

    File inputFile = new File("test.xls");
    File outputFile = new File("output1.csv");

    if(inputFile.etCanonicalPath().endsWith(".xls")) {
      System.out.print("Text filei\n"+out);
      convertToXls(inputFile, outputFile);
    }
    //System.out.println("out"+out);
    //convertToXls(inputFile, outputFile);
}

Upvotes: 1

Complicated
Complicated

Reputation: 85

This is what you want I think, you can use switch on extension of file and call corresponding methods.

public static void main(String[] args) throws IOException {
    File inputFile = new File("test.xls");
    File outputFile = new File("output1.csv");
    String inPath =  inputFile.getCanonicalPath();

    String ext = inPath.substring(inPath.lastIndexOf(".") + 1, inPath.length());
    switch(ext){
        case "xls":
            convertXls(inputFile,outputFile);
            break;
        case "xlsx":
            convertXlsx(inputFile,outputFile);
            break;
    }
}

Upvotes: 0

Mitul Maheshwari
Mitul Maheshwari

Reputation: 2647

you can use following code for that

import javax.activation.MimetypesFileTypeMap;
import java.io.File;

class GetMimeType {
   public static void main(String args[]) {
      File f = new File("gumby.gif");
      System.out.println("Mime Type of " + f.getName() + " is " +
                     new MimetypesFileTypeMap().getContentType(f));
// expected output :
// "Mime Type of gumby.gif is image/gif"

} }

Upvotes: 1

Ryo
Ryo

Reputation: 1035

You need to show what error made your program not work

I think you need to check if file existed before process

 if(inputFile.exists()){

     String out=inputFile .getCanonicalPath();
     if(out.endsWith(".xls")) {
        System.out.print("Text filei\n"+out);
       convertToXls(out, outputFile);
     }
 }

Upvotes: 0

Pphoenix
Pphoenix

Reputation: 1473

If you only want the file name, try getName()

    File inputFile = new File("test.xls");

    File outputFile = new File("output1.csv");

    String out=inputFile.getName();

    if(out.endsWith(".xls"))
    {
      System.out.print("Text filei\n"+out);
      convertToXls(out, outputFile);
    }

Upvotes: 0

Related Questions