コード例 #1
0
  public void saveNews(Context context) {
    Log.d(
        "Unit5Calendar",
        "Saving News.. We need to not let the user exterminate the app while saving.."); // TODO:
                                                                                         // figure
                                                                                         // out how
                                                                                         // to not
                                                                                         // let the
                                                                                         // app be
                                                                                         // closed
                                                                                         // while
                                                                                         // saving.
    try {
      if (newsFile == null) newsFile = new File(context.getFilesDir(), NEWS_FILE_NAME);
      if (!newsFile.exists()) newsFile.createNewFile();

      BufferedWriter writer =
          new BufferedWriter(
              new OutputStreamWriter(context.openFileOutput(NEWS_FILE_NAME, Context.MODE_PRIVATE)));

      Collections.sort(newsArticles, Utils.articlePubDateSorter);
      writer.write("size-" + newsArticles.size());
      Log.d("NewsSave", "size-" + newsArticles.size());
      writer.newLine();
      for (Article article : newsArticles) {
        writer.write(START_ARTICLE);
        writer.newLine();
        if (article.hasPubDate()) {
          writer.write("pubDate:" + article.getPubDate());
          Log.d("NewsSave", "pubDate:" + article.getPubDate());
          writer.newLine();
          writer.write(NEXT);
          Log.d("NewsSave", NEXT);
          writer.newLine();
        }
        writer.write("title:" + article.getTitle());
        Log.d("NewsSave", "title:" + article.getTitle());
        writer.newLine();
        writer.write(NEXT);
        Log.d("NewsSave", NEXT);
        writer.newLine();
        writer.write("desc:" + article.getDescription());
        Log.d("NewsSave", "desc:" + article.getDescription());
        writer.newLine();
        writer.write(NEXT);
        Log.d("NewsSave", NEXT);
        writer.newLine();
      }
      writer.write(END_ARTICLES);
      Log.d("NewsSave", END_ARTICLES);
    } catch (IOException e) {
      Log.d("Unit5Calendar", e.getMessage(), e);
    }
    Log.d("Unit5Calendar", "Done saving News, user can now safely exterminate app.");
  }
コード例 #2
0
 private Article readArticle(BufferedReader reader) throws IOException {
   Article article = new Article();
   String currentLine;
   while ((currentLine = reader.readLine()) != null
       && !currentLine.equals(END_ARTICLES)
       && !currentLine.equals(START_ARTICLE)) {
     int firstCol =
         currentLine.indexOf(':') + 1; // we want the text after the first colon, so we add 1.
     if (currentLine.startsWith("pubDate")) {
       article.setPubDate(readPubDate(currentLine, reader, firstCol));
     } else if (currentLine.startsWith("title")) {
       article.setTitle(readTitle(currentLine, reader, firstCol));
     } else if (currentLine.startsWith("desc")) {
       article.setDescription(readDescription(currentLine, reader, firstCol));
     }
   }
   return article;
 }