Java File Object Create toFile(String path)

Here you can find the source of toFile(String path)

Description

Like new File(path) EXCEPT this will convert Windows/Max/Linux separators into the local format.

License

Open Source License

Parameter

Parameter Description
path e.g. C:\foo\bar.txt

Return

e.g. C:/foo/bar.txt Note: this does not remove or change windows drive markers

Declaration

public static File toFile(String path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**//from  ww  w. j ava  2s  . c o  m
     * Like new File(path) EXCEPT this will convert Windows/Max/Linux separators into the local format.
     * Use-case: so getParentFile() will work reliably.
     * @param path e.g. C:\foo\bar.txt
     * @return e.g. C:/foo/bar.txt
     * Note: this does not remove or change windows drive markers 
     */
    public static File toFile(String path) {
        if (path == null)
            return null;
        if (File.separatorChar == '\\') {
            // From Windows to Linux/Mac
            return new File(path.replace('/', File.separatorChar));
        } else {
            // From Linux/Mac to Windows
            return new File(path.replace('\\', File.separatorChar));
        }
    }
}

Related

  1. toFile(String fileName, String content)
  2. toFile(String fileName, String cwd)
  3. toFile(String fileName, String txt)
  4. toFile(String inFile)
  5. toFile(String line)
  6. toFile(String path)
  7. toFile(String path)
  8. toFile(String path)
  9. toFile(String path)