Solved

TxT to Html Converter J++

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

12 Experts available now in Live!

Get 1:1 Help Now