java - 如何根据操作系统更改文件路径

标签 java linux windows file

我有一个类可以读取特定位置可用的列表,

下面是我的代码,

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExceptionInFileHandling {

   @SuppressWarnings({ "rawtypes", "unchecked" })
   public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
       try {
           File l_Directory = new File(a_Path);
           File[] l_files = l_Directory.listFiles();

           for (int c = 0; c < l_files.length; c++) {
               if (l_files[c].isDirectory()) {
                   a_folders.add(l_files[c].getName());
               } else {
                   a_files.add(l_files[c].getName());
               }
           }
       } catch (Exception ex){
           ex.printStackTrace();
       }

   }
   @SuppressWarnings("rawtypes")
   public static void main(String args[]) throws IOException {

       String filesLocation = "asdfasdf/sdfsdf/";
       List l_Files = new ArrayList(), l_Folders = new ArrayList();
       GetDirectory(filesLocation, l_Files, l_Folders);

       System.out.println("Files");
       System.out.println("---------------------------");
       for (Object file : l_Files) {
           System.out.println(file);
       }
       System.out.println("Done");

   }
}

在此文件路径可以作为参数传递,应根据操作系统采用,

filePath.replaceAll("\\\\|/", "\\" + System.getProperty("file.separator"))

这是正确的吗?

最佳答案

有更好的方法来使用文件路径...

// Don't do this
filePath.replaceAll("\\\\|/", "\\" + System.getProperty("file.separator"))

使用java.nio.file.path :

import java.nio.file.*;

Path path = Paths.get(somePathString);
// Here is your system independent path
path.toAbsolutePath();
// Or this works too
Paths.get(somePathString).toAbsolutePath();

使用File.seperator :

// You can also input a String that has a proper file seperator like so
String filePath = "SomeDirectory" + File.separator;
// Then call your directory method
try{
    ExceptionInFileHandling.GetDirectory(filePath, ..., ...);
} catch (Exception e){}

所以对你的方法做一个简单的改变现在就可以跨平台工作了:

@SuppressWarnings({ "rawtypes", "unchecked" })
   public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
       try {
           // File object is instead constructed 
           // with a URI by using Path.toUri()
           // Change is done here
           File l_Directory = new File(Paths.get(a_Path).toUri());

           File[] l_files = l_Directory.listFiles();
           for (int c = 0; c < l_files.length; c++) {
               if (l_files[c].isDirectory()) {
                   a_folders.add(l_files[c].getName());
               } else {
                   a_files.add(l_files[c].getName());
               }
           }
       } catch (Exception ex){
           ex.printStackTrace();
       }

   }

关于java - 如何根据操作系统更改文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26990712/

相关文章:

java - 这两个通用函数之间的区别

java - 我应该如何替换新 OpenCV 中的 FeatureDetector 功能?

java - 无法在 Mac OS 10.11 上加载 R xlsx 包

linux - 我无法在 Lazarus 中将 CGI 项目编译到 Linux

java - 如何使用 java 访问 CD 驱动器?

windows - 为什么我的网站在 PC 和 Mac 上显示不同?

c# - 如何找出在我的应用程序中导致 I/O 的原因?

java - 用 Java 读取网页的全部内容

linux - 为什么从用作监视服务的 bash 脚本启动的进程在脚本终止后仍然存在?

python - 我的 python 程序在 Windows 7 中运行良好并打印到控制台,但在 Linux Mint 中出现错误?