Solved

TxT to Html Converter J++

Posted on 1998-06-14
4
278 Views
Last Modified: 2012-05-04
Has anuone seen a simple Java application that converts normal text
into Html?
If so could you please post the url as a comment, I will only award the
100 points "graded A" if the full source code is posted here and that
it works with J++.
0
Comment
Question by:idc_movement
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 1223149
Normal text doesn't comprize any formatting statements such as bold, italic, headings, and
so on. Obviously, these statements will not magically appear in the conversion. All that you
will get is a flat text, as the original, but "converted" in an HTML file. The way to do this is
to enclose the text into the adequate HTML tags and use pre-formatting so that the end
of lines are treated as true end of lines (by default, end of lines are ignored in HTML).

Here is a sample, tested, implementation, written in 100% pure Java:

import java.io.*;

public class Txt2Html {

    public Txt2Html(String file) {
        try {
            FileInputStream  in  = new FileInputStream(file);
   
            FileOutputStream out = new FileOutputStream(file + ".html");
            out.write("<html>\n".getBytes()); // begin of HTML file
            out.write("<body>\n".getBytes()); // begin of the body of the page
            out.write("<pre>".getBytes()); // use pre-formatted text

            byte buffer[] = new byte[1000];

            int count=0;
            while((count=in.read(buffer)) > -1) {
                out.write(buffer,0,count);
            }

            out.write("</pre>".getBytes());  // end of pre-formatted text      
            out.write("</body>".getBytes()); // end of body
            out.write("</html>".getBytes()); // end of HTML
   
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // example of usage: java Txt2Html mytext.txt
    public static void main(String args[]) {
        Txt2Html converter = new Txt2Html(args[0]);
        return;
    }

}
0
 

Author Comment

by:idc_movement
ID: 1223150
Fontaine,
In J++, the following was produced.

C:\Administrator\Temp\Txt2Html.java(10,47) : error J0078: Class 'String' doesn't have a method that matches 'getBytes()'
C:\Administrator\Temp\Txt2Html.java(11,47) : error J0078: Class 'String' doesn't have a method that matches 'getBytes()'
C:\Administrator\Temp\Txt2Html.java(12,44) : error J0078: Class 'String' doesn't have a method that matches 'getBytes()'
C:\Administrator\Temp\Txt2Html.java(21,45) : error J0078: Class 'String' doesn't have a method that matches 'getBytes()'
C:\Administrator\Temp\Txt2Html.java(22,46) : error J0078: Class 'String' doesn't have a method that matches 'getBytes()'
C:\Administrator\Temp\Txt2Html.java(23,46) : error J0078: Class 'String' doesn't have a method that matches 'getBytes()'
Error executing jvc.exe.

Txt2Html - 6 error(s), 0 warning(s)

Then when I have the getBytes(), it produced this!

C:\Administrator\Temp\Txt2Html.java(10,20) : error J0078: Class 'FileOutputStream' doesn't have a method that matches 'write(String)'
C:\Administrator\Temp\Txt2Html.java(11,20) : error J0078: Class 'FileOutputStream' doesn't have a method that matches 'write(String)'
C:\Administrator\Temp\Txt2Html.java(12,20) : error J0078: Class 'FileOutputStream' doesn't have a method that matches 'write(String)'
C:\Administrator\Temp\Txt2Html.java(21,20) : error J0078: Class 'FileOutputStream' doesn't have a method that matches 'write(String)'
C:\Administrator\Temp\Txt2Html.java(22,20) : error J0078: Class 'FileOutputStream' doesn't have a method that matches 'write(String)'
C:\Administrator\Temp\Txt2Html.java(23,20) : error J0078: Class 'FileOutputStream' doesn't have a method that matches 'write(String)'
Error executing jvc.exe.

Txt2Html - 6 error(s), 0 warning(s)

Please review your code, am I missing anything?

0
 
LVL 5

Accepted Solution

by:
fontaine earned 100 total points
ID: 1223151
Visibly, J++ doesn't support JDK 1.1. Here is an equivalent, using only JDK 1.0 methods:

import java.io.*;

public class Txt2Html {

    public Txt2Html(String file) {
        try {
            FileInputStream  in  = new FileInputStream(file);
   
            FileOutputStream out = new FileOutputStream(file + ".html");
            out.write(getBytes("<html>\n"));
            out.write(getBytes("<body>\n"));
            out.write(getBytes("<pre>"));

            byte buffer[] = new byte[1000];

            int count=0;
            while((count=in.read(buffer)) > -1) {
                out.write(buffer,0,count);
            }

            out.write(getBytes("</pre>"));        
            out.write(getBytes("</body>"));
            out.write(getBytes("</html>"));
   
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public byte[] getBytes(String text) {
        byte txt[] = new byte[text.length()];
        text.getBytes(0,text.length(),txt,0);
        return txt;
    }

    // example of usage: java Txt2Html mytext.txt
    public static void main(String args[]) {
        Txt2Html converter = new Txt2Html(args[0]);
        return;
    }

}
0
 

Author Comment

by:idc_movement
ID: 1223152
I never new J++ didn't support JDK1.1.

Really Disapointed...

Anyways thanks alot, it worked like magic!

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 71
Configure a Bean in an XML file 4 48
Running JavaFX on JDeveloper 12C 1 76
jsp insert to database example 2 59
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question