文件讀取爲字符串,字符串存儲爲文件

將外部文件讀取成爲字符串

public String doPageToStr(String filename) {
		StringBuffer buf = new StringBuffer();
		BufferedReader breader = null;
		try {
			breader = new BufferedReader(
					new FileReader(filename));
			while (breader.ready()) {
				buf.append((char) breader.read());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (breader != null) {
				try {
					breader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return buf.toString();
	}

 將字符串存儲爲文件

public void doTask(String strPage, String basePath, ProductDownload product) {
		synchronized (lock) {
			String text = strPage;
			String path = basePath;
			String filePath = path + File.separator + "product"
					+ product.getProductId() + File.separator + "product.html";
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(filePath);
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
						fos));
				bw.write(text);
				bw.flush();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (fos != null) {
					try {
						fos.close();
					} catch (Exception e) {
					}
				}
			}
		}
	}

 

 

發佈了75 篇原創文章 · 獲贊 2 · 訪問量 3221
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章