Created
September 7, 2012 18:48
-
-
Save noahlz/3668527 to your computer and use it in GitHub Desktop.
Answer to a StackOverflow problem. 1000 rep points or bust!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.concurrent.atomic.AtomicInteger; | |
/** | |
* http://stackoverflow.com/q/12321837/7507 | |
*/ | |
public class GetLatestBuildNumber { | |
private static final AtomicInteger number = new AtomicInteger(); | |
private static final String FILE_NAME = "latest_build_number.txt"; | |
public static void main(String[] args) { | |
int latest = fetchLatestBuildNumber(); | |
writeLatestBuildNumber(latest); | |
int next = fetchLatestBuildNumber(); | |
int previous; | |
try { | |
previous = readLatestBuildNumber(); | |
System.out.println("previous build number " + previous); | |
System.out.println("current build number " + next); | |
} catch (IOException ioe) { | |
System.out.println("failed to read last build number " + | |
"from file " + FILE_NAME + ": " + ioe); | |
} catch (NumberFormatException nfx) { | |
System.out.println("failed to parse last build number: " + nfx); | |
} | |
} | |
private static int fetchLatestBuildNumber() { | |
return number.getAndIncrement(); | |
} | |
private static void writeLatestBuildNumber(int number) { | |
int latestBuildNumber = fetchLatestBuildNumber(); | |
FileWriter writer = null; | |
try { | |
writer = new FileWriter(FILE_NAME); | |
writer.write(Integer.toString(latestBuildNumber)); | |
} catch (IOException ex) { | |
System.out.println("failed to write build number " | |
+ number + ", error: " + ex); | |
} finally { | |
try { | |
if (writer != null) writer.close(); | |
} catch (IOException e) { /* we tried... */ } | |
} | |
} | |
private static int readLatestBuildNumber() throws IOException { | |
BufferedReader reader = null; | |
try { | |
reader = new BufferedReader(new FileReader(FILE_NAME)); | |
String number = reader.readLine(); | |
return Integer.parseInt(number); | |
} finally { | |
try { | |
if (reader != null) reader.close(); | |
} catch (IOException e) { /* we tried. */ } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment