Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java not trapping EOF correctly

Status
Not open for further replies.

adeleskie

IS-IT--Management
Joined
May 20, 2004
Messages
3
Location
CA
Hello,

I wrote a little utility to search for a specific string in the Application Event log in windows 2000 but it is causing me grief.

The app basically takes a host name as a parameter then it is supposed to parse out a username from a text file: AppEvent.evt.

If I delete a single character from the end of the event log it works. But nothing is returned if I point it to an unedited log.

I think this is probably a very simple thing to maybe check for a specific eof char or maybe change the type of stringhandler or something but I'm just not sure.

Anyway, here is the not so optimized code:


import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringWriter;
import java.io.FilterOutputStream;
import java.io.PrintStream;


public class ProxyHost {
public static void main(String[] args) throws IOException {

if(args.length > 0) {
String theFileName = ("\\\\" + args[0] + "\\c$\\winnt\\system32\\config\\AppEvent.Evt" );

System.out.println(theFileName);

try {
File inputFile = new File(theFileName);

FileReader in = new FileReader(inputFile);
BufferedReader buff = new BufferedReader(in);

StringWriter stringWrite = new StringWriter();
PrintWriter printWrite = new PrintWriter (stringWrite);
String proxyName = null;

boolean eof = false;
int count=0;
int proxyEvent=0;
int proxyNameStart=0;
int proxyNameEnd=0;
int proxyNameLength=0;


while (!eof) {

String line = buff.readLine();
count=count+1;
if (line == null)
eof = true;
else {
/* System.out.println("DEBUG: "+line); */
proxyEvent = line.indexOf("P r o x y H o s t S e r v i c e");
if (proxyEvent != 0 && proxyEvent != -1) {
System.out.println("host service found here: " + line) ;
proxyNameStart = line.indexOf(" a u t h e n t i c a t e d a s B I G - B L U E ",proxyEvent);
proxyNameStart = proxyNameStart+53;
proxyNameEnd = line.indexOf(" ) ",proxyNameStart);
proxyNameLength = proxyNameEnd - proxyNameStart ;

System.out.print("Line: "+count + "offset: "+proxyNameStart+ "Length: "+proxyNameLength +"\n");
stringWrite.write(line, proxyNameStart, proxyNameLength) ;
proxyName = stringWrite.toString();
System.out.print("Name? "+ proxyName);
eof = true;
}
}

}

in.close();
} catch (EOFException e) {
System.err.println("End of stream");
} catch (FileNotFoundException e) {
System.err.println("File Not Found");
}

}
else {
System.out.println("Usage: ProxyHost SERVERNAME") ;
}

}

}

Any help is greatly appreciated,

Aaron
 
You are going about the file read a bit strangely - this is how it should (or could) be done :

Code:
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String line = "";
while ((line = br.readLine()) != null) {
 // do stuff
}
br.close();
 
Thanks for the tip. That is a bit of a cleaner method than I was using for file handling. Unfortunately it didn't fix my initial problem. =(



 
post your error stack trace ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top