Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;

import java.io.IOException;

import android.text.TextUtils;

public class Main {
    public static final int FILE_ERROR = -1;
    public static final int FILE_CREATE = 0;
    public static final int FILE_EXISTS = 1;
    public static final int FILE_EXISTS_NULL = 2;
    public static final String FILE_SUFFIX = ".dat";

    public static int checkFileStatus(String dirPath, String fileName, String suffix) {
        int fileStatus;
        String filePath = checkFilePath(dirPath, fileName, suffix);

        if (filePath == null) {
            return fileStatus = FILE_ERROR;
        }

        try {
            File dirFile = new File(dirPath);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
            File mFile = new File(filePath);
            if (!mFile.exists()) {
                fileStatus = FILE_CREATE;
                mFile.createNewFile();
            } else {
                long size = mFile.length();
                if (size > 0) {
                    fileStatus = FILE_EXISTS;
                } else {

                    fileStatus = FILE_EXISTS_NULL;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return FILE_ERROR;
        }
        return fileStatus;

    }

    public static int checkFileStatus(String dirPath, String fileName) {
        return checkFileStatus(dirPath, fileName, null);
    }

    public static String checkFilePath(String dirPath, String fileName, String suffix) {
        if (!TextUtils.isEmpty(dirPath) && !TextUtils.isEmpty(fileName)) {
            if (suffix != null) {
                return dirPath + "/" + fileName + suffix;
            } else {
                return dirPath + "/" + fileName + FILE_SUFFIX;
            }
        }
        return null;
    }

    public static String checkFilePath(String dirPath, String fileName) {
        return checkFilePath(dirPath, fileName, null);
    }
}