在內部存儲中讀寫文件

一、寫文件

<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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章