在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 讀取 source.conf 配置文件后,如何提取出name、x、y的值分別放到

讀取 source.conf 配置文件后,如何提取出name、x、y的值分別放到不同的數(shù)組中

source.conf配置文件

screen={
    monitor=[
      {"name":"巴南區(qū)交委防火墻", "x":20, "y":40},
      {"name":"巴南區(qū)交委交換機", "x":25, "y":40},
      {"name":"巴南區(qū)路政", "x":28, "y":50}
    ]
    device={
        router=100
        switch=200
        firewall=90
        camera=1
        server=50
    }
}
package com.mvc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

public class FileInputStreamDemo {

    private static final int SIZE = 4096;

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

        /*
         * 將已有文件的數(shù)據(jù)讀取出來
         * 既然是讀,使用InputStream
         * 而且是要操作文件 FileInputStream
         *
         */

        //為了確保文件一定在之前是存在的,將字符串路徑封裝成File對象
        File file = new File("src/main/java/com/mvc/configure/source.conf");
        if(!file.exists()){
            throw new RuntimeException("要讀取的文件不存在");
        }

        //創(chuàng)建文件字節(jié)讀取流對象時,必須明確與之關(guān)聯(lián)的數(shù)據(jù)源。
        FileInputStream fis = new FileInputStream(file);

        //創(chuàng)建一個字節(jié)數(shù)組,定義len記錄長度
        int len = 0;
        byte[] buf = new byte[SIZE];
        while((len=fis.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }

        //關(guān)資源
        fis.close();
    }

}
回答
編輯回答
冷咖啡

一行一行的讀取. redeLine()
然后在進行字符串切割.

2017年7月28日 04:47
編輯回答
萌小萌
public class Bean {

    private String name;
    private int x;
    private int y;
BufferedReader bw=null;
        StringBuffer sb=null;
        try {
            bw=new BufferedReader(new FileReader("src/source.conf"));
            sb=new StringBuffer();
            String line=null;
            while((line=bw.readLine())!=null){
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String result=sb.toString().replaceAll("\\s+", "");
        Pattern p = Pattern.compile("monitor=(\\[[^\\]]*\\])");//正則表達式提取
        Matcher m = p.matcher(result);
        while(m.find()){
            String res=m.group();
            res=res.substring(res.indexOf("["), res.lastIndexOf("]")+1);
            Gson gson=new Gson();
            List<Bean> beans=gson.fromJson(res, new TypeToken<List<Bean>>() {}.getType());//Gson轉(zhuǎn)為List集合
            System.out.println(beans);
        }
2018年2月21日 01:51