Android读写SD卡中的txt文件

private String readTxt(String filepath){
		File file = new File(filepath);
		String result = "";
		if (file.exists()) {
			try {
				InputStream in = new BufferedInputStream(new FileInputStream(file));
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				StringBuffer sb = new StringBuffer();
				String temp;
				while ((temp = br.readLine()) != null) {
					sb.append(temp);
				}
				result = sb.toString();
				br.close();
				in.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}	
		return result;
	}
	
	private void save2txt(String msg, String path){
		if (null == msg || TextUtils.isEmpty(msg)) {
			return;
		}
		File file = new File(path);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			RandomAccessFile rf = new RandomAccessFile(file, "rw");
			long len = rf.length();
			rf.seek(len);
			rf.writeBytes(msg + "\n");
			rf.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_24179679/article/details/73340986
今日推荐