How to Convert HTML to Javascript in Java


Here this example focuses on how to convert HTML to Javascript in Java.

Source Code

package com.beginner.examples;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.PrintStream;

public class Html2jsExample {

	
		
	public static void main(String[] args) {
      
	  String pre = "document.write('";
	  String suf = "');";
	  
      try 
      {

          BufferedReader reader = new BufferedReader(new FileReader("in.html"));  
          OutputStream out= new FileOutputStream(new File("out.js"));
          PrintStream print = new PrintStream(out);

          StringBuffer sb = new StringBuffer();
          String line;

          while ((line = reader.readLine())!= null) 
          {

              line = pre+line+suf;              
              print.println(line);
          }
          
          print.close();
          out.close();
          reader.close();  
          System.out.println("Done!");
          
          }
      catch(Exception e) 
      {
    	  e.printStackTrace();
      }
   }
}

Output:

Done!

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments