Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: HELP with sending data within text file to client with printwriter

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP with sending data within text file to client with printwriter

    import java.io.*;
    import java.net.*;
    import java.util.*;
     
     
    public class Server{
     
      public static void main(String[] args){
        Server simpleserver = new Server();
        simpleserver.init();
      }
     
      public void init(){
        ServerSocket serversocket = null;
        Socket socket = null;
        try{
          serversocket = new ServerSocket(8189);
          System.out.println("Listening at 127.0.0.1 on port 8189");
          socket = serversocket.accept();
     
     
     
     
     
          PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
          InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream());
          BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            String datetimestring = (Calendar.getInstance()).getTime().toString();
          printwriter.println("You connected at " + datetimestring);
     
     
     
          String lineread = "";
          lineread = bufferedreader.readLine();
     
     
          char selection;
          Scanner input = new Scanner(System.in);
          selection = lineread.charAt(0);
          switch(selection)
          {
    case 's': case 'S':
          try
          {
     [COLOR="red"]     FileInputStream fstream = new FileInputStream("shows.txt");
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br1 = new BufferedReader(new InputStreamReader(in));
          String strLine;
          while ((strLine = br1.readLine()) != null)
          {
          System.out.println("");
          System.out.println (strLine);
          }[/COLOR]
          in.close();
          }
          catch (Exception e){System.err.println("Error: " + e.getMessage());}      }
     
     
     
     
     
     
     
          System.out.println("Closing connection.");
          bufferedreader.close();
          inputstreamreader.close();
          printwriter.close();
          socket.close();
        }catch(UnknownHostException unhe){
          System.out.println("UnknownHostException: " + unhe.getMessage());
        }catch(InterruptedIOException intioe){
          System.out.println("Timeout while attempting to establish socket connection.");
        }catch(IOException ioe){
          System.out.println("IOException: " + ioe.getMessage());
        }finally{
          try{
            socket.close();
            serversocket.close();
          }catch(IOException ioe){
            System.out.println("IOException: " + ioe.getMessage());
          }
        }
      }
    }
    Last edited by copeg; April 24th, 2011 at 02:42 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HELP with sending data within text file to client with printwriter

    First, please use the code tags. Second, what's the question?

Similar Threads

  1. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  2. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  3. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  4. Monitor the progress of sending a file to client
    By adumi in forum Java Theory & Questions
    Replies: 0
    Last Post: April 17th, 2010, 07:01 AM
  5. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM