java操作文件的内容的实现

1.读取文本下的特定信息,如[brand]下的内容。

2.首先我们通过file获取文件路径,然后使用BufferedReader进行文件的读取文件,代码如下:

public static ArrayList<String> getBranch(String value){
		ArrayList<String> list = new ArrayList<String>();
		File file = new File(value);
		if (!file.exists()) {
			System.out.println("找不到当前文件");
		}
		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.往某个[brand]下添加内容,或者添加新的[brand]的新内容,

public static Boolean writeNewMessage(String branch,String url,String  Information){
		File file = new File(url);
		if (!file.exists()) {
			System.out.println("找不到当前文件");
		}
		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;
	}

注:这里的map,如果部分添加的先后顺序,可以用HashMap,TreeMap,如果想添加后的文本顺序与原文本一样,那就用LinkedHashMap。

猜你喜欢

转载自blog.csdn.net/hbl6016/article/details/51026568