Advertisement
brilliant_moves

CountUpdater.java

Dec 14th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.54 KB | None | 0 0
  1. import java.util.Scanner;   // for user input
  2. import java.io.*;       // for File handling
  3.  
  4. public class CountUpdater {
  5.  
  6.     /**
  7.     *   Program:    CountUpdater.java
  8.     *   Purpose:    First run: save data.
  9.     *           Subsequent runs: recall data from disk; update; save new data.
  10.     *   Creator:    Chris Clarke
  11.     *   Created:    14.12.2013
  12.     */
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.         // where to save your text file
  17.         String filename = "file.txt";
  18.         File f = new File(filename);
  19.         String strTimesMade="";
  20.         int count = 0;
  21.         if ( !f.exists()) {
  22.             saveText( filename, "1");       // create new text file
  23.         } else {
  24.             strTimesMade = getText( filename);      // recall text
  25.             count = Integer.parseInt( strTimesMade);    // convert to int
  26.         } // end if
  27.  
  28.         System.out.println("count = "+ ++count);
  29.  
  30.         strTimesMade = "" +count;           // convert to String
  31.         saveText( filename, strTimesMade);      // save new text
  32.     } // end main()
  33.  
  34.    public static void saveText(String filename, String text) {
  35.         Writer writer = null;
  36.  
  37.         try {
  38.             File file = new File(filename);
  39.             writer = new BufferedWriter(new FileWriter(file));
  40.             writer.write(text);
  41.         } catch (FileNotFoundException e) {
  42.             e.printStackTrace();
  43.         } catch (IOException e) {
  44.             e.printStackTrace();
  45.         } finally {
  46.             try {
  47.                 if (writer != null) {
  48.                     writer.close();
  49.                 }
  50.             } catch (IOException e) {
  51.                 e.printStackTrace();
  52.             }
  53.         }
  54.  
  55.     } // end saveText()
  56.  
  57.     public static String getText(String filename) {
  58.         File file = new File(filename);
  59.         StringBuffer contents = new StringBuffer();
  60.         BufferedReader reader = null;
  61.  
  62.         try {
  63.             reader = new BufferedReader(new FileReader(file));
  64.             String text = null;
  65.  
  66.             // repeat until all lines are read
  67.             while ((text = reader.readLine()) != null) {
  68.                 contents.append(text);
  69.             }
  70.         } catch (FileNotFoundException e) {
  71.             e.printStackTrace();
  72.         } catch (IOException e) {
  73.             e.printStackTrace();
  74.         } finally {
  75.             try {
  76.                 if (reader != null) {
  77.                     reader.close();
  78.                 }
  79.             } catch (IOException e) {
  80.                 e.printStackTrace();
  81.             }
  82.         }
  83.  
  84.         // show file contents here
  85.         //System.out.println(contents.toString());
  86.  
  87.     return contents.toString();
  88.  
  89.     } // end recallText()
  90.  
  91. } // end CountUpdater
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement