文件保存方式:固定路径、根据上下文自动存储、将文件保存到sd卡
1.固定路径保存:
1 // 将文件保存到固定路径中 2 try { 3 String result = username + "##" + userpwd; 4 // 创建file类指定我们要把数据存储的位置 5 File file = new File("data/data/com.hollysource.login/info.txt"); 6 // 创建一个文件的输出流 7 FileOutputStream fos = new FileOutputStream(file); 8 fos.write(result.getBytes()); 9 fos.close(); 10 return true; 11 } catch (Exception e) { 12 // TODO: handle exception 13 e.printStackTrace(); 14 return false; 15
1 // 读取保存的在固定路径中的文件 2 try { 3 Map<String, String> maps = new HashMap<String, String>(); 4 File file = new File("data/data/com.hollysource.login/info.txt"); 5 FileInputStream fis = new FileInputStream(file); 6 BufferedReader buf = new BufferedReader(new InputStreamReader(fis)); 7 String content = buf.readLine(); // 读取数据 8 9 // 切割字符串 10 String[] splits = content.split("##"); 11 String name = splits[0]; 12 String pwd = splits[1]; 13 14 maps.put("name", name); 15 maps.put("pwd", pwd); 16 fis.close(); 17 return maps; 18 19 } catch (Exception e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 return null; 23 }
2.根据上下文自动存储文件
1 // 根据上下文保存 2 public static boolean saveInfo(Context context, String username,String userpwd){ 3 try { 4 5 // String path = context.getFilesDir().getPath(); 6 7 String result = username + "##" + userpwd; 8 // 创建file类指定我们要把数据存储的位置 9 // File file = new File("data/data/com.hollysource.login/info.txt"); 10 // File file = new File(path,"info.txt"); 11 // // 创建一个文件的输出流 12 // FileOutputStream fos = new FileOutputStream(file); 13 FileOutputStream fos = context.openFileOutput("infoo.txt", 0); 14 fos.write(result.getBytes()); 15 fos.close(); 16 return true; 17 } catch (Exception e) { 18 // TODO: handle exception 19 e.printStackTrace(); 20 return false; 21 } 22 }
1 // 根据上下文读取 2 public static Map<String,String>readInfo (Context context){ 3 try { 4 Map<String, String> maps = new HashMap<String, String>(); 5 // String path = context.getFilesDir().getPath(); 6 // File file = new File(path,"info.txt"); 7 // FileInputStream fis = new FileInputStream(file); 8 FileInputStream fis = context.openFileInput("infoo.txt"); 9 BufferedReader buf = new BufferedReader(new InputStreamReader(fis)); 10 String content = buf.readLine(); // 读取数据 11 12 // 切割字符串 13 String[] splits = content.split("##"); 14 String name = splits[0]; 15 String pwd = splits[1]; 16 17 maps.put("name", name); 18 maps.put("pwd", pwd); 19 fis.close(); 20 return maps; 21 22 } catch (Exception e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 return null; 26 }
context是调用方法时传的上下文:MainActivity.this;
3.将文件保存到sd卡中:
1>.判断sd的状态:
1 // 把数据保存到sd卡的时候,需要判断一下sd的状态是否可用 2 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { 3 Toast.makeText(MainActivity.this, "sd卡可用", 0).show(); 4 5 } else { 6 Toast.makeText(MainActivity.this,"sd卡不可用", 0).show(); 7 return; 8 }
2>.保存到sd卡:
1 public static boolean saveInfo(String username,String userpwd){ 2 try { 3 String result = username + "##" + userpwd; 4 // 把数据保存在sdcard中 5 String sdPath = Environment.getExternalStorageDirectory().getPath(); 6 File file = new File(sdPath,"haha.txt"); 7 // 创建一个文件的输出流 8 FileOutputStream fos = new FileOutputStream(file); 9 fos.write(result.getBytes()); 10 fos.close(); 11 return true; 12 } catch (Exception e) { 13 // TODO: handle exception 14 e.printStackTrace(); 15 return false; 16 } 17 }
3>.读取sd卡中的文件:
1 public static Map<String,String>readInfo (){ 2 try { 3 Map<String, String> maps = new HashMap<String, String>(); 4 String sdPath = Environment.getExternalStorageDirectory().getPath();//获取路径 5 File file = new File(sdPath,"haha.txt"); 6 FileInputStream fis = new FileInputStream(file); 7 BufferedReader buf = new BufferedReader(new InputStreamReader(fis)); 8 String content = buf.readLine(); // 读取数据 9 10 // 切割字符串 11 String[] splits = content.split("##"); 12 String name = splits[0]; 13 String pwd = splits[1]; 14 15 maps.put("name", name); 16 maps.put("pwd", pwd); 17 fis.close(); 18 return maps; 19 20 } catch (Exception e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 return null; 24 } 25 }
public static Map<String,String>readInfo (Context context){try {Map<String, String> maps = new HashMap<String, String>();//String path = context.getFilesDir().getPath();//File file = new File(path,"info.txt");//FileInputStream fis = new FileInputStream(file);FileInputStream fis = context.openFileInput("infoo.txt");BufferedReader buf = new BufferedReader(new InputStreamReader(fis));String content = buf.readLine(); // 读取数据// 切割字符串String[] splits = content.split("##");String name = splits[0];String pwd = splits[1];maps.put("name", name);maps.put("pwd", pwd);fis.close();return maps;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}

浙公网安备 33010602011771号