如何根据操作系统更改文件path

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

以下是我的代码,

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"); } } 

在这个文件path可以作为parameter passing,应该基于操作系统,

 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(); } } 

在调用File构造函数时,也可以在Windows上使用正斜杠作为目录分隔符。

为什么你不添加java定义的文件分隔符而不是创建一个字符串,然后全部替换。 尝试像

 String filesLocation = "asdfasdf"+File.separator+"sdfsdf"+File.separator; 

为什么你只是不使用“/”。 这是可以接受的Linux和Windows作为路径分隔符。

你的答案应该是正确的。 还有另一个类似的线程回答:

Java正则表达式来替换基于OS的文件路径

Java中与平台无关的路径

您可以从传递的参数中生成一个Path对象。 那么你不需要自己处理文件分隔符。

 public static void main(String[] args) { Path path = Paths.get(args[0]); System.out.println("path = " + path.toAbsolutePath()); } 

该代码能够处理以下传递的参数。

  • FOO \酒吧
  • 富/酒吧
  • FOO \酒吧/巴兹
  • FOO \\酒吧
  • FOO //巴兹
  • FOO \\吧//巴兹

首先,你不应该使用像asdfasdf/sdfsdf/这样的相对路径。 这是一个很大的错误来源,因为你的路径取决于你的工作目录。

那东西说,你的replaceAll是相当不错的,但它可以像这样改进:

 filePath.replaceAll( "[/\\\\]+", Matcher.quoteReplacement(System.getProperty("file.separator"))); 

replaceAll文档中使用quoteReplacement

返回指定String的文字替换字符串。 这个方法产生一个字符串,作为Matcher类的appendReplacement方法中的一个字面替换。 生成的字符串将与s中被处理为字面序列的字符序列相匹配。 斜杠('\')和美元符号('$')将没有特别的含义。

您需要使用java.io.File.separatorChar来与系统相关的默认名称分隔符。

String location =“usr”+ java.io.File.separatorChar +“local”+ java.io.File.separatorChar;

org.apache.commons.io.FilenameUtils包含了许多有用的方法,例如separatorsToSystem(String path)根据您正在使用的操作系统来转换给定路径中的分隔符。

使用下面的方法来了解OS文件分隔符,然后用此方法替换所有以前的分隔符。

 System.getProperty("file.separator");