Click to See Complete Forum and Search --> : How to write results to a text file ?????
yna54
February 5th, 2002, 02:56 PM
I am trying to append the results like this :
to a text file, here is what I wrote :
........
System.out.println("Type " + connection.getContentType());
save("Type " + connection.getContentType());
System.out.println("Length " + connection.getContentLength());
save("Length " + connection.getContentLength());
System.out.println("Length " + connection.getHeaderField(1));
save("Length " + connection.getHeaderField(1));
System.out.println("Length " + connection.getHeaderField(2));
System.out.println("Length " + connection.getHeaderField(3));
System.out.println("Length " + connection.getHeaderField(4));
}
public static void save(String data) {
try {
//String text = textField.getText();
byte b[] = data.getBytes();
String outputFileName = "text.txt";
FileOutputStream out = new FileOutputStream(outputFileName);
out.write(b);
out.close();
} catch(java.io.IOException e) {
System.out.println("Cannot write to text.txt");
}
}
....
but it does not append.
Norm
February 5th, 2002, 06:26 PM
What is supposed to be appended to what? I don't know from your program comments what the code is supposed to do.
Why do you expect data to be written to a file to be appended to an existing file vs creating a new file?
Does your documentation say that?
Read the doc for the classes that do file ouput and you'll find one that has a constructor with a second argument that specifies that the output is to be appended.
Norm
yna54
February 6th, 2002, 09:50 AM
Here is the result of my program :
C:\>javac MajidPortScanner4.java
C:\>java MajidPortScanner4
Test for ip 155.108.117.068 .......................................
current port scanning is :80
HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/5.0
Date: Wed, 06 Feb 2002 14:39:50 GMT
Content-Type: text/html
Content-Length: 87
<html><head><title>Error</title></head><body>The parameter is incorrect. </body>
</html>
A server is listening on port 80 of dev-gisg.statestr.com
current port scanning is :8080
The remote host dev-gisg.statestr.com is not listening on this port
Test for ip 155.108.117.012 ..........................................
current port scanning is :80
HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/5.0
Date: Wed, 06 Feb 2002 14:39:51 GMT
Content-Type: text/html
Content-Length: 87
<html><head><title>Error</title></head><body>The parameter is incorrect. </body>
</html>
A server is listening on port 80 of MIL2WWDS40
current port scanning is :8080
The remote host MIL2WWDS40 is not listening on this port
Test for ip 155.108.117.82 ................................................
current port scanning is :80
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
Invalid URI in request GET HTTP/1.0
<P>
<HR>
<ADDRESS>Apache/1.3.22 Server at mil2wwds011 Port 80</ADDRESS>
</BODY></HTML>
A server is listening on port 80 of MIL2WWDS011
current port scanning is :8080
The remote host MIL2WWDS011 is not listening on this port
Test for ip 155.108.117.064 .................................................
current port scanning is :80
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
Invalid URI in request GET HTTP/1.0
<P>
<HR>
<ADDRESS>Apache/1.3.22 Server at MIL2WWDS13.statestr.com Port 80</ADDRESS>
</BODY></HTML>
A server is listening on port 80 of MIL2WWDS13.statestr.com
current port scanning is :8080
HTTP/1.0 404 Not Found
Content-Type: text/html
Content-Length: 163
Servlet-Engine: Tomcat Web Server/3.2.3 (JSP 1.1; Servlet 2.2; Java 1.3.0_02; Wi
ndows 2000 5.0 x86; java.vendor=Sun Microsystems Inc.)
<head><title>Not Found (404)</title></head>
<body><h1>Not Found (404)</h1>
<b>Original request:</b> HTTP/1.0
<b>Not found request:</b> HTTP/1.0</body>
A server is listening on port 8080 of MIL2WWDS13.statestr.com
C:\>
AND here is where I disply this data to the console and in the same time to a file :
...........
String fname = "zfile.txt";
try {
......
FileWriter writer = new FileWriter(fname);
while ((theLine = br.readLine()) != null) {
System.out.println(theLine);
writer.write(theLine, 0, theLine.length());
//writer.close();
}
writer.close();
}
catch (MalformedURLException e) {
System.out.println( " not a valid URL");
}
catch (IOException e) {
System.out.println(e);
}
.........
BUT when I checked the text file zfile.txt I only found this :
HTTP/1.0 404 Not FoundContent-Type: text/htmlContent-Length: 163Servlet-Engine: Tomcat Web Server/3.2.3 (JSP 1.1; Servlet 2.2; Java 1.3.0_02; Windows 2000 5.0 x86; java.vendor=Sun Microsystems Inc.)<head><title>Not Found (404)</title></head><body><h1>Not Found (404)</h1><b>Original request:</b> HTTP/1.0
<b>Not found request:</b> HTTP/1.0</body>
Where is the other data ??
Norm
February 6th, 2002, 06:07 PM
Is it possible that your program calls the code shown more than once? Each time it is executed it will create a new version of the file.
To see how to use a class like FileWriter, I suggest that you write a small standalone program that you can make changes to until you understand how to use it. Then move that code to your program.
Norm
codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.