Results 1 to 2 of 2
  1. #1
    Bookworm123 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Question Java RMI reading file from server

    Hi,
    I'm new here. It's been ages since I had last worked with Java. I need a help.
    I'm trying to write a program to read a file from the server line by line.
    Now, every time I'll be able to read only first line.
    Kindly help. It'd be greatly appreciated.

    Here is my client..

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */


    /**
    *
    * @author user
    */
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.rmi.*;
    import java.rmi.registry.*;
    import java.rmi.server.*;
    public class myClient {
    public static void main(String args[])
    {
    try {
    String fnm,str,choice="yes";
    boolean flag1,flag2;
    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));

    String myServerURL = "rmi://" + args[0] + "/myServer";
    ReadFileIntf myServerIntf =(ReadFileIntf)Naming.lookup(myServerURL);
    System.out.println(" file is: " + args[1]);
    fnm=args[1];
    flag1=addServerIntf.openFile(fnm);
    if(flag1==false)
    System.out.println("the file does not exist. " );
    else
    {
    System.out.println("the file exists. " );
    while(!choice.equalsIgnoreCase("no"))
    {
    System.out.println("Do want to read the file content further,yes or no: " );
    choice=buf.readLine();
    if(choice.equalsIgnoreCase("yes"))
    {


    str=myServerIntf.nextLine(fnm);
    System.out.println("Line "+str );
    }
    else
    break;
    }
    flag2=closeFile1(fnm);
    }
    }
    catch(Exception e) {
    System.out.println("Exception: " + e);
    }
    }
    }


    __________________________________________________ ______________________________________________


    Interface Implement.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    /**
    *
    * @author user
    */

    import java.io.*;
    import java.rmi.*;
    import java.rmi.server.*;
    public class ReadFileImpl extends UnicastRemoteObject implements ReadFileIntf
    {

    public ReadFileImpl() throws RemoteException
    {
    }
    public boolean openFile(String filename) throws RemoteException
    {
    File file=new File(filename);
    boolean exists = file.exists();
    if (!exists)
    {
    return false;

    }
    else
    {
    return true;
    }
    }
    public String nextLine(String filename) throws RemoteException
    {
    String strLine1="";
    try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream(filename);
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    while (!(strLine = br.readLine()).equalsIgnoreCase("hello")) {

    return(strLine1);
    }


    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    return "no";
    }

    }


    __________________________________________________ _________-----


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    import java.net.*;
    import java.rmi.*;
    /**
    *
    * @author user
    */
    public class myFileServer {


    public static void main(String args[]) {
    try {
    ReadFileImpl r = new ReadFileImpl();
    Naming.rebind("myFileServer", r);
    }
    catch(Exception e) {
    System.out.println("Exception: " + e);
    }

    }
    }

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Java RMI reading file from server

    Please use CODE /CODE tags around your code so it's easier to understand. (I have no idea how to escape the brackets, but those should be in brackets.)

    Java Code:
    if(choice.equalsIgnoreCase("yes")) {
        str=myServerIntf.nextLine(fnm);
        System.out.println("Line "+str );
     }
    Appears to be only reading the first line.

    you'll want something like:
    Java Code:
       while(myServerIntf.hasNextLine(fnm)){     //I don't know this class... I don't know what's been implemented, etc.
           str=myServerIntf.nextLine(fnm);          //you may be able to read until null is returned, etc. that's why I say "like this"
           System.out.println("Line "+str );
       }
    Hope that at least points you in the right direction.

Similar Threads

  1. Replies: 4
    Last Post: 09-29-2011, 01:17 PM
  2. Applet reading/writing to server side file
    By JamieG in forum Java Applets
    Replies: 3
    Last Post: 09-10-2011, 10:25 PM
  3. Reading Binary File using java
    By pnbalaji in forum New To Java
    Replies: 16
    Last Post: 06-11-2010, 02:50 PM
  4. reading URL using java through proxy server
    By asheesh in forum Networking
    Replies: 16
    Last Post: 04-25-2010, 02:15 PM
  5. [SOLVED] Applet reading from file on server
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-26-2009, 11:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •