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

private static void readFile(String fileName) throws IOException {
    String line;/*w w w.j  a va2s  .  c  o  m*/
    FileReader fr = null;
    BufferedReader br = null;
    try {
        fr = new FileReader(fileName);
        br = new BufferedReader(fr);
        while ((line = br.readLine()) != null) {

            if (line.isEmpty() || line.startsWith("%"))
                continue;
            if (fileName.equals(INTENT_GET_EXTRAS_METHODS)) {
                intentExtrasMethods.add(line);
            } else if (fileName.equals(API_MISUSE_METHODS)) {
                APIMisuseMethods.add(line);
            } else if (fileName.equals(ANDROID_CLASSES)) {
                androidClassPrefixes.add(line);
            } else if (fileName.equals(API_SINK_METHODS)) {
                APISinkMethods.add(line);
            }
        }

    } finally {
        if (br != null)
            br.close();
        if (fr != null)
            fr.close();
    }
}

From source file:Main.java

public static String readFileByLines(@NonNull File file) {
    if (!file.isFile()) {
        return "";
    }/*from w  ww  . j  a v a  2s. com*/
    BufferedReader reader = null;
    StringBuilder builder = new StringBuilder();
    try {
        reader = new BufferedReader(new FileReader(file));
        String tempString;
        while ((tempString = reader.readLine()) != null) {
            builder.append(tempString);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
    } finally {
        CloseableClose(reader);
    }

    return builder.toString();
}

From source file:naftoreiclag.villagefive.SaveLoad.java

public static World load(Node rootNode, AssetManager assetManager) throws IOException, ParseException {
    World world = new World(rootNode, assetManager);

    JSONParser parser = new JSONParser();

    File file = new File("saves/save.json");
    FileReader fr = new FileReader(file);
    JSONObject root = (JSONObject) parser.parse(fr);
    JSONObject worldj = (JSONObject) root.get("world");

    System.out.println(worldj);//from  www .ja  v  a  2 s  . co m

    world.spawnFromJson(worldj);

    return world;
}

From source file:de.nava.informa.utils.FileUtils.java

public static boolean compare(File fileExpected, File fileActual) throws IOException {

    BufferedReader readExpected;/*from   w  w w . jav a2 s . c o m*/
    try {
        logger.debug("Comparing golden file " + fileExpected + " to " + fileActual);
        readExpected = new BufferedReader(new FileReader(fileExpected));
    } catch (IOException e) {
        logger.error("Could not read baseline: " + e);
        return false;
    }
    BufferedReader readActual = new BufferedReader(new FileReader(fileActual));
    return compare(readExpected, readActual);
}

From source file:gov.nih.nci.caintegrator.application.arraydata.GeneLocationFileLoader.java

static void loadFile(GeneLocationConfiguration geneLocationConfiguration, File geneLocationFile)
        throws ValidationException, IOException {
    CSVReader geneFileReader = new CSVReader(new FileReader(geneLocationFile), DELIMITER);
    try {//from   w  w w .  j  ava 2  s. com
        loadFile(geneLocationConfiguration, geneFileReader);
    } finally {
        IOUtils.closeQuietly(geneFileReader);
    }
}

From source file:atlantis.engine.graphics3d.importer.babylonjs.BabylonImporter.java

public static Mesh[] loadBabyonScene(String filename) {
    Mesh[] mesh = null;/*from  w w  w .j  a  v  a 2s.  c o  m*/
    BufferedReader reader = null;
    StringBuilder jsonString = new StringBuilder();

    try {
        reader = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = reader.readLine()) != null) {
            jsonString.append(line);
        }
        reader.close();

        String[] temp = filename.split("/");
        StringBuilder builder = new StringBuilder();
        for (int i = 0, l = temp.length - 1; i < l; i++) {
            builder.append(temp[i]);
            builder.append("/");
        }

        return getMeshes(createBabylonScene(jsonString.toString(), builder.toString()));

    } catch (IOException e) {
        e.printStackTrace();
    }

    return mesh;
}

From source file:eu.smartfp7.foursquare.AttendanceCrawler.java

/**
 * The main takes an undefined number of cities as arguments, then initializes
 * the specific crawling of all the trending venues of these cities.
 * The trending venues must have been previously identified using the `DownloadPages`
 * program.//w  w  w . j  a va  2s .  c  o  m
 * 
 * Current valid cities are: london, amsterdam, goldcoast, sanfrancisco.
 * 
 */
