- android的文件操作要有权限:
- view plaincopy to clipboardprint?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> - SD卡下的文件操作:
- 1、判断SD卡是否插入
- view plaincopy to clipboardprint?
Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); - 2、获得sd卡根目录:
- view plaincopy to clipboardprint?
File skRoot = Environment.getExternalStorageDirectory();
File skRoot = Environment.getExternalStorageDirectory(); - 私有目录下的文件操作:
- 1、获得私有根目录:
- view plaincopy to clipboardprint?
File fileRoot = Context.getFilesDir()+"\";
File fileRoot = Context.getFilesDir()+"\"; - 还未整理
- 文件夹或文件夹操作:
- 1、确定或获得文件夹和文件路径
- a、获得文件或文件夹的绝对路径和相对路径。区别
- view plaincopy to clipboardprint?
String path = File.getPath();//相对
String path = File.getAbsoultePath();//绝对
String path = File.getPath();//相对
String path = File.getAbsoultePath();//绝对 - b 、获得文件或文件夹的父目录
- view plaincopy to clipboardprint?
String parentPath = File.getParent();
String parentPath = File.getParent(); - c、获得文件或文件夹的名称:
- view plaincopy to clipboardprint?
String Name = File.getName();
String Name = File.getName(); - 2、建立文件或文件夹
- view plaincopy to clipboardprint?
File.mkDir(); //建立文件夹
File.createNewFile();//建立文件
File.mkDir(); //建立文件夹
File.createNewFile();//建立文件
- 3、判断是文件或文件夹
- view plaincopy to clipboardprint?
File.isDirectory()
File.isDirectory() - 4、列出文件夹下的所有文件和文件夹名
- view plaincopy to clipboardprint?
File[] files = File.listFiles();
File[] files = File.listFiles(); - 5、修改文件夹和文件名
- view plaincopy to clipboardprint?
File.renameTo(dest);
File.renameTo(dest); - 6、删除文件夹或文件
- view plaincopy to clipboardprint?
File.delete();
File.delete(); - view plaincopy to clipboardprint?
package otheri.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import otheri.io.Input;
import otheri.io.Output;
import android.content.Context;
import android.os.Environment;
public class FileHelper {
private Context context;
private String SDPATH;
private String FILESPATH;
public FileHelper(Context context) {
this.context = context;
SDPATH = Environment.getExternalStorageDirect ory().getPath() + "\";
FILESPATH = this.context.getFilesDir().getPath() + "\";
}
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
public boolean delSDFile(String fileName) {
File file = new File(SDPATH + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
file.delete();
return true;
}
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
public boolean delSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
return delDir(dir);
}
public boolean renameSDFile(String oldfileName, String newFileName) {
File oleFile = new File(SDPATH + oldfileName);
File newFile = new File(SDPATH + newFileName);
return oleFile.renameTo(newFile);
}
public boolean copySDFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(SDPATH + srcFileName);
File destFile = new File(SDPATH + destFileName);
return copyFileTo(srcFile, destFile);
}
public boolean copySDFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(SDPATH + srcDirName);
File destDir = new File(SDPATH + destDirName);
return copyFilesTo(srcDir, destDir);
}
public boolean moveSDFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(SDPATH + srcFileName);
File destFile = new File(SDPATH + destFileName);
return moveFileTo(srcFile, destFile);
}
public boolean moveSDFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(SDPATH + srcDirName);
File destDir = new File(SDPATH + destDirName);
return moveFilesTo(srcDir, destDir);
}
public Output writeSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file);
return new Output(fos);
}
public Output appendSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file, true);
return new Output(fos);
}
public Input readSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileInputStream fis = new FileInputStream(file);
return new Input(fis);
}
public File creatDataFile(String fileName) throws IOException {
File file = new File(FILESPATH + fileName);
file.createNewFile();
return file;
}
public File creatDataDir(String dirName) {
File dir = new File(FILESPATH + dirName);
dir.mkdir();
return dir;
}
public boolean delDataFile(String fileName) {
File file = new File(FILESPATH + fileName);
return delFile(file);
}
public boolean delDataDir(String dirName) {
File file = new File(FILESPATH + dirName);
return delDir(file);
}
public boolean renameDataFile(String oldName, String newName) {
File oldFile = new File(FILESPATH + oldName);
File newFile = new File(FILESPATH + newName);
return oldFile.renameTo(newFile);
}
public boolean copyDataFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(FILESPATH + srcFileName);
File destFile = new File(FILESPATH + destFileName);
return copyFileTo(srcFile, destFile);
}
public boolean copyDataFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(FILESPATH + srcDirName);
File destDir = new File(FILESPATH + destDirName);
return copyFilesTo(srcDir, destDir);
}
public boolean moveDataFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(FILESPATH + srcFileName);
File destFile = new File(FILESPATH + destFileName);
return moveFileTo(srcFile, destFile);
}
public boolean moveDataFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(FILESPATH + srcDirName);
File destDir = new File(FILESPATH + destDirName);
return moveFilesTo(srcDir, destDir);
}
public Output wirteFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName,
Context.MODE_WORLD_WRITEABLE);
return new Output(os);
}
public Output appendFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND);
return new Output(os);
}
public Input readFile(String fileName) throws IOException {
InputStream is = context.openFileInput(fileName);
return new Input(is);
}
*/
public boolean delFile(File file) {
if (file.isDirectory())
return false;
return file.delete();
}
public boolean delDir(File dir) {
if (dir == null || !dir.exists() || dir.isFile()) {
return false;
}
for (File file : dir.listFiles()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
delDir(file);// 递归
}
}
dir.delete();
return true;
}
public boolean copyFileTo(File srcFile, File destFile) throws IOException {
if (srcFile.isDirectory() || destFile.isDirectory())
return false;// 判断是否是文件
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int readLen = 0;
byte[] buf = new byte[1024];
while ((readLen = fis.read(buf)) != -1) {
fos.write(buf, 0, readLen);
}
fos.flush();
fos.close();
fis.close();
return true;
}
public boolean copyFilesTo(File srcDir, File destDir) throws IOException {
if (!srcDir.isDirectory() || !destDir.isDirectory())
return false;// 判断是否是目录
if (!destDir.exists())
return false;// 判断目标目录是否存在
File[] srcFiles = srcDir.listFiles();
for (int i = 0; i < srcFiles.length; i++) {
if (srcFiles[i].isFile()) {
// 获得目标文件
File destFile = new File(destDir.getPath() + "\"
+ srcFiles[i].getName());
copyFileTo(srcFiles[i], destFile);
} else if (srcFiles[i].isDirectory()) {
File theDestDir = new File(destDir.getPath() + "\"
+ srcFiles[i].getName());
copyFilesTo(srcFiles[i], theDestDir);
}
}
return true;
}
public boolean moveFileTo(File srcFile, File destFile) throws IOException {
boolean iscopy = copyFileTo(srcFile, destFile);
if (!iscopy)
return false;
delFile(srcFile);
return true;
}
public boolean moveFilesTo(File srcDir, File destDir) throws IOException {
if (!srcDir.isDirectory() || !destDir.isDirectory()) {
return false;
}
File[] srcDirFiles = srcDir.listFiles();
for (int i = 0; i < srcDirFiles.length; i++) {
if (srcDirFiles[i].isFile()) {
File oneDestFile = new File(destDir.getPath() + "\"
+ srcDirFiles[i].getName());
moveFileTo(srcDirFiles[i], oneDestFile);
delFile(srcDirFiles[i]);
} else if (srcDirFiles[i].isDirectory()) {
File oneDestFile = new File(destDir.getPath() + "\"
+ srcDirFiles[i].getName());
moveFilesTo(srcDirFiles[i], oneDestFile);
delDir(srcDirFiles[i]);
}
}
return true;
}
}
package otheri.common; - import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; - import otheri.io.Input;
import otheri.io.Output;
import android.content.Context;
import android.os.Environment; - public class FileHelper {
private Context context;
private String SDPATH;
private String FILESPATH; -
public FileHelper(Context context) {
this.context = context;
SDPATH = Environment.getExternalStorageDirect ory().getPath() + "\";
FILESPATH = this.context.getFilesDir().getPath() + "\";
} -
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
} -
public boolean delSDFile(String fileName) {
File file = new File(SDPATH + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
file.delete();
return true;
} -
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
} -
public boolean delSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
return delDir(dir);
} -
public boolean renameSDFile(String oldfileName, String newFileName) {
File oleFile = new File(SDPATH + oldfileName);
File newFile = new File(SDPATH + newFileName);
return oleFile.renameTo(newFile);
} -
public boolean copySDFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(SDPATH + srcFileName);
File destFile = new File(SDPATH + destFileName);
return copyFileTo(srcFile, destFile);
} -
public boolean copySDFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(SDPATH + srcDirName);
File destDir = new File(SDPATH + destDirName);
return copyFilesTo(srcDir, destDir);
} -
public boolean moveSDFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(SDPATH + srcFileName);
File destFile = new File(SDPATH + destFileName);
return moveFileTo(srcFile, destFile);
} -
public boolean moveSDFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(SDPATH + srcDirName);
File destDir = new File(SDPATH + destDirName);
return moveFilesTo(srcDir, destDir);
} -
public Output writeSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file);
return new Output(fos);
} -
public Output appendSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file, true);
return new Output(fos);
} -
public Input readSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileInputStream fis = new FileInputStream(file);
return new Input(fis);
}
public File creatDataFile(String fileName) throws IOException {
File file = new File(FILESPATH + fileName);
file.createNewFile();
return file;
} -
public File creatDataDir(String dirName) {
File dir = new File(FILESPATH + dirName);
dir.mkdir();
return dir;
} -
public boolean delDataFile(String fileName) {
File file = new File(FILESPATH + fileName);
return delFile(file);
} -
public boolean delDataDir(String dirName) {
File file = new File(FILESPATH + dirName);
return delDir(file);
} -
public boolean renameDataFile(String oldName, String newName) {
File oldFile = new File(FILESPATH + oldName);
File newFile = new File(FILESPATH + newName);
return oldFile.renameTo(newFile);
} -
public boolean copyDataFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(FILESPATH + srcFileName);
File destFile = new File(FILESPATH + destFileName);
return copyFileTo(srcFile, destFile);
} -
public boolean copyDataFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(FILESPATH + srcDirName);
File destDir = new File(FILESPATH + destDirName);
return copyFilesTo(srcDir, destDir);
} -
public boolean moveDataFileTo(String srcFileName, String destFileName)
throws IOException {
File srcFile = new File(FILESPATH + srcFileName);
File destFile = new File(FILESPATH + destFileName);
return moveFileTo(srcFile, destFile);
} -
public boolean moveDataFilesTo(String srcDirName, String destDirName)
throws IOException {
File srcDir = new File(FILESPATH + srcDirName);
File destDir = new File(FILESPATH + destDirName);
return moveFilesTo(srcDir, destDir);
} -
public Output wirteFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName,
Context.MODE_WORLD_WRITEABLE);
return new Output(os);
} -
public Output appendFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND);
return new Output(os);
} -
public Input readFile(String fileName) throws IOException {
InputStream is = context.openFileInput(fileName);
return new Input(is);
}
*/
public boolean delFile(File file) {
if (file.isDirectory())
return false;
return file.delete();
} -
public boolean delDir(File dir) {
if (dir == null || !dir.exists() || dir.isFile()) {
return false;
}
for (File file : dir.listFiles()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
delDir(file);// 递归
}
}
dir.delete();
return true;
} -
public boolean copyFileTo(File srcFile, File destFile) throws IOException {
if (srcFile.isDirectory() || destFile.isDirectory())
return false;// 判断是否是文件
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int readLen = 0;
byte[] buf = new byte[1024];
while ((readLen = fis.read(buf)) != -1) {
fos.write(buf, 0, readLen);
}
fos.flush();
fos.close();
fis.close();
return true;
} -
public boolean copyFilesTo(File srcDir, File destDir) throws IOException {
if (!srcDir.isDirectory() || !destDir.isDirectory())
return false;// 判断是否是目录
if (!destDir.exists())
return false;// 判断目标目录是否存在
File[] srcFiles = srcDir.listFiles();
for (int i = 0; i < srcFiles.length; i++) {
if (srcFiles[i].isFile()) {
// 获得目标文件
File destFile = new File(destDir.getPath() + "\"
+ srcFiles[i].getName());
copyFileTo(srcFiles[i], destFile);
} else if (srcFiles[i].isDirectory()) {
File theDestDir = new File(destDir.getPath() + "\"
+ srcFiles[i].getName());
copyFilesTo(srcFiles[i], theDestDir);
}
}
return true;
} -
public boolean moveFileTo(File srcFile, File destFile) throws IOException {
boolean iscopy = copyFileTo(srcFile, destFile);
if (!iscopy)
return false;
delFile(srcFile);
return true;
} -
public boolean moveFilesTo(File srcDir, File destDir) throws IOException {
if (!srcDir.isDirectory() || !destDir.isDirectory()) {
return false;
}
File[] srcDirFiles = srcDir.listFiles();
for (int i = 0; i < srcDirFiles.length; i++) {
if (srcDirFiles[i].isFile()) {
File oneDestFile = new File(destDir.getPath() + "\"
+ srcDirFiles[i].getName());
moveFileTo(srcDirFiles[i], oneDestFile);
delFile(srcDirFiles[i]);
} else if (srcDirFiles[i].isDirectory()) {
File oneDestFile = new File(destDir.getPath() + "\"
+ srcDirFiles[i].getName());
moveFilesTo(srcDirFiles[i], oneDestFile);
delDir(srcDirFiles[i]);
} -
}
return true;
}
}
- getPath与getAbsoultePath的区别:
- getAbsolutePath():返回抽象路径名的绝对路径名字符串。
- view plaincopy to clipboardprint?
public static void test1(){
File file1 = new File(".\\test1.txt");
File file2 = new File("D:\\workspace\\test\\test1.txt");
System.out.println("-----默认相对路径:取得路径不同------");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
}
-----默认相对路径:取得路径不同------
.\test1.txt
D:\workspace\test\.\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
----------------------------------------------------
public static void test2() throws Exception{
File file = new File("..\\src\\test1.txt");
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
}
D:\workspace\test\..\src\test1.txt
D:\workspace\src\test1.txt
--------------------------------------------
public static void test3() throws Exception{
File file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
(引自: http://blog.csdn.net/fenghome/archive/2010/06/13/5668598.aspx)