fileaccess.java

来自「模拟网络通信」· Java 代码 · 共 40 行

JAVA
40
字号
package com.fileAccess;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class FileAccess {
	private String path;
	public  Map readFile() throws NumberFormatException, IOException {
		HashMap<String, Integer> hm = new HashMap<String, Integer>();
		BufferedReader br = new BufferedReader(new FileReader(path));
		String str = null;
		while ((str = br.readLine()) != null) {
			String sl[] = str.split(" ");
			if ((sl[0] != null) && sl[0].equals("a_count:")) {
				hm.put(sl[0], Integer.parseInt(sl[1]));
			} else if ((sl[0] != null) && sl[0].equals("total_count:")) {
				hm.put(sl[0], Integer.parseInt(sl[1]));
			}
		}
		return hm;
	}
	public FileAccess(String path)
	{
		this.path=path;
	}
	public synchronized void writeFile(int a_count,int total_count,String path) throws FileNotFoundException
	{
		System.out.println("write a="+a_count+" t="+total_count);
		PrintWriter pw=new PrintWriter(new FileOutputStream(path),true);
		pw.println("a_count: "+a_count);
		pw.println("total_count: "+total_count);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?