android的文件操作。(未整理完成)
2014-09-21 17:15
274 查看
原文地址:/article/9075535.html
android的文件操作要有权限:
[xhtml] view
plaincopy
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
SD卡下的文件操作:
1、判断SD卡是否插入
[java] view
plaincopy
Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
2、获得sd卡根目录:
[java] view
plaincopy
File skRoot = Environment.getExternalStorageDirectory();
私有目录下的文件操作:
1、获得私有根目录:
[java] view
plaincopy
File fileRoot = Context.getFilesDir()+"//";
还未整理
文件夹或文件夹操作:
1、确定或获得文件夹和文件路径
a、获得文件或文件夹的绝对路径和相对路径。区别
[java] view
plaincopy
String path = File.getPath();//相对
String path = File.getAbsoultePath();//绝对
b 、获得文件或文件夹的父目录
[java] view
plaincopy
String parentPath = File.getParent();
c、获得文件或文件夹的名称:
[java] view
plaincopy
String Name = File.getName();
2、建立文件或文件夹
[java] view
plaincopy
File.mkDir(); //建立文件夹
File.createNewFile();//建立文件
3、判断是文件或文件夹
[java] view
plaincopy
File.isDirectory()
4、列出文件夹下的所有文件和文件夹名
[java] view
plaincopy
File[] files = File.listFiles();
5、修改文件夹和文件名
[java] view
plaincopy
File.renameTo(dest);
6、删除文件夹或文件
[java] view
plaincopy
File.delete();
[java] view
plaincopy
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.getExternalStorageDirectory().getPath() + "//";
FILESPATH = this.context.getFilesDir().getPath() + "//";
}
/**
* 在SD卡上创建文件
*
* @throws IOException
*/
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 删除SD卡上的文件
*
* @param fileName
*/
public boolean delSDFile(String fileName) {
File file = new File(SDPATH + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
file.delete();
return true;
}
/**
* 在SD卡上创建目录
*
* @param dirName
*/
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 删除SD卡上的目录
*
* @param dirName
*/
public boolean delSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
return delDir(dir);
}
/**
* 修改SD卡上的文件或目录名
*
* @param fileName
*/
public boolean renameSDFile(String oldfileName, String newFileName) {
File oleFile = new File(SDPATH + oldfileName);
File newFile = new File(SDPATH + newFileName);
return oleFile.renameTo(newFile);
}
/**
* 拷贝SD卡上的单个文件
*
* @param path
* @throws IOException
*/
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);
}
/**
* 拷贝SD卡上指定目录的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/**
* 移动SD卡上的单个文件
*
* @param srcFileName
* @param destFileName
* @return
* @throws IOException
*/
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);
}
/**
* 移动SD卡上的指定目录的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/*
* 将文件写入sd卡。如:writeSDFile("test.txt");
*/
public Output writeSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file);
return new Output(fos);
}
/*
* 在原有文件上继续写文件。如:appendSDFile("test.txt");
*/
public Output appendSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file, true);
return new Output(fos);
}
/*
* 从SD卡读取文件。如:readSDFile("test.txt");
*/
public Input readSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileInputStream fis = new FileInputStream(file);
return new Input(fis);
}
/**
* 建立私有文件
*
* @param fileName
* @return
* @throws IOException
*/
public File creatDataFile(String fileName) throws IOException {
File file = new File(FILESPATH + fileName);
file.createNewFile();
return file;
}
/**
* 建立私有目录
*
* @param dirName
* @return
*/
public File creatDataDir(String dirName) {
File dir = new File(FILESPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 删除私有文件
*
* @param fileName
* @return
*/
public boolean delDataFile(String fileName) {
File file = new File(FILESPATH + fileName);
return delFile(file);
}
/**
* 删除私有目录
*
* @param dirName
* @return
*/
public boolean delDataDir(String dirName) {
File file = new File(FILESPATH + dirName);
return delDir(file);
}
/**
* 更改私有文件名
*
* @param oldName
* @param newName
* @return
*/
public boolean renameDataFile(String oldName, String newName) {
File oldFile = new File(FILESPATH + oldName);
File newFile = new File(FILESPATH + newName);
return oldFile.renameTo(newFile);
}
/**
* 在私有目录下进行文件复制
*
* @param srcFileName
* : 包含路径及文件名
* @param destFileName
* @return
* @throws IOException
*/
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);
}
/**
* 复制私有目录里指定目录的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/**
* 移动私有目录下的单个文件
*
* @param srcFileName
* @param destFileName
* @return
* @throws IOException
*/
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);
}
/**
* 移动私有目录下的指定目录下的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/*
* 将文件写入应用私有的files目录。如:writeFile("test.txt");
*/
public Output wirteFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName,
Context.MODE_WORLD_WRITEABLE);
return new Output(os);
}
/*
* 在原有文件上继续写文件。如:appendFile("test.txt");
*/
public Output appendFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND);
return new Output(os);
}
/*
* 从应用的私有目录files读取文件。如:readFile("test.txt");
*/
public Input readFile(String fileName) throws IOException {
InputStream is = context.openFileInput(fileName);
return new Input(is);
}
/**********************************************************************************************************/
/*********************************************************************************************************/
*/
/**
* 删除一个文件
*
* @param file
* @return
*/
public boolean delFile(File file) {
if (file.isDirectory())
return false;
return file.delete();
}
/**
* 删除一个目录(可以是非空目录)
*
* @param dir
*/
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;
}
/**
* 拷贝一个文件,srcFile源文件,destFile目标文件
*
* @param path
* @throws IOException
*/
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;
}
/**
* 拷贝目录下的所有文件到指定目录
*
* @param srcDir
* @param destDir
* @return
* @throws IOException
*/
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;
}
/**
* 移动一个文件
*
* @param srcFile
* @param destFile
* @return
* @throws IOException
*/
public boolean moveFileTo(File srcFile, File destFile) throws IOException {
boolean iscopy = copyFileTo(srcFile, destFile);
if (!iscopy)
return false;
delFile(srcFile);
return true;
}
/**
* 移动目录下的所有文件到指定目录
*
* @param srcDir
* @param destDir
* @return
* @throws IOException
*/
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():返回抽象路径名的绝对路径名字符串。
[java] view
plaincopy
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());
(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
D:/Text.txt注意这里试大写的Text.txt
(2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
D:/text.txt同样的代码得到不同的结果。
android的文件操作要有权限:
[xhtml] view
plaincopy
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
SD卡下的文件操作:
1、判断SD卡是否插入
[java] view
plaincopy
Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
2、获得sd卡根目录:
[java] view
plaincopy
File skRoot = Environment.getExternalStorageDirectory();
私有目录下的文件操作:
1、获得私有根目录:
[java] view
plaincopy
File fileRoot = Context.getFilesDir()+"//";
还未整理
文件夹或文件夹操作:
1、确定或获得文件夹和文件路径
a、获得文件或文件夹的绝对路径和相对路径。区别
[java] view
plaincopy
String path = File.getPath();//相对
String path = File.getAbsoultePath();//绝对
b 、获得文件或文件夹的父目录
[java] view
plaincopy
String parentPath = File.getParent();
c、获得文件或文件夹的名称:
[java] view
plaincopy
String Name = File.getName();
2、建立文件或文件夹
[java] view
plaincopy
File.mkDir(); //建立文件夹
File.createNewFile();//建立文件
3、判断是文件或文件夹
[java] view
plaincopy
File.isDirectory()
4、列出文件夹下的所有文件和文件夹名
[java] view
plaincopy
File[] files = File.listFiles();
5、修改文件夹和文件名
[java] view
plaincopy
File.renameTo(dest);
6、删除文件夹或文件
[java] view
plaincopy
File.delete();
[java] view
plaincopy
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.getExternalStorageDirectory().getPath() + "//";
FILESPATH = this.context.getFilesDir().getPath() + "//";
}
/**
* 在SD卡上创建文件
*
* @throws IOException
*/
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 删除SD卡上的文件
*
* @param fileName
*/
public boolean delSDFile(String fileName) {
File file = new File(SDPATH + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
file.delete();
return true;
}
/**
* 在SD卡上创建目录
*
* @param dirName
*/
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 删除SD卡上的目录
*
* @param dirName
*/
public boolean delSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
return delDir(dir);
}
/**
* 修改SD卡上的文件或目录名
*
* @param fileName
*/
public boolean renameSDFile(String oldfileName, String newFileName) {
File oleFile = new File(SDPATH + oldfileName);
File newFile = new File(SDPATH + newFileName);
return oleFile.renameTo(newFile);
}
/**
* 拷贝SD卡上的单个文件
*
* @param path
* @throws IOException
*/
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);
}
/**
* 拷贝SD卡上指定目录的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/**
* 移动SD卡上的单个文件
*
* @param srcFileName
* @param destFileName
* @return
* @throws IOException
*/
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);
}
/**
* 移动SD卡上的指定目录的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/*
* 将文件写入sd卡。如:writeSDFile("test.txt");
*/
public Output writeSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file);
return new Output(fos);
}
/*
* 在原有文件上继续写文件。如:appendSDFile("test.txt");
*/
public Output appendSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileOutputStream fos = new FileOutputStream(file, true);
return new Output(fos);
}
/*
* 从SD卡读取文件。如:readSDFile("test.txt");
*/
public Input readSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
FileInputStream fis = new FileInputStream(file);
return new Input(fis);
}
/**
* 建立私有文件
*
* @param fileName
* @return
* @throws IOException
*/
public File creatDataFile(String fileName) throws IOException {
File file = new File(FILESPATH + fileName);
file.createNewFile();
return file;
}
/**
* 建立私有目录
*
* @param dirName
* @return
*/
public File creatDataDir(String dirName) {
File dir = new File(FILESPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 删除私有文件
*
* @param fileName
* @return
*/
public boolean delDataFile(String fileName) {
File file = new File(FILESPATH + fileName);
return delFile(file);
}
/**
* 删除私有目录
*
* @param dirName
* @return
*/
public boolean delDataDir(String dirName) {
File file = new File(FILESPATH + dirName);
return delDir(file);
}
/**
* 更改私有文件名
*
* @param oldName
* @param newName
* @return
*/
public boolean renameDataFile(String oldName, String newName) {
File oldFile = new File(FILESPATH + oldName);
File newFile = new File(FILESPATH + newName);
return oldFile.renameTo(newFile);
}
/**
* 在私有目录下进行文件复制
*
* @param srcFileName
* : 包含路径及文件名
* @param destFileName
* @return
* @throws IOException
*/
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);
}
/**
* 复制私有目录里指定目录的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/**
* 移动私有目录下的单个文件
*
* @param srcFileName
* @param destFileName
* @return
* @throws IOException
*/
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);
}
/**
* 移动私有目录下的指定目录下的所有文件
*
* @param srcDirName
* @param destDirName
* @return
* @throws IOException
*/
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);
}
/*
* 将文件写入应用私有的files目录。如:writeFile("test.txt");
*/
public Output wirteFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName,
Context.MODE_WORLD_WRITEABLE);
return new Output(os);
}
/*
* 在原有文件上继续写文件。如:appendFile("test.txt");
*/
public Output appendFile(String fileName) throws IOException {
OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND);
return new Output(os);
}
/*
* 从应用的私有目录files读取文件。如:readFile("test.txt");
*/
public Input readFile(String fileName) throws IOException {
InputStream is = context.openFileInput(fileName);
return new Input(is);
}
/**********************************************************************************************************/
/*********************************************************************************************************/
*/
/**
* 删除一个文件
*
* @param file
* @return
*/
public boolean delFile(File file) {
if (file.isDirectory())
return false;
return file.delete();
}
/**
* 删除一个目录(可以是非空目录)
*
* @param dir
*/
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;
}
/**
* 拷贝一个文件,srcFile源文件,destFile目标文件
*
* @param path
* @throws IOException
*/
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;
}
/**
* 拷贝目录下的所有文件到指定目录
*
* @param srcDir
* @param destDir
* @return
* @throws IOException
*/
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;
}
/**
* 移动一个文件
*
* @param srcFile
* @param destFile
* @return
* @throws IOException
*/
public boolean moveFileTo(File srcFile, File destFile) throws IOException {
boolean iscopy = copyFileTo(srcFile, destFile);
if (!iscopy)
return false;
delFile(srcFile);
return true;
}
/**
* 移动目录下的所有文件到指定目录
*
* @param srcDir
* @param destDir
* @return
* @throws IOException
*/
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():返回抽象路径名的绝对路径名字符串。
[java] view
plaincopy
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());
(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
D:/Text.txt注意这里试大写的Text.txt
(2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
D:/text.txt同样的代码得到不同的结果。
相关文章推荐
- android的文件操作。(未整理完成)
- android的文件操作。(未整理完成)
- android的文件操作。(未整理完成)
- android的文件操作。(未整理完成)
- Android--完成删除文件、文件夹操作
- Android文件相关操作整理
- winxp文件、打印机共享故障排除方法及“操作无法完成.键入的打印机名不正确,或者指定的打印机没有连接到服务器上"
- Android开发——对sdcard扩展卡文件操作
- 整理javascript操作文件说明:读写文件
- HTTP 文件上载操作需要很长时间才能完成
- Android 文件操作 读/写
- 关于文件操作的整理
- Android文件操作
- 利用Asp.net IO.File类完成文件新建复制删除操作
- “由于文件许可权错误,word无法完成保存操作”解决办法
- word中出现“由于文件许可权错误,word无法完成保存操作”的解决办法
- 在 Eclipse Workbench 之外使用 Eclipse GUI,第 3 部分: 添加操作、菜单和工具栏完成简单的文件资源管理器应用程序
- Symbian文件操作整理
- java怎么操作word文件(bbs上copy,未整理)
- Android SDCard操作(文件读写,容量计算)