Example usage for java.io FileReader FileReader

List of usage examples for java.io FileReader FileReader

Introduction

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

Prototype

public FileReader(FileDescriptor fd) 

Source Link

Document

Creates a new FileReader , given the FileDescriptor to read, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:Main.java

public static String read(File src) throws IOException {
    long length = src.length();

    // Probably not the best way to do it, but I'm lazy.
    char[] chars = new char[(int) length];
    FileReader fileReader = new FileReader(src);
    fileReader.read(chars);// w  w w . ja  va2s.co  m
    fileReader.close();
    return new String(chars);
}

From source file:Main.java

public static String readFileAsStr(File file) throws IOException {
    StringBuffer sb = new StringBuffer();
    if (file == null)
        return "";
    if (!file.exists())
        return "";

    LineNumberReader lnr = null;//  w w w. java2s . c  om
    try {
        lnr = new LineNumberReader(new FileReader(file));

        String s = lnr.readLine();
        while (s != null) {
            sb.append(s);
            s = lnr.readLine();
        }
    } catch (java.io.IOException ioe) {
        throw ioe;
    } finally {
        if (lnr != null) {
            lnr.close();
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Reads the first line of text from the given file
 *//*from ww w  .j av  a2s.co  m*/
public static String readOneLine(String fileName) {
    String line = null;
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(fileName), 512);
        line = reader.readLine();
    } catch (IOException e) {
        Log.e(TAG, "Could not read from file " + fileName, e);
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            // ignored, not much we can do anyway
        }
    }

    return line;
}

From source file:Main.java

public static String getDeviceInfo(Context context) {
    try {/*  www  .  j  a v a2s . c om*/
        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:Main.java

public static String ReadAllText(String path) throws IOException {
    String returnValue = "";

    BufferedReader reader = null;

    try {/*from w w w  . jav a2 s .c  o  m*/
        FileReader file = new FileReader(path);
        reader = new BufferedReader(file);
        String line = "";
        while ((line = reader.readLine()) != null) {
            returnValue += line + "\n";
        }
    } finally {
        if (reader != null)
            reader.close();
    }

    return returnValue;
}

From source file:br.com.ceosites.cachedproperties.cache.test.util.DatabaseUtils.java

public static DataSource prepareDatabase() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:dataSource?hsqldb.sqllog=3");
    dataSource.setUsername("SA");
    dataSource.setPassword("");
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    ScriptRunner scriptRunner = new ScriptRunner(dataSource);

    try {/*  ww w  .j  a  v a2 s .c  om*/
        scriptRunner.execute(new BufferedReader(new FileReader("src/main/resources/db/create-db.sql")));
        scriptRunner.execute(new BufferedReader(new FileReader("src/main/resources/db/insert-data.sql")));
    } catch (IOException ex) {
        LOGGER.error("Error on read SQL files.", ex);
    } catch (SQLException ex) {
        LOGGER.error("Error on  execute SQL script.", ex);
    }

    return dataSource;
}

From source file:TraverseDirectory.java

public static void traveseFile(String file) throws FileNotFoundException, IOException {
    BufferedReader buffer = new BufferedReader(new FileReader(file));
    Pattern p = Pattern.compile("^[7-9]\\d{9}");
    String read;//  w  ww  . java 2s. c  o m
    while ((read = buffer.readLine()) != null) {
        Matcher r = p.matcher(read);
        if (r.matches()) {
            System.out.println(read);
        }
    }

}

From source file:Main.java

public static double[] read_array(String filename) throws Exception {
    FileReader f = new FileReader(filename);
    BufferedReader inp = new BufferedReader(f);

    String str = inp.readLine();/*  w  w w  . ja v  a 2  s .  co  m*/
    int nl = 0;
    while (str != null) {
        nl++;
        str = inp.readLine();
    }

    inp.close();
    f.close();

    double[] res = new double[nl];
    f = new FileReader(filename);
    inp = new BufferedReader(f);
    for (int i = 0; i < nl; i++) {
        str = inp.readLine();
        res[i] = Double.valueOf(str).doubleValue();
    }

    inp.close();
    f.close();

    return res;
}

From source file:com.brouwersystems.testing.SqlBatchManager.java

public static List<String> loadSqlCommands(String commandFileName) {
    if (commandFileName == null) {
        LOG.error("Missing filename");
        throw new IllegalArgumentException("Missing filename");
    }// ww  w . ja  v a 2  s  .  c o  m

    List<String> sqlCommands = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(commandFileName))) {
        sqlCommands = new ArrayList<>();
        String sql = br.readLine();
        while (sql != null) {
            if (StringUtils.isNotEmpty(sql)) {
                sqlCommands.add(sql);
            }
            sql = br.readLine();
        }
    } catch (FileNotFoundException ex) {
        LOG.error("File " + commandFileName + " not found", ex);
    } catch (IOException ex) {
        LOG.error("IO error accessing file " + commandFileName, ex);
    }

    for (String sql : sqlCommands) {
        System.out.println("SQL: " + sql);
    }
    return sqlCommands;
}

From source file:br.edimarmanica.weir2.rule.Loader.java

/**
 *
 * @param rule/*from   w w  w  . j  a  v  a2  s.  com*/
 * @param formatted valores formatados como na avaliao?
 * @return Map<PageId, Value>
 *
 */
public static Map<String, String> loadPageValues(File rule, boolean formatted) {
    Map<String, String> pageValues = new HashMap<>();

    try (Reader in = new FileReader(rule.getAbsolutePath())) {
        try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) {
            for (CSVRecord record : parser) { //para cada value
                String url = formatURL(record.get("URL"));
                String value = record.get("EXTRACTED VALUE");

                if (formatted) {
                    value = Formatter.formatValue(value);
                }

                if (!value.trim().isEmpty()) {
                    pageValues.put(url, value);
                }
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return pageValues;
}