android .txt文件的写入,读取,还有复制图片
txt文件的写入:
1 private void save() { 2 FileOutputStream fos = null; 3 String state = Environment.getExternalStorageState();// sd状态 4 if (state.equals(Environment.MEDIA_MOUNTED)) {// 判断sd卡是否可用 5 File root = Environment.getExternalStorageDirectory(); 6 File targeDir = new File(root, super.getPackageName());// 要把文件写相应在activity下 7 8 if (!targeDir.exists()) {// 判断目录是否存在 9 targeDir.mkdir();// 创建 10 } 11 // String newFile=file.toString()+"/"+"user.txt"; 12 // IO操作 13 // 流 14 try { 15 // 括号里是路径 16 fos = new FileOutputStream(new File(targeDir, "userinfo.txt"));// 输出流 17 String content = name; 18 /* 19 * for(int i=0;i<content.length();i++){ 20 * fos.write(content.charAt(i));//一个个字节输 } 21 */ 22 byte[] data = content.getBytes(); 23 fos.write(data); 24 25 } catch (FileNotFoundException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } catch (IOException e) { 29 // 输入输出都会引发IO异常 30 e.printStackTrace(); 31 } finally { 32 // 打开流就需要关闭 33 try { 34 fos.close(); 35 } catch (IOException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 } 40 41 } 42 }
txt文件的读取:
1 private void read() { 2 FileInputStream fis = null; 3 String state = Environment.getExternalStorageState(); 4 if (state.equals(Environment.MEDIA_MOUNTED)) { 5 File root = Environment.getExternalStorageDirectory(); 6 File file = new File(root, super.getPackageName() + "/userinfo.txt"); 7 if (file.exists()) { 8 try { 9 fis = new FileInputStream(file); 10 int len = 0; 11 StringBuilder builder = new StringBuilder(); 12 byte[] buffer = new byte[1024]; 13 14 while ((len = fis.read(buffer)) != -1) { 15 builder.append(new String(buffer, 0, len)); 16 // builder.append(buffer);//乱码 17 } 18 /* 19 * while((len=fis.read())!=-1){ builder.append((char)len); } 20 */ 21 Toast.makeText(getApplicationContext(), builder.toString(), 22 Toast.LENGTH_SHORT).show(); 23 text2.setText(builder.toString()); 24 25 } catch (FileNotFoundException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 Log.i("read", e.getMessage()); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } finally { 33 try { 34 fis.close(); 35 } catch (IOException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 } 40 } 41 } 42 }
图片的复制:
先在assets文件夹下建一个image文件夹,里面放一张图片
1 private void copy() { 2 FileOutputStream output = null; 3 InputStream source = null; 4 String state = Environment.getExternalStorageState(); 5 if (state.equals(Environment.MEDIA_MOUNTED)) { 6 // inputstream=super.getResources().openRawResource(R.drawable.ic_launcher); 7 8 try { 9 AssetManager manager = super.getAssets(); 10 source = manager.open("image/ic_launcher.png"); 11 File target = new File( 12 Environment.getExternalStorageDirectory() + "/" 13 + super.getPackageName() + "/mycopyimg.png"); 14 output = new FileOutputStream(target); 15 16 int len = 0; 17 byte[] buffer = new byte[1024]; 18 while ((len = source.read(buffer)) != -1) { 19 output.write(buffer, 0, len); 20 } 21 } catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 Log.i("111", e.getMessage()); 25 } finally { 26 try { 27 source.close(); 28 output.close(); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 } 35 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。