Android文件操作(可应用于JAVA)

不知道如何上传文件,只好以代码的形式发布

接口:

package evan.FileSystem.EventsInterface;

public interface IReaderData {
/*读取数据*/
void ReaderDatas(char[] buf);
/*完成读取*/
void FinishReader();
}

 

 

实现:

package evan.FileSystem;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import evan.FileSystem.EventsInterface.IReaderData;
import android.R.integer;
import android.os.*;

public class FileSystem {

private static final String systemPath = Environment
.getExternalStorageDirectory().getAbsolutePath();

// 获取Android系统的根目录:ROOT
public static String getSystemPath() {
return systemPath;
}

private String pathString = "";
private String fileName = "";
private Boolean isOverWriteFile = false;// 重写File
private IReaderData ReaderDataEvent;

/* 获取文件全路径 */
public String FileFullPath() {
return pathString + File.separator + fileName;
}

/* 路径 */
public String Path() {
return pathString;
}

/* 文件名称 */
public String FileName() {
return fileName;
}

/*
* @Description:指定文件系统进行操作 约定传入路径时,最后一个字符不以"\"或者"/"结束
*/
public FileSystem(String path, String fileName, Boolean isOverWrite) {
pathString = path;
this.fileName = fileName;
this.isOverWriteFile = isOverWrite;
}

/*
* 默认重写File
*/
public FileSystem(String path, String fileName) {
this(path, fileName, true);
}

/* 已在构造函数里定义了 */
public Boolean HasFile() {
return HasFile(FileFullPath());
}

public Boolean HasFile(String fileFullPath) {
File file = new File(fileFullPath);
if (file.isFile()) {
return file.exists();
}
return false;
}

public File CreatePath() throws IOException {
return CreatePath(Path());
}

/*
* 创建路径
*/
public File CreatePath(String path) throws IOException {
File file = new File(path + File.separator);
if (file.mkdirs())
return file;
return null;
}

public File CreaterFile() throws IOException {
return CreaterFile(FileFullPath());
}

/* 创建文件 */
public File CreaterFile(String fileFullPath) throws IOException {
File file = new File(fileFullPath);
if (file.isFile()) {
if (!HasFile(fileName) || isOverWriteFile)
file.createNewFile();
}
return file;
}

public Boolean WriteStreamToFile(InputStream inputStream)
throws FileNotFoundException, IOException {
return WriteStreamToFile(FileFullPath(), inputStream);
}

/*默认GBK*/
public InputStream StringToStream(String strMsg) throws UnsupportedEncodingException {
return StringToStream(strMsg, "GBK");
}

/* 将字符串转为流 */
public InputStream StringToStream(String strMsg,String charsetName) throws UnsupportedEncodingException {
if (strMsg == null || strMsg.length() == 0) {
return null;
}
return new ByteArrayInputStream(strMsg.getBytes(charsetName));
}

/* 将数据流写入文件中 */
public Boolean WriteStreamToFile(String fileFullPath,
InputStream inputStream) throws FileNotFoundException, IOException {
OutputStream outputStream = new FileOutputStream(
CreaterFile(fileFullPath));
byte[] buffer = new byte[1024 * 2];
Integer len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
outputStream.close();
return true;
}

public InputStream ReadFileToStream() throws FileNotFoundException {
return ReadFileToStream(FileFullPath());
}

/* 读取文档流 */
public InputStream ReadFileToStream(String fileFullPath)
throws FileNotFoundException {
InputStream inputStream = null;
if (HasFile(fileFullPath)) {
inputStream = new FileInputStream(fileFullPath);
}
return inputStream;
}

/* 默认采用GBK读取 */
public BufferedReader GetBufferedReader(InputStream inputStream)
throws UnsupportedEncodingException {
return GetBufferedReader(inputStream, "GBK");
}

public BufferedReader GetBufferedReader(InputStream inputStream,
String encoding) throws UnsupportedEncodingException {
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, encoding);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
return bufferedReader;
}

/* 设置事件 */
public void setReaderDataEvent(IReaderData ReaderDataEvent) {
this.ReaderDataEvent = ReaderDataEvent;
}

/* 需要实现IReaderData */
public void ReaderData(BufferedReader bufferedReader) throws IOException {
ReaderData(bufferedReader, true);
}

/* 需要实现IReaderData */
public void ReaderData(BufferedReader bufferedReader, Boolean isCloseStream)
throws IOException {
if (this.ReaderDataEvent == null) {
return;
}
int bufLen=1024 * 4;
char[] buf = new char[bufLen];
int len = -1;
while ((len = bufferedReader.read(buf)) != -1) {
if (len == bufLen) {
ReaderDataEvent.ReaderDatas(buf);
} else {
char[] lastbuf = new char[len];
System.arraycopy(buf, 0, lastbuf, 0, len);
ReaderDataEvent.ReaderDatas(lastbuf);
}
}
this.ReaderDataEvent.FinishReader();
if (isCloseStream) {
bufferedReader.close();
}
}

}

 

调用:

FileSystem fileSystem = new FileSystem(FileSystem.getSystemPath(),
"test.txt");
fileSystem.setReaderDataEvent(new ReaderData());
try {
fileSystem
.WriteStreamToFile(fileSystem.StringToStream("我是EvanLee"));

fileSystem.ReaderData(fileSystem.GetBufferedReader(fileSystem
.ReadFileToStream()));
} catch (Exception e) {
e.printStackTrace();
}
    public class ReaderData implements IReaderData {

@Override
public void ReaderDatas(char[] buf) {
charList.add(buf);
// 读取的数据
Log.d("msg", String.valueOf(buf));
System.out.println(buf);
}

@Override
public void FinishReader() {
//TODO
}
}

 


 

 

 

 

你可能感兴趣的:(android)