• 【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会找不到路径

  • 相关阅读:
    C++指针(一)
    探讨mvc下linq多表查询使用viewModel的问题
    利用aspnetPager控件加存储过程实现对gridview的分页
    Javascript 笔记
    对于最近学习WCF的总结——唔聊的服务
    今天面试的一些知识性总结
    使用C#反序列化plist文件
    Running x86 apps on WinRT devices
    ItemsSource绑定中遇到的问题
    字节对齐 详解
  • 原文地址:https://www.cnblogs.com/flydkPocketMagic/p/7122437.html
Copyright © 2020-2023  润新知