Results 1 to 2 of 2
- 11-04-2012, 06:20 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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);
}
}
}
- 11-04-2012, 07:07 AM #2
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
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.)
Appears to be only reading the first line.Java Code:if(choice.equalsIgnoreCase("yes")) { str=myServerIntf.nextLine(fnm); System.out.println("Line "+str ); }
you'll want something like:
Hope that at least points you in the right direction.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 ); }
Similar Threads
-
java server client socket sending and reading data HELP!
By mrhid6 in forum Java GamingReplies: 4Last Post: 09-29-2011, 01:17 PM -
Applet reading/writing to server side file
By JamieG in forum Java AppletsReplies: 3Last Post: 09-10-2011, 10:25 PM -
Reading Binary File using java
By pnbalaji in forum New To JavaReplies: 16Last Post: 06-11-2010, 02:50 PM -
reading URL using java through proxy server
By asheesh in forum NetworkingReplies: 16Last Post: 04-25-2010, 02:15 PM -
[SOLVED] Applet reading from file on server
By DenniGa in forum Java AppletsReplies: 3Last Post: 02-26-2009, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks