Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: Java 5 | Size: 4.01 KB | Hits: 36 | Expires: Never
Copy text to clipboard
  1. package jtodo.app;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.io.Writer;
  10. import java.util.Locale;
  11.  
  12. import jtodo.ui.DefaultUITodoEntry;
  13. import jtodo.ui.Severity;
  14.  
  15. /**
  16.  * Contains the methods loadfile and savefile to load and save Todo entries in
  17.  * and from a file.
  18.  */
  19. public class TodoEntryIO {
  20.  
  21.         /**
  22.          * Loads to-do entries from the file that is specified by the file name
  23.          * given. Reading the file line for line, splitting the lines up and saving
  24.          * the splits in an array, after that creates a new object of the type
  25.          * "DefaultTodoEntry with the saved informations.
  26.          *
  27.          * @param fileName
  28.          *            To load to-do entries from
  29.          * @throws IOException
  30.          *             if loading the file fails.
  31.          */
  32.         public static void loadfile(String fileName) throws ApplicationException {
  33.                 System.out.println("load: NIY");
  34.  
  35.                 try {
  36.                         BufferedReader input = new BufferedReader(new FileReader(fileName));
  37.  
  38.                         String zeile = null;
  39.  
  40.                         // reads the single information and saving it into an array as long
  41.                         // as the line isn't empty (end of file)
  42.                         while ((zeile = input.readLine()) != null) {
  43.                                 String[] strings = zeile.split("\t");
  44.  
  45.                                 // englische dateformat "MM'/'d'/'yyyy',' h:mm a"
  46.  
  47.                                 // string[0] ist immer die locale des eintrags.
  48.                                 if (strings[0].contains("en")
  49.                                                 && (Locale.getDefault().toString().contains("de"))) {
  50.  
  51.                                         // strings[1] sollte das datum sein gesplittet mit /
  52.                                         String duedate[] = strings[1].split("/");
  53.  
  54.                                         // duedate[2] sollten die jahre sein + das anhängsel der
  55.                                         // uhrzeit
  56.                                         String datetime[] = duedate[2].split(",");
  57.  
  58.                                         // datetime[1] sollte logischerweise die zeit im format h:mm
  59.                                         // a sein
  60.                                         String time[] = datetime[1].split(":");
  61.  
  62.                                         // time[1] sind die minuten + das am/pm anhängsel
  63.                                         String ampm[] = time[1].split("");
  64.  
  65.                                         // +12 stunden rechnen falls ampm[1] pm ist
  66.                                         if (ampm[1] == "pm") {
  67.                                                 time[0] = time[0] + 12;
  68.                                         }
  69.  
  70.                                         // dd.MM.yyyy, hh:mm sollte rauskommen.
  71.                                         strings[2] = duedate[1] + "." + duedate[0] + "."
  72.                                                         + datetime[0] + "," + time[0] + ":" + ampm[0];
  73.  
  74.                                 }
  75.  
  76.                                 // creating the new entry with the informations saved in the
  77.                                 // array
  78.                                 DefaultUITodoEntry tempentry = new DefaultUITodoEntry(
  79.                                                 strings[1], strings[2], Severity.valueOf(strings[3]));
  80.                                 Application.instance.validateEntry(tempentry);
  81.                                 Application.instance.liste.add(tempentry);
  82.  
  83.                         }
  84.                 } catch (IOException exp) {
  85.                         throw new ApplicationException("Error loading file!");
  86.                 }
  87.  
  88.         }
  89.  
  90.         /**
  91.          * Saves to-do entries to the file that is specified by the file name given.
  92.          * Writes for each entry, the single informations in a line and splits them
  93.          * with a tab
  94.          *
  95.          * @param fileName
  96.          *            To save to-do entries in
  97.          * @throws IOException
  98.          *             if creating the file fails.
  99.          */
  100.         public static void savefile(String fileName) throws ApplicationException {
  101.  
  102.                 PrintWriter pw = null;
  103.                 Locale log = Locale.getDefault();
  104.                 try {
  105.                         Writer fw = null;
  106.  
  107.                         // specifies the filename and adding the ending .cvs if the file
  108.                         // doesn't have it
  109.                         if (fileName.contains(".cvs")) {
  110.                                 fw = new BufferedWriter(new FileWriter(fileName));
  111.                         } else {
  112.                                 fw = new BufferedWriter(new FileWriter(fileName + ".cvs"));
  113.                         }
  114.                         pw = new PrintWriter(fw);
  115.  
  116.                         // writing the single informations of each entry in a single line
  117.                         for (int i = 0; i < Application.instance.liste.size(); i++) {
  118.                                 pw.print(log.toString());
  119.                                 pw.print("\t");
  120.                                 pw.print(Application.instance.liste.get(i).getDueDate());
  121.                                 pw.print("\t");
  122.                                 pw.print(Application.instance.liste.get(i).getDescription());
  123.                                 pw.print("\t");
  124.                                 pw.println(Application.instance.liste.get(i).getSeverity()
  125.                                                 .toString());
  126.                         }
  127.  
  128.                 } catch (IOException e) {
  129.                         System.err.println("Error creating file!");
  130.                 } finally {
  131.                         if (pw != null) {
  132.                                 pw.close();
  133.                         }
  134.                 }
  135.         }
  136. }