android 对SD卡文件的I/O操作

public class FileUtils {


private final static String LOG_MESSAGE = "MZZ_LOG : " ;
//得到手机SD卡的目录
private final static String SDPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

//创建一个新的文件目录
public File createSDFile(String fileName) throws IOException{
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}

//创建一个新的目录
public File createSDDir(String dirName){
File file = new File(SDPATH + dirName) ;
return file ;
}

//判断目录是否存在
public boolean isFileExist(String path , String fileName){
File file = new File(SDPATH + path + "/" + fileName);
return file.exists() ;
}

//将非文本文件写到SD卡中
public File writeFile2SDFromInput(String path , String fileName , InputStream inputStream){
File file = null ;
OutputStream outputStream = null ;
try{
createSDDir(path);
file = createSDFile(path + "/" + fileName);
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[4 * 1024];
while(inputStream.read(buffer) != -1){
outputStream.write(buffer);
}
}catch(Exception e){
e.printStackTrace();
}

return file;
}

//将文本文件写到SD卡中
public File writeText2SDFromInput(String path , String fileName , InputStream inputStream){
File file = null ;
StringBuffer sb = new StringBuffer() ;
String temp = null ;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream , "GB2312"));
createSDDir(path);
file = createSDFile(path +"/" + fileName);
FileOutputStream os = new FileOutputStream(file,false);
OutputStreamWriter bw = new OutputStreamWriter(os,"GB2312");
while((temp = br.readLine()) != null){
sb.append(temp);
sb.append("\n");
}
bw.flush();
bw.write(sb.toString() , 1 , sb.toString().length()-1);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(LOG_MESSAGE + e);
}

return file ;
}


}

你可能感兴趣的:(android,exception,String,File,buffer,Path)