您的位置:首页 > 移动开发 > Android开发

android 文件创建、读、写、删除总结

2016-06-01 18:30 525 查看
接触Android 这么久发现对文件的读写还没有理解好,接下来就把常用的总结如下:

Android是有内置外置sd卡之说,此处的造作都是针对内置sd卡来说。

1、在内置sd卡路径下创建文件夹并在这个文件夹下建个txt文件

public File createSDFile(String fileName) {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Test");
// 在file下建fileName文件
File file2 = new File(file, fileName);
try {
if (!file.exists()) {
file.mkdir();// 创建文件夹
String path = file.getPath();
System.out.println("文件夹路径:" + path);
}
if (!file2.exists()) {
file2.createNewFile();// 创建文件
}
} catch (IOException e) {
e.printStackTrace();
}
return file;
}

2、在上面创建的文件写入内容

// sd写入文件

public void writeFileinSD(String message, String fileName) {
try {
FileWriter fileWriter = new FileWriter(Environment.getExternalStorageDirectory().getPath()

+ "/Test" + "//"+ fileName);
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Test" + "//" + fileName);
fileWriter.write(message);
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeShort(2);
dos.writeUTF("");
fileWriter.flush();
fileWriter.close();

} catch (IOException e) {
e.printStackTrace();
}
}

3、删除文件夹和下面文件

public void deleteFile(File file) {
if (file.isFile()) {
file.delete();
return ;
}
if (file.isDirectory()) {
File[] childFiles = file.listFiles();
//空文件夹直接删除
if (childFiles == null || childFiles.length == 0) {
file.delete();
return;
}
//非空循环删除
for (int i = 0; i < childFiles.length; i++) {
deleteFile(childFiles[i]);
}
file.delete();
}
}

4、删除文件夹下指定文件

public boolean deleteSDFile(String fileName) {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Test" + "//" + fileName);
if (file == null || !file.exists() || file.isDirectory()) {
return false;
}
return file.delete();
}

5、私有文件夹下的文件存(/data/data/包名/files)

public void writeFileData(String fileName, String message) {
try {
FileOutputStream fout = mContext.openFileOutput(fileName,
mContext.MODE_PRIVATE);
byte[] bytes = message.getBytes();
fout.write(bytes);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}

6、私有文件夹下的文件读(/data/data/包名/files)

public String readFileData(String fileName) {
String res = "";
try {
FileInputStream fin = mContext.openFileInput(fileName);
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}

7、从assets读取文件
public String readFromAssets(String fileName) {
String result = "";
try {
InputStream is = mContext.getResources().getAssets().open(fileName);
int length = is.available();// 实际可读字节数总大小
byte[] buffer = new byte[length];
is.read(buffer);
result = EncodingUtils.getString(buffer, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

8、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)

public String readFromRaw(int fileInRaw) {
String res = "";
try {
InputStream in = mContext.getResources().openRawResource(fileInRaw);
int length = in.available();
byte[] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "GBK");
// res = new String(buffer,"GBK");
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息