码迷,mamicode.com
首页 > 移动开发 > 详细

Android存取txt

时间:2014-10-30 11:50:17      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:android   io   os   ar   java   文件   on   log   ad   

/** 取SD卡路径 **/
	private String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);

		// 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();
			// 获取根目录
		}
		if (sdDir != null) {
			return sdDir.toString();
		} else {
			return "";
		}
	}

	/** 保存密码文件路径 **/
	private String getDirectory() {
		String dir = getSDPath() + "/" + "QQTSAVE";
		File file = new File(dir);
		if (!file.exists()) {
			try {
				file.mkdirs();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dir;
	}

	private void toSave(String user, String password) {
		File file = new File(getDirectory() + "/" + "us.txt");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {
			FileWriter fw = new FileWriter(file);
			fw.write(user + "asd" + password);
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String Read() {
		String sb = "";
		
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(getDirectory() + "/" + "us.txt"));
			String line = "";
			while ((line = br.readLine()) != null) {
				sb=line;
			}
			br.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.e("--------------", sb);
		return sb;
	}
	public void deleteSave(String user){
		File file = new File(getDirectory() + "/" + "us.txt");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {
			FileWriter fw = new FileWriter(file);
			fw.write(user+"null");
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

Android存取txt

标签:android   io   os   ar   java   文件   on   log   ad   

原文地址:http://blog.csdn.net/u014765620/article/details/40615841

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!