Java读取文件总结

Java读取文件总结,第1张

Java读取文件总结
public static ArrayList readTxtFile() {
		String fileName = JavaFile.class.getClassLoader().getResource("filename/list.txt").getPath();
		ArrayList arrayList = new ArrayList<>();
		try {
			File jsonFile = new File(fileName);
			FileReader fileReader = new FileReader(jsonFile);
			//Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
			BufferedReader bf = new BufferedReader(fileReader);
			int ch = 0;
			StringBuffer sb = new StringBuffer();
			String str;
			while ((str = bf.readLine()) != null) {
                arrayList.add(str);
            }
			fileReader.close();
			bf.close();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return arrayList;
		
		
	}

// 读取json文件
	public static String readJsonFile(String fileName) {
		String jsonStr = "";
		try {
			File jsonFile = new File(fileName);
			FileReader fileReader = new FileReader(jsonFile);
			Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
			int ch = 0;
			StringBuffer sb = new StringBuffer();
			while ((ch = reader.read()) != -1) {
				sb.append((char) ch);
			}
			fileReader.close();
			reader.close();
			jsonStr = sb.toString();
			return jsonStr;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ouyanglol.neo4j.vo.EventMessage;
public static List readSysJsonFile() {
		List eventList = new ArrayList<>();
		String path = JavaUtils.class.getClassLoader().getResource("file/dict.json").getPath();
		String s = readJsonFile(path);
		//JSonArray jsonArray = JSONArray.parseArray(s);
		JSonObject jsonObject = JSON.parseObject(s);
		Map parMap =  JSONObject.parseObject(jsonObject.toJSonString(), new TypeReference>(){});

		for (Map.Entry map: parMap.entrySet()) {
			String key = map.getKey();
			Map obj = (Map)map.getValue();
			
			list.add(message);
		}
		return list;
	}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5137199.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)