Solved

TxT to Html Converter J++

Posted on 1998-06-14
4
267 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 54
splitOdd10 challenge 5 74
Java Filesystem manipulation of Word Docx file (find replace text) 3 64
maven example error 3 52
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now