R
rboelio
I'm having what I think boils down to a problem with scope in Java.
I'm working on a small server for my networks class, and am fairly new
to the language. I've got some code to open a socket, and parse the
first line of text received for the pathname of a file I want to send
back through the socket. Once the path is received, I'm trying to use
a FileReader to extract text from the file, a BufferedReader to read
it and a DataOutputStream to send it. I'm a bit confused as to why I
need so many streams and readers, etc, but I digress. Here is a bit
of code, with some more details below it.
while(( clientLine = inFromClient.readLine() ) != null){
if(lineCount == 0){
int d1 = clientLine.indexOf(" ");
int d2 = clientLine.indexOf(" ", d1+1);
String path = new String(clientLine.substring(d1+1, d2));
fin = new BufferedReader(new FileReader(args[0] + path));
lineCount++;
}
String line;
while(( line = fin.readLine() ) != null){
outToClient.writeBytes(line);
}
}
The problem comes with the declaration and instantiation of the
variable fin. Currently, I've got this line:
BufferedReader fin = new BufferedReader(null);
At the top of main(). That line is causing runtime errors, I presume
because of the null parameter. However, I've got nothing else to pass
the constructor yet because the socket isn't even opened. However,
when I try this line in the outer while loop pasted above:
BufferedReader fin = new BufferedReader(new FileReader(args[0] +
path);
where " args[0] + path" contains the correct pathname, the compiler
complains about subsequent lines that attempt to read from fin (the
inner while loop) and close the stream. I get errors saying fin may
not have been instantiated. I assume this is because it is possible
that fin may never get instantiated within the conditional if(),
although I have added some lines to prevent that. Any help would be
much appreciated, as this is only the second of a 3 part lab due on
Thursday
I'm working on a small server for my networks class, and am fairly new
to the language. I've got some code to open a socket, and parse the
first line of text received for the pathname of a file I want to send
back through the socket. Once the path is received, I'm trying to use
a FileReader to extract text from the file, a BufferedReader to read
it and a DataOutputStream to send it. I'm a bit confused as to why I
need so many streams and readers, etc, but I digress. Here is a bit
of code, with some more details below it.
while(( clientLine = inFromClient.readLine() ) != null){
if(lineCount == 0){
int d1 = clientLine.indexOf(" ");
int d2 = clientLine.indexOf(" ", d1+1);
String path = new String(clientLine.substring(d1+1, d2));
fin = new BufferedReader(new FileReader(args[0] + path));
lineCount++;
}
String line;
while(( line = fin.readLine() ) != null){
outToClient.writeBytes(line);
}
}
The problem comes with the declaration and instantiation of the
variable fin. Currently, I've got this line:
BufferedReader fin = new BufferedReader(null);
At the top of main(). That line is causing runtime errors, I presume
because of the null parameter. However, I've got nothing else to pass
the constructor yet because the socket isn't even opened. However,
when I try this line in the outer while loop pasted above:
BufferedReader fin = new BufferedReader(new FileReader(args[0] +
path);
where " args[0] + path" contains the correct pathname, the compiler
complains about subsequent lines that attempt to read from fin (the
inner while loop) and close the stream. I get errors saying fin may
not have been instantiated. I assume this is because it is possible
that fin may never get instantiated within the conditional if(),
although I have added some lines to prevent that. Any help would be
much appreciated, as this is only the second of a 3 part lab due on
Thursday