Solved

TxT to Html Converter J++

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 52
Java Jpanels and Jframe 8 33
Error trying to install RTMT Win7 5 60
session migration servlets 2 36
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 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