随手记一下文件读写

/**
	 * 保存文件
	 * 
	 * @param fileName
	 * @param fileContent
	 * @throws Exception
	 * Context.MODE_PRIVATE:新内容覆盖原内容
     * Context.MODE_APPEND:新内容追加到原内容后
     * Context.MODE_WORLD_READABLE:允许其他应用程序读取
     * Context.MODE_WORLD_WRITEABLE:允许其他应用程序写入,会覆盖原数据。
	 */
	public void save(String fileName, String fileContent) throws Exception {
		FileOutputStream fileOutputStream = this.context.openFileOutput(fileName, Context.MODE_APPEND);
		fileOutputStream.write(fileContent.getBytes());
	}

	/**
	 * 读取文件
	 * 
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public String read(String fileName) throws Exception {
		FileInputStream fileInputStream = this.context.openFileInput(fileName);
		ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fileInputStream.read(buffer)) > 0) {
			byteArray.write(buffer, 0, len);
		}
		;
		return byteArray.toString();
	}


你可能感兴趣的:(随手记一下文件读写)