How to change OS-based file path

I have a class that reads a list available in a specific place,

below is my code,

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

   }
}

in this, the file path can be passed as an argument and which should be considered based on the OS,

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

Is it correct?

+4
source share
10 answers

There are more efficient ways to use file paths ...

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

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

Use 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){}

So, a simple change to your method will now work with the cross platform:

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

   }
+6
source

Windows, .

+5

java , .

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

-, , asdfasdf/sdfsdf/. , .

replaceAll , :

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

quoteReplacement replaceAll

String . , s appendReplacement Matcher. , , s, . ('\') ('$') .

0

Path . .

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

.

  • Foo\
  • Foo/
  • Foo\/Baz
  • Foo\\
  • Foo//
  • Foo\\//
  • ...
0

java.io.File.separatorChar - .

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

0

org.apache.commons.io.FilenameUtils , separatorsToSystem (String path) .

0

, , .

System.getProperty("file.separator");
0

"/". linux, windows .

0

All Articles