zoukankan      html  css  js  c++  java
  • 【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会找不到路径

  • 相关阅读:
    Codeforces Round #384 (Div. 2)
    Codeforces Round #383 (Div. 2)
    bzoj-4514(网络流)
    bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)
    bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)
    hdu-5988 Coding Contest(费用流)
    hdu-5992 Finding Hotels(kd-tree)
    用链表实现杭电1276士兵队列训练问题
    循环链表
    图书管理系统
  • 原文地址:https://www.cnblogs.com/flydkPocketMagic/p/7122437.html
Copyright © 2011-2022 走看看