public static void main(String[] args) throws Exception {
    Settings settings = Settings.getInstance();
    String folder = settings.getFolder();

    // We keep info and error logs, so that we know what happened in case
    // of incoherence in the time series.
    Map<String, FileWriter> info_logs = new HashMap<String, FileWriter>();
    Map<String, FileWriter> error_logs = new HashMap<String, FileWriter>();

    // For each city we monitor, we store the venue IDs that we got from
    // a previous crawl.
    Map<String, Collection<String>> city_venues = new HashMap<String, Collection<String>>();

    // Contains the epoch time when the last API call has been made for each 
    // venue. Ensures that we get data only once each hour. 
    Map<String, Long> venue_last_call = new HashMap<String, Long>();

    // Contains the epoch time when we last checked if time series were broken
    // for each city.
    // We do these checks once every day before the batch forecasting begins.
    Map<String, Long> sanity_checks = new HashMap<String, Long>();

    // We also keep in memory the number of checkins for the last hour for
    // each venue.
    Map<String, Integer> venue_last_checkin = new HashMap<String, Integer>();

    Map<Long, Integer> APICallsCount = new HashMap<Long, Integer>();

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    int total_venues = 0;
    long total_calls = 0;
    long time_spent_on_API = 0;

    for (String c : args) {
        settings.checkFileHierarchy(c);

        city_venues.put(c, loadVenues(c));
        total_venues += city_venues.get(c).size();

        info_logs.put(c,
                new FileWriter(folder + c + File.separator + "log" + File.separator + "info.log", true));
        error_logs.put(c,
                new FileWriter(folder + c + File.separator + "log" + File.separator + "error.log", true));

        Calendar cal = Calendar.getInstance();

        info_logs.get(c).write("[" + df.format(cal.getTime()) + "] Crawler initialization for " + c + ". "
                + city_venues.get(c).size() + " venues loaded.\n");
        info_logs.get(c).flush();

        // If we interrupted the program for some reason, we can get back
        // the in-memory data.
        // Important: the program must not be interrupted for more than one
        // hour, or we will lose time series data.
        for (String venue_id : city_venues.get(c)) {
            String ts_file = folder + c + File.separator + "attendances_crawl" + File.separator + venue_id
                    + ".ts";

            if (new File(ts_file).exists()) {
                BufferedReader buffer = new BufferedReader(new FileReader(ts_file));
                String mem = null, line = null;
                for (; (line = buffer.readLine()) != null; mem = line)
                    ;
                buffer.close();

                if (mem == null)
                    continue;

                String[] tmp = mem.split(",");
                venue_last_call.put(venue_id, df.parse(tmp[0]).getTime());
                venue_last_checkin.put(venue_id, Integer.parseInt(tmp[3]));

                VenueUtil.fixBrokenTimeSeriesVenue(new File(ts_file));
            } // if
        } // for

        sanity_checks.put(c, cal.getTimeInMillis());
    } // for

    if (total_venues > 5000) {
        System.out.println(
                "Too much venues for a single API account (max 5000).\nPlease create a new Foursquare API account and use these credentials.\nExiting now.");
        return;
    }

    while (true) {

        for (String c : args) {
            // We create a FIFO queue and pop venue IDs one at a time.
            LinkedList<String> city_venues_buffer = new LinkedList<String>(city_venues.get(c));
            String venue_id = null;

            // Artificial wait to avoid processors looping at 100% of their capacity
            // when there is no more venues to crawl for the current hour.
            Thread.sleep(3000);

            while ((venue_id = city_venues_buffer.pollFirst()) != null) {
                // We get the current time according to the city's time zone
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MILLISECOND,
                        TimeZone.getTimeZone(settings.getCityTimezone(c)).getOffset(cal.getTime().getTime())
                                - Calendar.getInstance().getTimeZone().getOffset(cal.getTime().getTime()));
                //TimeZone.getTimeZone("Europe/London").getOffset(cal.getTime().getTime()));

                long current_time = DateUtils.truncate(cal.getTime(), Calendar.HOUR).getTime();

                // We query Foursquare only once per hour per venue.
                if (venue_last_call.get(venue_id) != null
                        && current_time < venue_last_call.get(venue_id) + 3600000)
                    continue;

                intelligentWait(total_venues, cal.getTime().getTime(),
                        (total_calls == 0 ? 0 : Math.round(time_spent_on_API / total_calls)));

                Venue venue = null;

                try {
                    long beforeCall = System.currentTimeMillis();
                    venue = new Venue(getFoursquareVenueById(venue_id, c));

                    // If there is no last call, this is the beginning of the time series
                    // for this venue. We get the number of people "here now" to initialize
                    // the series.
                    if (venue_last_call.get(venue_id) == null) {
                        /** TODO: by doing this, we keep a representation of the venue dating from the beginning
                         *       of the specific crawl. we might want to change this and update this file once
                         *      in a while.
                         */
                        FileWriter info = new FileWriter(folder + c + File.separator + "foursquare_venues"
                                + File.separator + venue_id + ".info");
                        info.write(venue.getFoursquareJson());
                        info.close();

                        FileWriter out = new FileWriter(folder + c + File.separator + "attendances_crawl"
                                + File.separator + venue_id + ".ts");
                        out.write("Date,here_now,hour_checkins,total_checkins\n");
                        out.write(df.format(current_time) + "," + venue.getHereNow() + "," + venue.getHereNow()
                                + "," + venue.getCheckincount() + "\n");
                        out.close();
                    } else {
                        FileWriter out = new FileWriter(folder + c + File.separator + "attendances_crawl"
                                + File.separator + venue_id + ".ts", true);
                        int checks = venue.getCheckincount() - venue_last_checkin.get(venue_id);
                        out.write(df.format(current_time) + "," + venue.getHereNow() + ","
                                + Integer.toString(checks) + "," + venue.getCheckincount() + "\n");
                        out.close();
                    }

                    if (APICallsCount.get(current_time) == null)
                        APICallsCount.put(current_time, 1);
                    else
                        APICallsCount.put(current_time, APICallsCount.get(current_time) + 1);

                    total_calls++;

                    venue_last_call.put(venue_id, current_time);
                    venue_last_checkin.put(venue_id, venue.getCheckincount());

                    time_spent_on_API += System.currentTimeMillis() - beforeCall;
                } catch (Exception e) {
                    // If something bad happens (crawler not available, IO error, ...), we put the
                    // venue_id in the FIFO queue so that it gets reevaluated later.
                    //e.printStackTrace();
                    error_logs.get(c)
                            .write("[" + df.format(cal.getTime().getTime()) + "] Error with venue " + venue_id
                                    + " (" + e.getMessage() + "). " + APICallsCount.get(current_time)
                                    + " API calls so far this hour, " + city_venues_buffer.size()
                                    + " venues remaining in the buffer.\n");
                    error_logs.get(c).flush();

                    System.out.println("[" + df.format(cal.getTime().getTime()) + "] " + c + " -- "
                            + APICallsCount.get(current_time) + " API calls // " + city_venues_buffer.size()
                            + " venues remaining " + " (" + e.getMessage() + ")");

                    if (e instanceof FoursquareAPIException)
                        if (((FoursquareAPIException) e).getHttp_code().equals("400")
                                && ((FoursquareAPIException) e).getError_detail()
                                        .equals("Venue " + venue_id + " has been deleted")) {
                            city_venues.get(c).remove(venue_id);
                            removeVenue(venue_id, c);
                        } else
                            city_venues_buffer.add(venue_id);

                    continue;
                }
            } // while

            // Every day between 0am and 2am, we repair all the broken time series (if there
            // is something to repair).
            Calendar cal = Calendar.getInstance();
            if (city_venues_buffer.peekFirst() == null
                    && (cal.getTimeInMillis() - sanity_checks.get(c)) >= 86400000
                    && cal.get(Calendar.HOUR_OF_DAY) < 2) {
                VenueUtil.fixBrokenTimeSeriesCity(c, folder);
                sanity_checks.put(c, cal.getTimeInMillis());
                info_logs.get(c).write("[" + df.format(cal.getTime()) + "] Sanity check OK.\n");
                info_logs.get(c).flush();
            }
        } // for
    } // while
}

From source file:Main.java

/**
 * get total size of ram. Work for all API levels.
 * @return total size in byte//from   w  ww.j  a va 2 s.  c om
 * @deprecated
 */
public static long getTotalRamSize() {
    StringBuilder sb = new StringBuilder();
    //read this file and pick up numbers
    File file = new File("/proc/meminfo");
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(file));
        String readLine = br.readLine();
        char[] charArray = readLine.toCharArray();
        for (char c : charArray) {
            if (c >= '0' && c <= '9') {
                sb.append(c);
            }
        }
        String string = sb.toString();
        long parseLong = Long.parseLong(string);
        return parseLong * 1024;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return 0;
}

From source file:com.vikingbrain.nmt.test.util.MockUtils.java

private static String readFile(String fileName) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    try {//from  www  . j a  va 2 s  . co m
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }
        return sb.toString();
    } finally {
        br.close();
    }
}

From source file:Main.java

public static int countSubstringOccurrence(File file, String substring) throws IOException {
    int count = 0;
    BufferedReader reader = null;
    try {/*  w  ww .j ava  2 s. co m*/

        reader = new BufferedReader(new FileReader(file));
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            count += countSubstringOccurrence(line, substring);
        }

    } finally {
        closeQuietly(reader);
    }
    return count;
}