create File In SDCard : SDCard « Hardware « Android






create File In SDCard

  
//package ping.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.os.Environment;

public class FileUtils {
  private String SDCardRoot;

  public FileUtils() {
    SDCardRoot = Environment.getExternalStorageDirectory()
        .getAbsolutePath()
        + "/";
    System.out.println("SD dir:" + SDCardRoot);
  }

  public File createFileInSDCard(String fileName, String dir)
      throws IOException {
    File file = new File(SDCardRoot + dir + File.separator + fileName);
    file.createNewFile();
    return file;
  }

  public File createSDDir(String dir) {
    File dirFile = new File(SDCardRoot + dir + File.separator);
    System.out.println("creat dir:" + dirFile.mkdirs());
    return dirFile;
  }

  public boolean isFileExist(String fileName, String path) {
    File file = new File(SDCardRoot + path + File.separator + fileName);
    return file.exists();
  }

  public File write2SDFromInput(String path, String fileName,
      InputStream input) {
    File file = null;
    OutputStream output = null;
    try {
      createSDDir(path);
      file = createFileInSDCard(fileName, path);
      output = new FileOutputStream(file);
      byte buffer[] = new byte[4 * 1024];
      int temp;
      while ((temp = input.read(buffer)) != -1) {
        output.write(buffer, 0, temp);
      }
      output.flush();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        output.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return file;
  }

}

   
    
  








Related examples in the same category

1.Load image from mounted sdcard
2.Uri for sdcard
3.Get SDCard Megs Free
4.SDCard File Util
5.export the current database to the SDcard
6.get Sd card Path