Solved

TxT to Html Converter J++

Posted on 1998-06-14
4
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

628 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