Advertisement
brilliant_moves

IcedTea.java

Nov 20th, 2014
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.23 KB | None | 0 0
  1. import java.util.Scanner;   // for user input
  2. import java.io.*;       // for File handling
  3.  
  4. public class IcedTea {
  5.  
  6.     /**
  7.     *   Program:    IcedTea.java
  8.     *   Purpose:    First run: save data.
  9.     *           Subsequent runs: recall data from disk; update; save new data.
  10.     *   Creator:    Chris Clarke
  11.     *   Created:    11.07.2013
  12.     */
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.         // where to save your text file
  17.         File tempDir=new File( File.separator+"temp");
  18.         if (!tempDir.exists()) tempDir.mkdir();         // create new directory
  19.  
  20.         String filename = File.separator+"temp"+File.separator+"IcedTeaTextFile.txt";
  21.         String strTimesMade="";
  22.         int timesMade = 0;
  23.  
  24.         Scanner scan = new Scanner (System.in);
  25.  
  26.         System.out.print("Are you running this program for the first time? ");
  27.         String firstTime = scan.nextLine();
  28.         if (firstTime.toLowerCase().startsWith("y")) {
  29.             saveText( filename, strTimesMade);      // create new text file
  30.         } else {
  31.             strTimesMade = readText( filename);     // read text from file
  32.             timesMade = Integer.parseInt(strTimesMade); // convert to int
  33.         }
  34.  
  35.         System.out.println("Hi! You've made iced tea "+timesMade+" times so far since 05.10.14.");
  36.         System.out.println("Please enter a number to add to the current number\n"
  37.          +"of times you've made iced tea.");
  38.         int number = scan.nextInt();
  39.         timesMade += number;
  40.         System.out.println("Thanks. You've now made iced tea "+timesMade+" times so far since 05.10.14.");
  41.  
  42.         strTimesMade = "" +timesMade;           // convert to String
  43.         saveText( filename, strTimesMade);      // save new text
  44.         System.out.println("Now run this program again.");
  45.     } // end main()
  46.  
  47.    public static void saveText(String filename, String text) {
  48.         Writer writer = null;
  49.  
  50.         try {
  51.             File file = new File(filename);
  52.             writer = new BufferedWriter(new FileWriter(file));
  53.             writer.write(text);
  54.         } catch (FileNotFoundException e) {
  55.             e.printStackTrace();
  56.         } catch (IOException e) {
  57.             e.printStackTrace();
  58.         } finally {
  59.             try {
  60.                 if (writer != null) {
  61.                     writer.close();
  62.                 }
  63.             } catch (IOException e) {
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.  
  68.     } // end saveText()
  69.  
  70.     public static String readText(String filename) {
  71.         File file = new File(filename);
  72.         StringBuffer contents = new StringBuffer();
  73.         BufferedReader reader = null;
  74.  
  75.         try {
  76.             reader = new BufferedReader(new FileReader(file));
  77.             String text = null;
  78.  
  79.             // repeat until all lines are read
  80.             while ((text = reader.readLine()) != null) {
  81.                 contents.append(text);
  82.             }
  83.         } catch (FileNotFoundException e) {
  84.             e.printStackTrace();
  85.         } catch (IOException e) {
  86.             e.printStackTrace();
  87.         } finally {
  88.             try {
  89.                 if (reader != null) {
  90.                     reader.close();
  91.                 }
  92.             } catch (IOException e) {
  93.                 e.printStackTrace();
  94.             }
  95.         }
  96.  
  97.         // show file contents here
  98.         //System.out.println(contents.toString());
  99.  
  100.     return contents.toString();
  101.  
  102.     } // end readText()
  103.  
  104. } // end IcedTea
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement