Example usage for java.io FileReader close

List of usage examples for java.io FileReader close

Introduction

In this page you can find the example usage for java.io FileReader close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:org.alfresco.bm.file.AbstractTestFileService.java

/**
 * Looks for a {@link #PROPERTIES_FILE properties file} containing the name of the fileset that
 * this server uses.  The fileset is therefore unique to every local data location.
 */// ww  w .j a v  a  2s  . c  om
private static synchronized String getFileset(File mirrorDir) {
    Properties properties = new Properties();
    // See if there is a file with the properties present
    File propsFile = new File(mirrorDir, PROPERTIES_FILE);
    if (propsFile.exists()) {
        if (propsFile.isDirectory()) {
            throw new RuntimeException(
                    "Expected to find a properties file but found a directory: " + propsFile);
        }
        // Just read the server's unique key from it
        properties = new Properties();
        FileReader reader = null;
        try {
            reader = new FileReader(propsFile);
            properties.load(reader);
        } catch (IOException e) {
            throw new RuntimeException("Failed to load properties from file: " + propsFile, e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }

    // Read the property
    String fileset = properties.getProperty(PROPERTY_FILESET);
    if (fileset == null) {
        // We must write a value into the file
        fileset = UUID.randomUUID().toString();
        properties.put(PROPERTY_FILESET, fileset);
        // Write the properties back
        FileWriter writer = null;
        try {
            writer = new FileWriter(propsFile);
            properties.store(writer, "Auto-generated fileset name");
        } catch (IOException e) {
            throw new RuntimeException("Failed to write fileset to properties file: " + propsFile, e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
    }
    // Done
    return fileset;
}

From source file:org.cvrgrid.hl7.fileparse.PicuDataLoader.java

private static ArrayList<String> getProcessedFiles(File processedFileRecord, ArrayList<String> processedFiles) {
    try {/*ww w .j a v a  2s  . c  o m*/
        FileReader fr = new FileReader(processedFileRecord);
        BufferedReader br = new BufferedReader(fr);
        String filePath;
        while ((filePath = br.readLine()) != null) {
            processedFiles.add(filePath);
        }
        br.close();
        fr.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return processedFiles;
}

From source file:Main.java

public static String getDeviceInfo(Context context) {
    try {//  ww  w  . j a v a  2  s.  com
        org.json.JSONObject json = new org.json.JSONObject();
        android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String device_id = null;
        if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
            device_id = tm.getDeviceId();
        }
        String mac = null;
        FileReader fstream = null;
        try {
            fstream = new FileReader("/sys/class/net/wlan0/address");
        } catch (FileNotFoundException e) {
            fstream = new FileReader("/sys/class/net/eth0/address");
        }
        BufferedReader in = null;
        if (fstream != null) {
            try {
                in = new BufferedReader(fstream, 1024);
                mac = in.readLine();
            } catch (IOException e) {
            } finally {
                if (fstream != null) {
                    try {
                        fstream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        json.put("mac", mac);
        if (TextUtils.isEmpty(device_id)) {
            device_id = mac;
        }
        if (TextUtils.isEmpty(device_id)) {
            device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
        }
        json.put("device_id", device_id);
        return json.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:ac.ucy.cs.spdx.license.License.java

/**
 * Loads all the licenses that are saved in the standard directory into
 * License objects.//from  ww w.j  a  v  a2  s  .  co  m
 * 
 */
public static void loadLicenses() {
    File licensesText = new File("licensesText/");
    if (licensesText.isDirectory()) {
        File[] files = licensesText.listFiles();
        for (File f : files) {
            FileReader fr;
            try {
                fr = new FileReader(f);
                BufferedReader br = new BufferedReader(fr);
                String lName = br.readLine();
                String lIdentifier = br.readLine();
                if (licenseExists(lIdentifier))
                    continue;
                new License(lName, lIdentifier, f);
                br.close();
                fr.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.apache.hcatalog.cli.HCatCli.java

private static int processFile(String fileName) throws IOException {
    FileReader fileReader = null;
    BufferedReader reader = null;
    try {//from w ww.j a v a2  s  . com
        fileReader = new FileReader(fileName);
        reader = new BufferedReader(fileReader);
        String line;
        StringBuilder qsb = new StringBuilder();

        while ((line = reader.readLine()) != null) {
            qsb.append(line + "\n");
        }

        return (processLine(qsb.toString()));
    } finally {
        if (fileReader != null) {
            fileReader.close();
        }
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.sec.ose.osi.ui.dialog.about.JPanAbout.java

public static String getLicenseText() {
    StringBuffer opensourceAnnouncement = new StringBuffer(Version.getApplicationVersionInfo() + "\n");
    FileReader fReader = null;
    BufferedReader bReader = null;
    try {/*from  ww  w. j  a  v  a 2s .c  o m*/
        fReader = new FileReader(new File(LICENSE_FILE));
        bReader = new BufferedReader(fReader);

        String tempOpensourceAnnouncement = null;
        while ((tempOpensourceAnnouncement = bReader.readLine()) != null) {
            opensourceAnnouncement.append(tempOpensourceAnnouncement + "\n");
        }
    } catch (FileNotFoundException e) {
        log.warn(e);
    } catch (IOException e) {
        log.warn(e);
    } finally {
        if (fReader != null) {
            try {
                fReader.close();
            } catch (IOException e) {
                log.warn(e);
            } finally {
                fReader = null;
            }
        }
        if (bReader != null) {
            try {
                bReader.close();
            } catch (IOException e) {
                log.warn(e);
            } finally {
                bReader = null;
            }
        }
    }

    return opensourceAnnouncement.toString();
}

From source file:org.globus.workspace.client_core.utils.FileUtils.java

public static String readFileAsString(String path) throws IOException {

    final File file = new File(path);

    if (!file.exists()) {
        String err = "File does not exist, path: '" + path + "'";
        if (!file.isAbsolute()) {
            err += " (absolute path: '" + file.getAbsolutePath() + "')";
        }//from w  w w.j  a v  a 2s .c  o  m
        throw new IOException(err);
    }

    if (!file.canRead()) {
        String err = "Cannot read file: \"" + path + "\"";
        if (!file.isAbsolute()) {
            err += " (absolute path: \"" + file.getAbsolutePath() + "\")";
        }
        throw new IOException(err);
    }

    final StringBuffer sb = new StringBuffer(1024);

    BufferedReader in = null;
    FileReader fr = null;
    String s;
    try {
        fr = new FileReader(file);
        in = new BufferedReader(fr);
        s = in.readLine();
        while (s != null) {
            sb.append(s);
            sb.append("\n");
            s = in.readLine();
        }
    } finally {
        if (fr != null) {
            fr.close();
        }
        if (in != null) {
            in.close();
        }
    }
    return sb.toString();
}

From source file:nl.utwente.trafficanalyzer.GeoTagger.java

public static List readCsvFile(File fileName) {

    FileReader fileReader = null;
    CSVParser csvFileParser = null;//from w w w . j  ava 2  s  .c  o  m

    //Create the CSVFormat object with the header mapping
    CSVFormat csvFileFormat = CSVFormat.DEFAULT;

    try {

        //initialize FileReader object
        fileReader = new FileReader(fileName);

        //initialize CSVParser object
        csvFileParser = new CSVParser(fileReader, csvFileFormat);

        //Get a list of CSV file records
        List csvRecords = csvFileParser.getRecords();

        return csvRecords;
    } catch (Exception e) {
        System.out.println("Error in CsvFileReader !!!");
        e.printStackTrace();

    } finally {
        try {
            fileReader.close();
            csvFileParser.close();
        } catch (IOException e) {
            System.out.println("Error while closing fileReader/csvFileParser !!!");
            e.printStackTrace();
        }
    }
    return null;

}

From source file:com.oneis.common.utils.SSLCertificates.java

private static byte[] readPEMBytes(String filename) throws java.io.IOException {
    FileReader fileReader = new FileReader(filename);
    BufferedReader reader = new BufferedReader(fileReader);
    String line = reader.readLine();
    if (line == null && !line.startsWith("-----BEGIN ")) {
        throw new RuntimeException("Doesn't look like a PEM file: " + filename);
    }/*from w w w.ja va 2s  .  c om*/
    StringBuffer buffer = new StringBuffer();
    while ((line = reader.readLine()) != null && !line.startsWith("-----END ")) {
        buffer.append(line.trim());
    }
    if (line == null) {
        throw new RuntimeException("End marker not found in PEM file: " + filename);
    }
    reader.close();
    fileReader.close();
    return Base64.decode(buffer.toString());
}

From source file:Main.java

/**
 * Reads data from specified file./*w  w w .j a  v  a2  s  . c  o  m*/
 * @param path Path to file.
 * @return 2-dimensional array of data.
 */
public static ArrayList<ArrayList<String>> loadFile(String path) {
    FileReader fr = null;
    String line = "";
    ArrayList<ArrayList<String>> dataArray = new ArrayList<>();

    try {
        fr = new FileReader(path);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
    }

    BufferedReader br = new BufferedReader(fr);
    ArrayList<String> tokens = new ArrayList<>();

    try {
        while ((line = br.readLine()) != null) {
            tokens = parseLine(line);
            dataArray.add(tokens);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        fr.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return dataArray;
}