Properties file Read - Java java.util

Java examples for java.util:Properties File

Description

Properties file Read

Demo Code


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String filePath = "java2s.com";
        System.out.println(fileRead(filePath));
    }/*from w w w  .j a v a 2 s  .co m*/
    private static final Logger logger = Logger
            .getLogger("com.aircell.abs.acpu.videoservice.AtomicFileUtils");
    public static final Object IOLOCK = new Object();
    private static final String MAGIC_CODE = "CAFEGOGO";
    public static Properties fileRead(String filePath) {
        if (filePath == null) {
            return null;
        }
        File file = new File(filePath);
        return fileRead(file);

    }
    public static Properties fileRead(File file) {

        Properties props = null;

        if (file == null) {
            return null;
        }

        if (isValidFile(file) <= 0) {
            return null;
        }

        synchronized (IOLOCK) {

            if (file.canRead()) {
                // read all lines,  check if the magiccode exists in the first and last record
                Path path = FileSystems.getDefault().getPath(
                        file.getAbsolutePath());
                logger.debug("file path = " + path);
                List<String> records;
                props = new Properties();
                try {
                    records = Files.readAllLines(path,
                            StandardCharsets.UTF_8);
                    int size = records.size();
                    for (int n = 1; n < size - 1; ++n) {
                        String record = records.get(n);
                        String kvp[] = record.split("=");
                        if (kvp.length > 1) {
                            props.put(kvp[0], kvp[1]);
                        } else if (kvp.length == 1) {
                            props.put(kvp[0], "");
                        } else {
                            continue;
                        }
                    }
                } catch (IOException ioException) {
                    logger.error("Reading properties IOException for {} "
                            + path.getFileName());
                    ioException.printStackTrace();
                    props = null;
                } catch (Exception e) {
                    logger.error("File IOException for {} "
                            + path.getFileName());
                    e.printStackTrace();
                    props = null;
                }
            }
        }
        return props;
    }
    /**
     *
     * @param filePath absolute file path to check the validity of
     * @return 0 if file does not exists, 1 if file is valid, -1 if file is not valid
     */
    public static int isValidFile(String filePath) {
        File file = new File(filePath);
        return isValidFile(file);
    }
    public static int isValidFile(File file) {

        int retVal = -1;

        if (file == null) {
            return retVal;
        }
        synchronized (IOLOCK) {
            if (!file.exists()) {
                logger.debug("File {} does not exists"
                        + file.getAbsoluteFile());
                retVal = 0;
                return retVal;
            }
            if (file.exists() && file.canRead()) {
                // read all lines,  check if the magiccode exists in the first and last record
                Path path = FileSystems.getDefault().getPath(
                        file.getAbsolutePath());
                logger.debug("file path = " + path);
                List<String> fileList;
                try {
                    fileList = Files.readAllLines(path,
                            StandardCharsets.UTF_8);
                    int size = fileList.size();
                    if (fileList.get(0).equals(MAGIC_CODE)
                            && fileList.get(size - 1).equals(MAGIC_CODE)) {
                        logger.debug("file {} is valid "
                                + path.getFileName());
                        retVal = 1;
                    }
                } catch (IOException ioException) {
                    logger.error("File IOException for {} "
                            + path.getFileName());
                    retVal = -1;
                } catch (Exception e) {
                    logger.error("File IOException for {} "
                            + path.getFileName());
                    retVal = -1;
                }
            }
            return retVal;
        }
    }
}

Related Tutorials