~~~
package com.example.filerw;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.net.URL;
import org.apache.http.util.EncodingUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
public class FileUtil
{
private Context context;
public FileUtil(Context context)
{
this.context=context;
}
/**
* 读取data/data/<package name>/files 目录下的文件
* @param fileName
* @return
*/
public String readFromData(String fileName)
{
try
{
FileInputStream fis =context.openFileInput(fileName);
//图片如此操作
//InputStream in = new BufferedInputStream(fis);
//Bitmap bitmap= BitmapFactory.decodeStream(in);
byte[]buff =new byte[1024];
int hasRead=0;
StringBuilder sb =new StringBuilder("");
while((hasRead=fis.read(buff))>0)
{
sb.append(new String(buff,0,hasRead));
}
fis.close();
return sb.toString();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 将数据写入data/data/<package name>/files 目录下的文件
* @param content
* @param fileName
* @param mode
*/
public void writeToData(String content,String fileName,int mode)
{
try {
FileOutputStream fos =context.openFileOutput(fileName, mode);
PrintStream ps =new PrintStream(fos);
ps.println(content);
ps.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 读取SD卡 目录下的文件
* @param fileName
* @return
*/
public String readFromSDCard(String fileName)
{
try {
//检测SD卡是否存在
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File sdCardDir = Environment.getExternalStorageDirectory();
FileInputStream fis =new FileInputStream(sdCardDir.getCanonicalPath()+"/"+fileName);
BufferedReader br =new BufferedReader(new InputStreamReader(fis));
StringBuilder sb =new StringBuilder("");
String line=null;
while((line =br.readLine())!=null)
{
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
/**
* 接着写入SD卡目录下的文件 追加内容
* @param content
* @param FileName
* @param mode
*/
public void writeToSDCard(String content,String FileName,String mode)
{
try {
//检测SD卡是否存在
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File sdCardDir = Environment.getExternalStorageDirectory();
File targetFile =new File(sdCardDir.getCanonicalPath()+"/"+FileName);
RandomAccessFile raf =new RandomAccessFile(targetFile,mode);
raf.seek(targetFile.length());
raf.write(content.getBytes());
raf.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* fileName可以是路径加文件名
* 必须先检测SD卡是否存在
* @param fileName
* @return
* @throws IOException
*/
public String readSDFile(String fileName) throws IOException
{
if(isSDCardExists())
{
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
//如果是图片可以这样得到BitMap
// InputStream is = new BufferedInputStream(fis);
// Bitmap bitmap = BitmapFactory.decodeStream(is);
int length = fis.available();
byte [] buffer = new byte[length];
fis.read(buffer);
String res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}
else
{
return null;
}
}
/**
* 必须先检测SD卡是否存在
* fileName可以是路径加文件名
* @param fileName
* @param write_str
* @throws IOException
*/
public void writeSDFile(String fileName, String write_str) throws IOException
{
if(isSDCardExists())
{
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte [] bytes = ((String) write_str).getBytes();
fos.write(bytes);
fos.close();
}
}
/**
* 获取网络资源
* 图片 文件 。。。
* @throws IOException
*/
public void writeToSDFromURL(String s,String fileName) throws IOException
{
if(isSDCardExists())
{
URL url =new URL(s);
InputStream is =url.openStream();
//转化为Bitmap
//Bitmap bitmap =BitmapFactory.decodeStream(is);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file =new File(path+"/"+fileName);
FileOutputStream fop= new FileOutputStream(file);
OutputStream os = new BufferedOutputStream(fop);
//若写入到DATA中就如下做
//OutputStream os=context.openFileOutput(fileName, context.MODE_WORLD_READABLE);
byte [] buff=new byte[1024];
int hasRead=0;
while((hasRead=is.read(buff))>0)
{
os.write(buff,0,hasRead);
}
is.close();
os.close();
}
}
public boolean isSDCardExists()
{
try {
String s= Environment.getExternalStorageDirectory().getCanonicalPath();
if(new File(s).exists())
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return false;
}
//有关SD卡读写都要加上读写权限
//<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
// String Name = File.getName(); //获得文件或文件夹的名称:
// String parentPath = File.getParent(); //获得文件或文件夹的父目录
// String path = File.getAbsoultePath();//绝对路经
// String path = File.getPath();//相对路经
// File.createNewFile();//建立文件
// File.mkDir(); //建立文件夹
// File.isDirectory(); //判断是文件或文件夹
// File[] files = File.listFiles(); //列出文件夹下的所有文件和文件夹名
// File.renameTo(dest); //修改文件夹和文件名
// File.delete(); //删除文件夹或文件
}
~~~
文件读写总结:
SD卡中的文件使用FileInputStream和FileOutputStream进行文件的操作。
存放在数据区(/data/data/..)的文件只能使用openFileOutput和openFileInput进行操作,不能使用FileInputStream和FileOutputStream进行文件的操作。
文件操作的一些方法:
String Name = File.getName(); //获得文件或文件夹的名称:
String parentPath = File.getParent(); //获得文件或文件夹的父目录
String path = File.getAbsoultePath();//绝对路经
String path = File.getPath();//相对路经
File.createNewFile();//建立文件
File.mkDir(); //建立文件夹
File.isDirectory(); //判断是文件或文件夹
File[] files = File.listFiles(); //列出文件夹下的所有文件和文件夹名
File.renameTo(dest); //修改文件夹和文件名
File.delete(); //删除文件夹或文件