package com.lisq.kuaipan.oauth; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.os.Environment; import android.util.Log; public class FileUtils { private String SDPATH; // 用于存sd card的文件的路径 private final static String TAG = "kuaipan"; public String getSDPATH() { return SDPATH; } public void setSDPATH(String sDPATH) { SDPATH = sDPATH; } /** * 构造方法 获取SD卡路径 */ public FileUtils() { // 获得当前外部存储设备的目录 SDPATH = Environment.getExternalStorageDirectory() + "/"; Log.e(TAG, "sd card's directory path: " + SDPATH); } /** * 在SD卡上创建文件 * * @throws IOException */ public File createSDFile(String fileName) throws IOException { File file = new File(SDPATH + fileName); file.createNewFile(); return file; } /** * 在SD卡上创建目录 */ public File createSDDir(String dirName) { File dir = new File(SDPATH + dirName); Log.e(TAG, "storage device's state :" + Environment.getExternalStorageState()); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Log.e(TAG, "this directory real path is:" + dir.getAbsolutePath()); Log.e(TAG, "the result of making directory:" + dir.mkdir() + " it is exist? = " + dir.exists()); } return dir; } /** * 判断SD卡上的文件夹是否存在 */ public boolean isFileExist(String fileName) { File file = new File(SDPATH + fileName); Log.e(TAG, "FileUtils isFileExist fileName= " + fileName); return file.exists(); } /** * 将一个inputSteam里面的数据写入到SD卡中 */ public File write2SDFromInput(String path, String fileName, InputStream inputStream) { File file = null; OutputStream output = null; // BufferedWriter bufferedWriter; try { // 创建SD卡上的目录 File tempf = createSDDir(path); Log.e(TAG, "FileUtils write2SDFromInput directory in the sd card: = " + tempf.exists()); file = createSDFile(path + fileName); Log.e(TAG, "FileUtils write2SDFromInput file in the sd card: = " + file.exists()); output = new FileOutputStream(file); // bufferedWriter = new BufferedWriter(new FileWriter(file)); // byte buffer[] = new byte[20 * 1024]; // int filesize = inputStream.available(); // byte buffer[] = new byte[filesize]; // Log.e(TAG, "FileUtils write2SDFromInput 111= "); // int length = 0; // // length = inputStream.read(buffer); // Log.e(TAG, "FileUtils write2SDFromInput 1 length= " + length); // // while ((length = inputStream.read(buffer)) != -1) { // // Log.e(TAG, "FileUtils write2SDFromInput length= " + length); // // bufferedWriter.write(buffer, 0, length); // // } // while ((inputStream.read(buffer)) != -1) { // output.write(buffer); // } byte[] buffer = new byte[1024]; int readIndex; while (-1 != (readIndex = inputStream.read(buffer, 0, buffer.length))) { output.write(buffer, 0, readIndex); } // bufferedWriter.flush(); inputStream.close(); // bufferedWriter.close(); output.flush(); } catch (FileNotFoundException e) { Log.e(TAG, "FileUtils write2SDFromInput 222"); e.printStackTrace(); } catch (IOException e) { Log.e(TAG, "FileUtils write2SDFromInput 333"); e.printStackTrace(); } finally { try { Log.e(TAG, "FileUtils write2SDFromInput 444"); // output.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return file; } }
文件存储 读写
猜你喜欢
转载自yebingzi.iteye.com/blog/2095340
今日推荐
周排行