Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Read contents of a file using StreamConnection Class


Code ResourceAuthor: Vamshidhar Matam  

Difficulty Level:

Published: 5th Nov 2006   Read: 5427 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This example uses the StreamConnection class to read the contents of a file referenced by a URL. Example described in MIDP Network Programming Using HTTP and the Connection Framework.

 


Sample Code
  1.  
  2. import java.io.*;
  3. import javax.microedition.io.*;
  4. import javax.microedition.lcdui.*;
  5. import javax.microedition.midlet.*;
  6.  
  7. public class FirstExample extends MIDlet {
  8.  
  9.     private Display display;
  10.  
  11.     String url = "http://www.javacourses.com/hello.txt";
  12.  
  13.     public FirstExample() {
  14.        display = Display.getDisplay(this);
  15.     }
  16.  
  17.     /**
  18.      * This will be invoked when we start the MIDlet
  19.      */
  20.     public void startApp() {
  21.         try {
  22.             getViaStreamConnection(url);
  23.         } catch (IOException e) {
  24.             //Handle Exceptions any other way you like.
  25.             System.out.println("IOException " + e);
  26.             e.printStackTrace();
  27.         }
  28.     }
  29.  
  30.     /**
  31.      * Pause, discontinue ....
  32.      */
  33.     public void pauseApp() {
  34.        
  35.     }
  36.  
  37.     /**
  38.      * Destroy must cleanup everything.  
  39.      */
  40.     public void destroyApp(boolean unconditional) {
  41.     }
  42.  
  43.     /**
  44.      * read url via stream connection
  45.      */
  46.     void getViaStreamConnection(String url) throws IOException {
  47.         StreamConnection c = null;
  48.         InputStream s = null;
  49.         StringBuffer b = new StringBuffer();
  50.         TextBox t = null;
  51.         try {
  52.           c = (StreamConnection)Connector.open(url);
  53.           s = c.openInputStream();
  54.           int ch;
  55.           while((ch = s.read()) != -1) {
  56.              b.append((char) ch);
  57.           }
  58.           System.out.println(b.toString());
  59.           t = new TextBox("hello....", b.toString(), 1024, 0);
  60.         } finally {
  61.            if(s != null) {
  62.               s.close();
  63.            }
  64.            if(c != null) {
  65.               c.close();
  66.            }
  67.         }
  68.         // display the contents of the file in a text box.
  69.         display.setCurrent(t);
  70.     }
  71.  
  72. }
  73.  
Copyright GeekInterview.com


Next: Image Applet


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora130725
2. iamdvr97690
3. venki_madesh35134
4. Raju18486
5. parmod kumar duhan15755
6. Kiran.jakkaraju14056
7. chowsys7455
8. Venkateswara Rao6473
9. ashish.cns6348
10. Vamshidhar Matam5428

Active Coders

Refined Tags