1. 程式人生 > >java 檔案讀取,一行一行的讀取

java 檔案讀取,一行一行的讀取

public static void main(String[] args) throws IOException {
		File file = new File("c:/error.txt");
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		JsonObject json = new JsonObject();
		String readoneline = null;
		while((readoneline = br.readLine()) != null){
			String[] lines = readoneline.split("\\-");
			json.addProperty(lines[0], lines[1]);
		}
		br.close();
		isr.close();
		fis.close();
		File errorJson = new File("c:/errorJson.txt");
		if(!errorJson.exists()) errorJson.mkdir();
		FileOutputStream fos = new FileOutputStream(errorJson);
		fos.write(json.toString().getBytes());
		fos.close();
		System.out.println("OK...");
	}

主要解決的問題是:把檔案中的有對應格式的字串,轉換成json字串的檔案

核心程式碼:

String readoneline = null;
		while((readoneline = br.readLine()) != null){
			String[] lines = readoneline.split("\\-");
			json.addProperty(lines[0], lines[1]);
		}