Java change contant of the ".java" files

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ChangeDocuments;

import java.io.*;

/**
 *
 * @author Vera
 */
public class ChangeJavaClass {
    public static BufferedReader bufread;
    private static String path = "D:/HelloMIDlet.java";//the file path
    private static File filename = new File(path);
    private static String readStr = "";

    public static void replaceTxtByStr(String oldStr, String replaceStr) {
       String temp = "",s = "";
       try {
        File file = new File(path);
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        StringBuffer buf = new StringBuffer();

        

        while((s = br.readLine()) != null){
            String old = "http://";
            if(s.contains("http://")){
                System.out.println("chath the url: "+s);
                s= s.replace(old, replaceStr);
                System.out.println("now s is "+s);
            }
            temp = temp+ s + "/r/n";
            System.out.println("new the length of s is "+s.length());
        }

        FileOutputStream fos = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(fos);
        pw.write(temp);

        br.close();
        pw.flush();
        pw.close();
       } catch (IOException e) {
        e.printStackTrace();
       }
    }
    
    public static void main(String[] s) throws IOException {
        String str = "String url = "+"/""+"http://"+"/";";
        System.out.println("the url is: "+str);
        if(filename.exists()){
            System.out.println("file exist");
        }
       ChangeJavaClass.replaceTxtByStr(str, "http://www.google.com/");
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章