package com.**; import android.content.Context; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; /** * Created by hr on 2016/8/22. */ public class CopyFile2SDCard { private Context context; // private String srcFilePath;//源文件路径 // private String targetFilePath;//目标路径 // private String fileName;//文件名 //String srcFilePath,String targetFilePath,String fileName public CopyFile2SDCard(Context context) { this.context = context; // this.srcFilePath= srcFilePath; // this.fileName= fileName; // this.targetFilePath = targetFilePath; // if(!isExits()){ // writeFile2SDCard(srcFilePath,targetFilePath,fileName); // } } /** * 写文件到sdcard * @param srcFilePath 源文件路径 * @param targetFilePath 目标文件路径 * @param fileName 文件名 */ public void writeFile2SDCard(String srcFilePath,String targetFilePath,String fileName){ InputStream in =null; try { in = new FileInputStream(srcFilePath); File file =new File(targetFilePath); if(!file.exists()){ file.mkdirs(); } FileOutputStream out = new FileOutputStream(targetFilePath+fileName); byte[] buffer =new byte[1024]; int count=0; while ((count=in.read(buffer))>0){ out.write(buffer,0,count); } out.flush(); out.close(); in.close(); }catch (Exception e){ e.printStackTrace(); } } /** * 判断文件是否存在 * @return */ public boolean isExits(String targetFilePath,String fileName){ File file = new File(targetFilePath+fileName); if(file.exists()){ return true; }else { return false; } } /** * 讲一个字符串的内容写出到sdcard 进行保存 * @param s * @param targetPath * @param fileName */ public void writeString2SDCard(String s,String targetPath,String fileName){ File file = new File(targetPath); if(!file.exists()){ file.mkdirs(); } try { FileOutputStream out = new FileOutputStream(targetPath+ fileName); out.write(s.getBytes(),0,s.length()); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }
写文件到sdcard
最新推荐文章于 2017-11-08 11:05:07 发布