15 Dec 2007 @ 12:33 PM 
 

Updating a textfile trough the net

 


1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
434 views

Here is a sample code that I wrote some time ago to update a (text)file. It can be used to update an application or whatever you want to update but this version can only update a textfile. I'll release a new version soon that demonstrates how to update a file/application when connected to the internet.

JAVA:
  1. package full_projects;
  2. /*--------------------------------------------------------------------------
  3. //Author List:
  4. //deAppel
  5. //
  6. //Description:
  7. // An example of updating a textfile trough the net.
  8. //
  9. //Environment:
  10. //This software was developed using Eclipse and Java 1.6
  11. //
  12. //Copyright Information:
  13. //Copyright (C) 2007
  14. //        Ark de Appel             www.engineeringserver.com
  15. //
  16. //----------------------------------------------------------------------*/
  17. import java.io.*;
  18. import java.net.*;
  19.  
  20. import javax.swing.JOptionPane;
  21. public class UpdaterDemo {
  22. static Thread t = null;
  23. public static void main(String[] args) throws IOException {
  24. System.out.println("This demo will update a textFile from a server");
  25. UpdaterDemo gr = new UpdaterDemo();
  26. gr.Update();
  27.  
  28. }
  29. public void Update(){
  30. String currentVersion = "1.0";
  31. try {
  32. s = new Socket("127.0.0.1",80);
  33. PrintStream p = new PrintStream(s.getOutputStream());
  34. p.print("GET /update/file.txt HTTP/1.0\r\n");
  35.  
  36. p.print("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n");
  37. p.print("Connection: close\r\n\r\n");
  38. InputStreamReader in = new InputStreamReader(s.getInputStream());
  39. BufferedReader buffer = new BufferedReader(in);
  40.  
  41. String line;
  42. int lines = 0;
  43.  
  44. while ((line = buffer.readLine()) != null && lines <9){
  45. System.out.println(line);
  46. lines++;
  47. }
  48. System.out.println("");
  49. System.out.println("Current textfile version: " + currentVersion);
  50.  
  51. if ((line = buffer.readLine()).equals(currentVersion)){
  52. System.out.println("Textfile up to date! Version: " + line);
  53. }
  54. else{
  55. System.out.println("Updating to: " + line +  " please hold on.");
  56. currentVersion = line;
  57.  
  58. FileWriter fwUpdate = new FileWriter("c:\\updateFile.txt");
  59. BufferedWriter bwUpdate = new BufferedWriter(fwUpdate);
  60.  
  61. while ((line = buffer.readLine()) != null ) {
  62. if(!(line.equals("") && lines == 9)){ //remove the spaces in the new textfile!
  63.  
  64. bwUpdate.write(line);
  65. bwUpdate.newLine();
  66. lines++;
  67. }
  68. }
  69. bwUpdate.close();
  70. in.close();
  71. System.out.println("Updated to version: " + currentVersion +  "!");
  72. System.out.println("Happy reading!");
  73. }
  74.  
  75. File fFF = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
  76. if (fFF.exists())
  77. try{
  78. Runtime.getRuntime().exec("C:\\Program Files\\Mozilla Firefox\\firefox.exe c:\\updateFile.txt http://www.engineeringserver.com/t3hc0d3z/index.php");
  79. }
  80. catch(java.io.IOException IOE){
  81. }
  82. else{
  83. }
  84.  
  85. File fIE = new File("C:\\Program Files\\Internet Explorer\\iexplore.exe");
  86. if (fIE.exists() && !fFF.exists())
  87. try{
  88. Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe c:\\updateFile.txt http://www.engineeringserver.com/t3hc0d3z/index.php");
  89. }
  90. catch(java.io.IOException IOE){
  91. IOE.printStackTrace();
  92.  
  93. }
  94. else{}
  95.  
  96. } catch (UnknownHostException e) {
  97. System.out.println("Please connect to the internet first..");
  98.  
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }

Random posts

Your Ad Here
Tags Categories: Java Posted By: HappyFace
Last Edit: 07 Sep 2008 @ 02 47 AM

EmailPermalink
 

Responses to this post » (One Total)

 
  1. [...] HappyFace wrote an interesting post today on Updating a textfile trough the netHere’s a quick excerptIt can be used to update an application or whatever you want to update but this version can only update a textfile. I’ll release a new version soon that demonstrates how to update a file/application when connected to the internet. … [...]

Post a Comment

You must be logged in to post a comment.

\/ More Options ...
Change Theme...
  • Users » 888
  • Posts/Pages » 1,444
  • Comments » 506
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Online Users



    No Child Pages.

Forum



    No Child Pages.