2 Very useful java class for web developing

View previous topic View next topic Go down

2 Very useful java class for web developing

Post by BIT0104-ANIK on Sun Aug 01, 2010 8:17 am

Well, it's been some time since i have last shared something with you guys. I didn't learn much though else i would have shared it.

Now, I do know that, not much of you are doing web developing works. But someday you will. That time you may find these 2 classes very helpful.

A few days ago, I got a .doc file to be converted to html page which again would have to have fields that could be filled dynamically. Quite a task for me.

The first challenge was met easily with the help of Amit. Now I have to convert this page to php page in order to do surgical work on it. You might say that, "eh...that's easy!! just have some echo(); before each line."

In this case, there are two problems.
1. If the file is too large (mine was 1500 lines) and you're gonna do it manually..(well, what can i say then?).
2. If the page has double quotes, then they must be changed to single quote before you can use echo(""); method with them.(R U gonna do it 'manushally'???)

At first i thought i would be able to do them by hand!!

BEHOLD!!! I have solved both problem's by 3 hrs of java coding and 4 smokes.(smoke may not work on you though). I am giving you 2 java classes that would change your html source code page to php, with just 2 CTRL+F11's. You would find file reading and writing codes on the internet but these codes are customized to serve your purpose.

Run the first one to solve prob number 2.
Code:

import java.io.*;

 public class fileJava {
  public static void main(String args[]){
    try {
      fileJava j = new fileJava();
      j.insertStringInFile
          (new File("c:\\FileToBeChanged.txt"));
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }

  public void insertStringInFile(File inFile)
      throws Exception {

    File outFile = new File("c:\\temp.txt");
   

    FileInputStream fis  = new FileInputStream(inFile);
    BufferedReader in = new BufferedReader
        (new InputStreamReader(fis));

       
    FileOutputStream fos = new FileOutputStream(outFile);
    PrintWriter out = new PrintWriter(fos);

    String thisLine = "";
   
   
   
    while ((thisLine = in.readLine()) != null) {
    char[] arr=thisLine.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]=='"') arr[i]=''';
           
        }
       
       
    String s=new String(arr);   
     
      out.println(s);
     
      }
    out.flush();
    out.close();
    in.close();
   
    inFile.delete();
    outFile.renameTo(inFile);
  }

}

Now run the second one to add echo's with each line.

Code:



import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;


public class fileJava2 {

  public static void main(String[] args) throws FileNotFoundException {

    File file = new File("C:\\fileToBeChanged.txt");
    File file1=new File("C:\\temp.txt");
   
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    FileOutputStream fos=new FileOutputStream(file1);
    PrintWriter out = new PrintWriter(fos);
   
   
   
    try {
      fis = new FileInputStream(file);

   
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

   
      while (dis.available() != 0) {

          out.println("echo(""+dis.readLine()+"");");
      }


      fis.close();
      bis.close();
      dis.close();
      out.close();
     
      file.delete();
      file1.renameTo(file);

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}


fianally just add php tag and other functions as necessary.

Let me know if you find it helpful.

_________________________________________________________________
Buddhong shoronogocchami..
dhommong shoronogocchami..
shonghong shoronogocchami..
Jogoter sokol prani.. shukhi hok.
avatar
BIT0104-ANIK
Administrator
Administrator

Course(s) :
  • BIT

Blood Group : O+
Posts : 423
Points : 699

View user profile http://anikstech.blogspot.com

Back to top Go down

Re: 2 Very useful java class for web developing

Post by BIT0122-Amit on Sun Aug 01, 2010 8:30 pm

nicely done Cool!
and thanks Very Happy

However Razz i am not going to give you rep++. because a non java php developer may find it scary to find the input and output file.

allow me, to give advice about making it even better.

My suggestion?

make the java files accept filenames via command line. Better, make a command line based instructiom (shell or batch) and when you drag the html file over the batch file, it will convert it automatically by the same file name with php extension.
Also, there can be option of making a gui Smile

that can be expected from a talented one like you, right?

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: 2 Very useful java class for web developing

Post by BIT0112-Rokon on Sun Aug 01, 2010 10:09 pm

Nice work Anik..

btw you can find some online converter to make life more easy. Here I can give you one of them,
http://www.quasarcr.com/html2php/

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: 2 Very useful java class for web developing

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum