Created
April 19, 2013 00:44
-
-
Save Zeklandia/5417345 to your computer and use it in GitHub Desktop.
Java check-for-update function.
This file contains hidden or 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.awt.*; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.nio.file.FileSystems; | |
import java.nio.file.Files; | |
import javax.swing.*; | |
public class UpdateCheck | |
{ | |
public static void main (String[] args) throws ClassNotFoundException | |
{ | |
String userHome = System.getProperty("user.home"); | |
String VersionFileLink = "Link here"; | |
String DownloadFileLink = "Link here"; | |
try | |
{ long startTime = System.currentTimeMillis(); | |
System.out.println("Checking for Update..."); | |
URL url = new URL(VersionFileLink); | |
url.openConnection(); | |
InputStream reader = url.openStream(); | |
FileOutputStream writer = new FileOutputStream(userHome + "/Downloads/.version"); | |
byte[] buffer = new byte[153600]; | |
int totalBytesRead = 0; | |
int bytesRead = 0; | |
while ((bytesRead = reader.read(buffer)) > 0) | |
{ | |
writer.write(buffer, 0, bytesRead); | |
buffer = new byte[153600]; | |
totalBytesRead += bytesRead; | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println("Update Check Complete.\nSaved to " | |
+ userHome | |
+ "/Downloads/.version\n" | |
+ (new Integer(totalBytesRead).toString()) | |
+ " bytes downloaded:\n (" | |
+ (new Long(endTime - startTime).toString()) | |
+ " millseconds).\n"); | |
writer.close(); | |
File updateFileSource = new File(userHome + "/Downloads/.version"); | |
String updateFile = new String(Files.readAllBytes(FileSystems.getDefault().getPath(userHome + "/Downloads/.version"))); | |
int version = 0; | |
int update = (int) Double.parseDouble(updateFile); | |
if (version == update){ | |
System.out.println("This version is up to date!"); | |
} | |
else if(version < update){ | |
System.out.println("An update is available!"); | |
JOptionPane.showMessageDialog(frameMain, "An Update is Available!"); | |
} | |
else if(version > update){ | |
System.out.println("Back... TO THE FUTURE!!!!"); | |
JOptionPane.showMessageDialog(frameMain, "ಠ_ಠ\nDon't edit that file!"); | |
} | |
reader.close(); | |
updateFileSource.delete(); | |
System.out.println("Version code file deleted."); | |
} | |
catch (MalformedURLException t) | |
{ | |
System.err.println("What happened? I think there was a disk error!"); | |
} | |
catch (IOException r) | |
{ | |
System.err.println("Great Scott! An error!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment