在内部存储中读写文件

一、写文件

<span style="white-space:pre">		</span>//data/data/com.xxx:这就是内部存储空间的路径
    		File file = new File("data/data/com.xxx/info.txt");
    		FileOutputStream fos;
			try {
				fos = new FileOutputStream(file);
				fos.write((name + "##" + pass).getBytes());
				fos.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

二、读文件

    	File file = new File("data/data/com.itheima.rwinrom/info.txt");
    	if(file.exists()){
	    	try {
				FileInputStream fis = new FileInputStream(file);
				//把字节流转换成字符流
				BufferedReader br = new BufferedReader(new InputStreamReader(fis));
				//读取txt文件里的用户名和密码
				String text = br.readLine();
				String[] s = text.split("##");
				
		    	et_name.setText(s[0]);
		    	et_pass.setText(s[1]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}

三、通过api来获得文件路径

    		//返回一个File对象,其路径是data/data/com.xxx/files
//    		File file = new File(getFilesDir(), "info.txt");
    		
    		//返回值也是一个File对象,其路径是data/data/com.xxx/cache
    		File file = new File(getCacheDir(), "info.txt");


发布了51 篇原创文章 · 获赞 0 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章