反正加s就對了,mkdir不能建立多個目錄 html
《------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ java
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------》 android
android的文件操做要有權限: app
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
SD卡下的文件操做: this
一、判斷SD卡是否插入 spa
- Environment.getExternalStorageState().equals(
- android.os.Environment.MEDIA_MOUNTED);
- Environment.getExternalStorageState().equals(
- android.os.Environment.MEDIA_MOUNTED);
二、得到sd卡根目錄: .net
- File skRoot = Environment.getExternalStorageDirectory();
- File skRoot = Environment.getExternalStorageDirectory();
私有目錄下的文件操做: htm
一、得到私有根目錄: blog
- File fileRoot = Context.getFilesDir()+"//";
- File fileRoot = Context.getFilesDir()+"//";
還未整理 遞歸
文件夾或文件夾操做:
一、肯定或得到文件夾和文件路徑
a、得到文件或文件夾的絕對路徑和相對路徑。區別
- String path = File.getPath();//相對
- String path = File.getAbsoultePath();//絕對
- String path = File.getPath();//相對
- String path = File.getAbsoultePath();//絕對
b 、得到文件或文件夾的父目錄
- String parentPath = File.getParent();
- String parentPath = File.getParent();
c、得到文件或文件夾的名稱:
- String Name = File.getName();
- String Name = File.getName();
二、創建文件或文件夾
- File.mkDir(); //創建文件夾
- File.createNewFile();//創建文件
- File.mkDir(); //創建文件夾
- File.createNewFile();//創建文件
三、判斷是文件或文件夾
四、列出文件夾下的全部文件和文件夾名
- File[] files = File.listFiles();
- File[] files = File.listFiles();
五、修改文件夾和文件名
六、刪除文件夾或文件
- 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;
- }
- }
- 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():返回抽象路徑名的絕對路徑名字符串。
- 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());
- 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());