H
Hal Vaughan
I am experimenting with reading a data file from the web. In Firefox, I go
to an HTML page that is a form, enter the data for the form, then click
on "Submit", which calls a Perl program that outputs the data to Firefox.
The file is 173,910 bytes long and I'm using Apache2 on Linux on the
backend. When I do this on Firefox, the file comes back so quickly I can't
time it -- almost instantaneous.
I'm trying to read the same file in Java with the method at the bottom of
the post. When I first tried it with this file, it took so long (literally
at least 3-4 minutes), that I finally added in some debugging statements
that are commented out below (like printing a dot after each line was read
in so I could verify it was working).
I need to read this file and longer ones in quickly. I have very little
experience with network programming, in Java or in other languages. The
code is based on examples in tutorials and other places. What can I do to
get this method to work quickly? My guess is the slowdown is caused by
continually adding more and more text to a String. Should I be handling
the buffering or input differently?
What puzzles me is this code is similar to not just one, but several
examples I found while using Google to find tutorials and examples. Are
tutorials teaching us inefficient techniques, or is this more a "learning"
way to do it with better ones available?
Any helpful ideas or links are greatly appreciated.
Hal
==================
Java Method In use:
public String connect(String sURL, String messageText) {
String sLine, resultPage = "";
URL uPage;
URLConnection ucPage = null;
BufferedReader inRead;
PrintWriter outPrint;
try {
// System.out.println("URL: " + sURL);
uPage = new URL(sURL);
ucPage = uPage.openConnection();
ucPage.setDoOutput(true);
ucPage.setDoInput(true);
outPrint = new PrintWriter(ucPage.getOutputStream());
System.out.println("Outgoing data:\n" + messageText);
outPrint.print(messageText);
outPrint.close();
inRead = new BufferedReader(new
InputStreamReader(ucPage.getInputStream()));
resultPage = "";
while ((sLine = inRead.readLine()) != null) {
// System.out.print(".");
resultPage = resultPage + sLine + "\n";
}
System.out.println("\nIncoming message complete.");
// System.out.println("Result:\n-------------------------\n" +
// resultPage + "\n-------------------------\n");
inRead.close();
} catch (Exception e) {
resultPage = "error: connection incomplete";
e.printStackTrace();
}
return resultPage;
}
to an HTML page that is a form, enter the data for the form, then click
on "Submit", which calls a Perl program that outputs the data to Firefox.
The file is 173,910 bytes long and I'm using Apache2 on Linux on the
backend. When I do this on Firefox, the file comes back so quickly I can't
time it -- almost instantaneous.
I'm trying to read the same file in Java with the method at the bottom of
the post. When I first tried it with this file, it took so long (literally
at least 3-4 minutes), that I finally added in some debugging statements
that are commented out below (like printing a dot after each line was read
in so I could verify it was working).
I need to read this file and longer ones in quickly. I have very little
experience with network programming, in Java or in other languages. The
code is based on examples in tutorials and other places. What can I do to
get this method to work quickly? My guess is the slowdown is caused by
continually adding more and more text to a String. Should I be handling
the buffering or input differently?
What puzzles me is this code is similar to not just one, but several
examples I found while using Google to find tutorials and examples. Are
tutorials teaching us inefficient techniques, or is this more a "learning"
way to do it with better ones available?
Any helpful ideas or links are greatly appreciated.
Hal
==================
Java Method In use:
public String connect(String sURL, String messageText) {
String sLine, resultPage = "";
URL uPage;
URLConnection ucPage = null;
BufferedReader inRead;
PrintWriter outPrint;
try {
// System.out.println("URL: " + sURL);
uPage = new URL(sURL);
ucPage = uPage.openConnection();
ucPage.setDoOutput(true);
ucPage.setDoInput(true);
outPrint = new PrintWriter(ucPage.getOutputStream());
System.out.println("Outgoing data:\n" + messageText);
outPrint.print(messageText);
outPrint.close();
inRead = new BufferedReader(new
InputStreamReader(ucPage.getInputStream()));
resultPage = "";
while ((sLine = inRead.readLine()) != null) {
// System.out.print(".");
resultPage = resultPage + sLine + "\n";
}
System.out.println("\nIncoming message complete.");
// System.out.println("Result:\n-------------------------\n" +
// resultPage + "\n-------------------------\n");
inRead.close();
} catch (Exception e) {
resultPage = "error: connection incomplete";
e.printStackTrace();
}
return resultPage;
}