The realization of java operation file content

1. Read specific information under the text, such as the content under [brand].

2. First, we get the file path through file, and then use BufferedReader to read the file. The code is as follows:

public static ArrayList<String> getBranch(String value){
		ArrayList<String> list = new ArrayList<String>();
		File file = new File(value);
		if (!file.exists()) {
			System.out.println("Cannot find the current file");
		}
		BufferedReader reader = null;
		String biaoti = "";
		String branch="";
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				if (tempString.startsWith("[/")) {
					biaoti = tempString.replace("[", "");
					branch = biaoti.replace("]", "");
					list.add(branch);
				}
			}
		}catch (IOException e) {
			e.printStackTrace ();
		}
		return list;
	}
3. Add content to a [brand], or add new content of a new [brand],

public static Boolean writeNewMessage(String branch,String url,String  Information){
		File file = new File(url);
		if (!file.exists()) {
			System.out.println("Cannot find the current file");
		}
		BufferedReader reader = null;
		String biaoti = "";
		ArrayList<String> beizhu = new ArrayList<String>();
		ArrayList<String> groups = new ArrayList<String>();
		Map<String,ArrayList<String>> map = new LinkedHashMap<String,ArrayList<String>>();
		BufferedWriter writer = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				if (tempString.startsWith("[/")) {
					ArrayList<String> list = new ArrayList<String>();
					biaoti = tempString;
					map.put(biaoti, list);
				} else if (tempString.startsWith("[groups]")) {
					ArrayList<String> list = new ArrayList<String>();
					biaoti = tempString;
					map.put(biaoti, list);
				} else if (tempString.startsWith("#")) {
					beizhu.add (tempString);
				} else {
					if (!"".equals(tempString.trim())) {
						ArrayList<String> s = map.get(biaoti);
						if (s!= null) {
							s.add(tempString);
							map.put(biaoti, s);
						}
					}
				}
			}
			ArrayList<String> temp = map.get("["+branch+"]");
			if(temp==null){
				ArrayList<String> temp1=new ArrayList<String>();
				temp1.add(Information);
				map.put("["+branch+"]", temp1);
			}else{
			temp.add(Information);
			map.put("["+branch+"]", temp);
			}
			System.out.println(map);
			groups = map.get("[groups]");
			writer = new BufferedWriter(new FileWriter(url));
			for(String s:beizhu){
				writer.write(s+"\n");
			}
			writer.write("\n");
			writer.write("[groups]"+"\n");
			for (String s: groups) {
				writer.write(s+"\n");
			}
			writer.write("\n");
			for (String key : map.keySet()) {
				if (!key.equals("[groups]")) {
					ArrayList<String> list1 = (ArrayList<String>)map.get(key);
					writer.write(key+"\n");
					for (String s: list1) {
						writer.write(s+"\n");
					}
					writer.write("\n");
				}
			}
			reader.close();
			
		} catch (IOException e) {
			e.printStackTrace ();
			return false;
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace ();
				return false;
			}
		}
		return true;
	}

Note: For the map here, if the order of partial addition, you can use HashMap, TreeMap, if you want the added text order to be the same as the original text, then use LinkedHashMap.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325592378&siteId=291194637