java读写文件的笔记 如properties转为map

12=Pop Music一个example的函数调用我的searchBySongType接口,example传递Pop Music给searchBySongType。searchBySongType拿到Pop Music后要转换成12再到库里去查询。

13=Pop Music/Classic还有一种就是Pop Music和Classic同时指向13,我是这样处理的:首先要保证Classic能搜到音乐,Pop Music也能搜到音乐。所以Classic和Pop Music为键, 13为值。

key :Pop Music指向value:12, 13 set集合
key:Classic指向 value:13 set集合

这些存放在songType.txt中,我要处理txt文件,将他们转换乘java中的map的键值对形式。

private Map<String, HashSet<String>> SONG_TYPE_REF_MAP = new ConcurrentHashMap<>();

下面是处理songType.txt文本

58=Singer Songwriter/Rock 摇滚/流行/摇滚
29=Miscellaneous
46=R&B/流行/RB 节奏布鲁斯/节奏布鲁斯
13=流行/Pop 流行/K-Pop 韩国流行/Rock 摇滚
61=World Music 世界音乐

java代码

@PostConstruct
    private void init() {
	//加载songType.txt文件
        ClassPathResource classPathResource = new ClassPathResource("songType.txt");
        try (
		//将songType.txt文件转换为流文件一行一行读取
                InputStream is =  classPathResource.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                BufferedReader reader = new BufferedReader(isr);
                ) {
            String line = null;

            while((line = reader.readLine()) != null) {
                String[] temp = line.split("=");
                if (temp.length > 1) {
		    //这里因为是12=Pop Music表示方式,所以temp[1]为key,temp[0]为value
                    String key = temp[1];
                    String value = temp[0];
                    //可能在temp[1]中存在多种属性,所以将他们分隔开成为一个新的键
                    String[] temp_keys = StringUtils.split(key, "/");
                    for (String single_key : temp_keys) {
                        single_key = StringUtils.trimToEmpty(single_key);
                        if (StringUtils.isNotBlank(single_key)) {
                            HashSet<String> values = SONG_TYPE_REF_MAP.get(single_key);
                            if (values == null) {
                                values = new HashSet<>();
                                SONG_TYPE_REF_MAP.put(key, values);
                            }
                            values.add(value);
                        }
                    }


                }

            }
        } catch (Exception e) {

        }
    }

读取json文件,将json文件内容一次性转换为string,再转成相应的json

poetry.json存放在resources目录下

[ {"59f859fa5fb61e36f8eee085":"转寿春守,太和庚"},
  {"59f8557a5fb61e1d744058b2":"辛巳除夕与彭同年"},
  {"59f85c465fb61e36f8ef0cbe":"达奚中丞东斋壁画"}
]
JSONArray array = new JSONArray();

try {
    ClassPathResource classPathResource = new ClassPathResource("poetry.json");
    String str = IOUtils.toString(new InputStreamReader(classPathResource.getInputStream(), StandardCharsets.UTF_8));
    array = JSONObject.parseArray(str);
} catch (Exception e) {
    e.printStackTrace();
}

以properties文件为例子,根据绝对路径读取文件。如果根据查找的路径文件不存在,再新建一个文件

try {
	// 从输入流中读取属性列表(键和元素对)
	Properties prop = new Properties();
	if(StringUtils.isNotBlank(path)) {
		File file = new File(path);
        //如果文件不存在,再新建一个文件
		if(!file.exists()) {
			file.createNewFile();
		}

		FileInputStream fin = new FileInputStream(file);

		prop.load(fin);

		if(StringUtils.isNotBlank(prop.getProperty("spider_album_index"))) {
			pageIndex = Integer.parseInt(prop.getProperty("spider_album_index"));
		}

		fin.close();
	}
} catch (Exception e) {
	e.printStackTrace();
}

根据绝对路径 写入文件

try {
	Properties prop = new Properties();
	if(StringUtils.isNotBlank(path)) {

		FileOutputStream fos = new FileOutputStream(path);
		prop.setProperty("spider_album_index", String.valueOf(page));
		prop.store(fos, "");

		fos.close();
		fos.flush();
	}
} catch (Exception e) {
	e.printStackTrace();
}

猜你喜欢

转载自blog.csdn.net/Hello_Ray/article/details/80989197