Board index » jbuilder » Refining the textfile output?

Refining the textfile output?


2003-10-18 01:34:20 AM
jbuilder14
Hi All,
I am attempting to save 3 lines of text using a
FileOutputStream. Java saves what seems to be some
instruction code at the start of the file to
be saved.
I have this code:
package stringgrabber;
import java.io.*;
public class grabStrings {
public static void main(String[] args) {
String[] strings = {
"The time is now that all good workers should ",
"begin to carry their tools to the jobsite ",
"and all ambitious workers should have pencil ",
"and paper so that in the end ",
"you can make a record of what you have done ",
"so that later when you return to the office"
};
String bigString = new String();
int i = 0;
while (i < strings.length) {
bigString = bigString + strings[i];
i++;
}
String newString = bigString.substring(bigString.indexOf(
"begin"), bigString.indexOf("end") + 3);
try {
FileOutputStream fout = new
FileOutputStream("TextOnly.txt");
ObjectOutputStream out = new
ObjectOutputStream(fout);
out.writeBytes(newString);
//out.writeChars(newString);
System.out.println(newString);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
"TextOnly.txt" looks like this:
wsbegin to carry their tools to the jobsite and all ambitious
workers should have pencil and
paper so that in the end
Can't this textfile be cleaned up to look like this when showing
the text file after it has been
made??
begin to carry their tools to the jobsite and all ambitious
workers should have pencil and paper
so that in the end
TIA
Bob H
 
 

Re:Refining the textfile output?

Owen,
Thanks for the information that allows me to obtain a better
looking text file.
Extending the ideas I wanted to save the information from a
html page.
Below is my program shows one of 2 versions that allow the
display an html document either
from the net, or from a html document that is stored on the
my HD. In the output there is little if
any spacing and a lot of wrapping. Is there a better selection
of imports to use to make it
happen????
Can you help?
Bob H
package html2textbyreal;
import java.net.*;
import java.io.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
// Get connected to the internet before running this
public class Html2Text
extends HTMLEditorKit.ParserCallback {
StringBuffer s;
public Html2Text() {}
public void parse(Reader in) throws IOException {
s = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
delegator.parse(in, this, true); // maybe false here.... not
understood why ???
}
public void handleText(char[] text, int pos) {
s.append(text);
}
public String getText() {
return s.toString();
}
public static void main(String[] args) throws Exception {
String Line = "";
try {
// the HTML to convert: this by URL
URL url = new
URL("newsgroups.borland.com/cgi-bin/dnewsweb?cm
d=article&group=borland.public.jbuil
der.students&item=7407&utag=");
BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream())
);
Html2Text parser = new Html2Text();
parser.parse(reader);
reader.close();
Line = parser.getText();
}
catch (Exception e) {
e.printStackTrace();
}
try {
DataOutputStream out = new DataOutputStream(new
FileOutputStream(
"TextOnly.html")); //or "TextOnly.txt"
out.writeBytes(Line);
System.out.println(Line);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/*
// alternate design html already present in folder
package html2textbyreal;
import java.io.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
public class Html2Text
extends HTMLEditorKit.ParserCallback {
StringBuffer s;
public Html2Text() {}
public void parse(Reader in) throws IOException {
s = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
delegator.parse(in, this, true);
}
public void handleText(char[] text, int pos) {
s.append(text);
}
public String getText() {
return s.toString();
}
public static void main(String[] args) {
String Line = "";
try {
// the HTML to convert
FileReader in = new FileReader(
"Newsgroup Articles
borland_public_jbuilder_students7407.htm");
Html2Text parser = new Html2Text();
parser.parse(in);
in.close();
Line = parser.getText();
DataOutputStream out = new DataOutputStream(new
FileOutputStream(
"TextOnly.txt"));
out.writeBytes(Line);
System.out.print(Line);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
*/
Output from the above Java Program (abbreviated):
....... edited to show detail......
borland.public.jbuilder.studentsSubject:Re: Refining the
textfile output?Date:Fri, 17 Oct 2003
18:36:49 +0100From:Owen Green
Newsgroups:borland.public.jbuilde
r.studentsOn 17 Oct 2003
10:34:20 -0700, Bob H wrote:>>Hi All,>I am attempting
to save 3 lines of text using a>FileOutputStream. Java
saves what seems to be some>instruction code at the start
of the
file to>be saved. That's because you're using an
ObjectOutputStream which writes
serialized object data to a stream in a specific format to be
read by ObjectInputStream. So, either use an
ObjectInuptStream whilst reading the data (not
reccomended), or use
something like PrintWriter instead: (untested) PrintWriter
output = new PrintWriter(new
OutputStreamWriter(fout),true)); Even better, use a
FileWriter instead of FileOutputStream
(unless you need to include some binary content) and
dispense with the
OutputStreamWriter. Also,>String bigString = new
String();>int i = 0;>while (i <
strings.length) {>bigString = bigString + strings[i];>i++;>
}>String newString =
bigString.substring(bigString.indexOf
..................... end of copied.......