Returns true if the given filePath refers a text file. - Android File Input Output

Android examples for File Input Output:File Path

Description

Returns true if the given filePath refers a text file.

Demo Code

// Copyright 2009-2011 Google, All Rights reserved
//package com.java2s;

public class Main {
    /**/*from   ww  w . j av  a  2 s. c o  m*/
     * Returns true if the given filePath refers a text file.
     */
    public static boolean isTextFile(String filePath) {
        String contentType = getContentTypeForFilePath(filePath);
        return contentType.startsWith("text/");
    }

    /**
     * Returns the content type for the given filePath.
     */
    public static String getContentTypeForFilePath(String filePath) {
        filePath = filePath.toLowerCase();

        if (filePath.endsWith(".gif")) {
            return "image/gif";
        }
        if (filePath.endsWith(".jpg") || filePath.endsWith(".jpeg")) {
            return "image/jpeg";
        }
        if (filePath.endsWith(".png")) {
            return "image/png";
        }

        if (filePath.endsWith(".apk")) {
            return "application/vnd.android.package-archive; charset=utf-8";
        }

        if (filePath.endsWith(".zip")) {
            return "application/zip; charset=utf-8";
        }

        if (filePath.endsWith(".keystore")) {
            return "application/octet-stream";
        }

        // default
        return "text/plain; charset=utf-8";
    }
}

Related Tutorials