Esempio n. 1
0
  /**
   * @param context Application context
   * @param directory The name of the directory
   * @param fName The name of the file
   * @param what The string to save
   * @param append True to append string to end of file, false to overwrite
   */
  public static void writeFile(
      Context context, String directory, String fName, String what, boolean append) {

    // This creates the directory if it doesn't exist...
    File dir = context.getDir(directory, Context.MODE_PRIVATE);
    if (dir == null) {
      Logging.debug(TAG, "writeFile:", "Error with " + directory);
      return;
    }

    try {
      File rf = new File(dir, fName);
      Logging.debug(TAG, "writeFile - routeFile = ", rf.getName() + ", " + rf.getAbsolutePath());
      FileOutputStream fOut = new FileOutputStream(rf, append);
      OutputStreamWriter writer = new OutputStreamWriter(fOut, "UTF-8");
      BufferedWriter fbw = new BufferedWriter(writer);
      fbw.write(what);
      fbw.newLine();
      fbw.flush();
      fbw.close();
      writer.close();
      fOut.close();
    } catch (IOException e) {
      Logging.debug(TAG, "writeFile:", e.getMessage());
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }
Esempio n. 2
0
  /**
   * @param context Application context
   * @param directory The name of the directory
   * @param fName The name of the file
   * @return The saved routes if they exist, or an empty string otherwise.
   */
  public static String readFile(Context context, String directory, String fName) {

    BufferedReader in = null;
    File dir = context.getDir(directory, Context.MODE_PRIVATE);
    if (dir == null) {
      Logging.debug(TAG, "readFile:", "Context.getDir() failed");
      return new String();
    }

    File rf = new File(dir, fName);
    if (!rf.exists()) {
      Logging.debug(TAG, "readFile", rf.getAbsolutePath() + "/" + rf.getName() + " does NOT exist");
      return new String();
    }

    StringBuilder sb;

    try {
      InputStream inStream = new FileInputStream(rf);

      InputStreamReader inputreader = new InputStreamReader(inStream, "UTF-8");
      in = new BufferedReader(inputreader);

      String line;
      sb = new StringBuilder();

      line = in.readLine();

      while (line != null) {
        sb.append(line);
        line = in.readLine();
      }

      Logging.debug(TAG, "readFile - routeFile contents = ", sb.toString());

      return sb.toString();
    } catch (IOException e) {
      Logging.debug(TAG, "readFile:", e.getMessage());
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          Logging.debug(TAG, "readFile:", e.getMessage());
        }
      }
    }

    return new String();
  }