【File】替换.json中的路径

package com...util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import com....StringUtil;

/**
 * @Title: AndyFrameJSONUtil.java
 * @Package: com.wisesoft.scenic.util
 * @Description: TODO
 * @author: 
 * @date: 2017年7月5日 下午5:10:54
 * @version: V1.0
 */
public class AndyFrameJSONUtil {
    public static String ReadFile(String Path) {
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(Path);
            InputStreamReader inputStreamReader = new InputStreamReader(
                    fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }
    
    public static void replaceContextPath(String path, String rStr, String contextPath){
        String rSrc = ReadFile(path);
        if(StringUtil.hasText(rSrc) && StringUtil.hasText(rStr) && StringUtil.hasText(contextPath)){
            rSrc = rSrc.replaceAll(rStr, contextPath);
            writeFile(path, rSrc);
        }
    }
    
    public static void writeFile(String path, String json){
        FileOutputStream fos;
        OutputStreamWriter osw;
        try {
            fos = new FileOutputStream(path+"1.json");
            osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(json);
            osw.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
    
    public static void main(String[] args) {
        replaceContextPath("C:\\JavaWorkSpace\\ws20170405\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp2\\wtpwebapps\\scenic-console\\wsbpScenic\\andyui1.1.0\\js\\framework\\json\\system.json",
                "contextPath", "22223333");
    }
}
String jsonPath = request.getSession().getServletContext().getRealPath("/")
                +"xx.json";
        AndyFrameJSONUtil.replaceContextPath(jsonPath, "contextPath", contextPath);

 


  注意:不要有\\的写法,所有都应该/ 否则linux会找不到路径

posted @ 2017-07-05 17:13  丶会飞的羊  阅读(421)  评论(0编辑  收藏  举报