Java-指定文件路径

Java-指定文件路径

Tags: Java

摘要

在java中指定文件路径-斜杠-反斜杠-傻傻分不清楚

File.separator

传统的创建文件路径的方式是, 使用 File.separator 或者 System.getProperty("file.separator"). 这两种方法都会返回正确的文件分隔符 ,比如:

  1. Windows = \ (大于90度就是反斜杠)
  2. *nix or Mac = / (小于90度就是斜杠)

指定文件路径的例子

package cn.javacore.io.file;

import java.io.File;
import java.io.IOException;

public class FilePathExample1 {
    public static void main(String[] args) {
        try {

            String filename = "newFile.txt";
            String workingDirectory = System.getProperty("user.dir");
            //****************//
            String absoluteFilePath = "";
            //absoluteFilePath = workingDirectory + System.getProperty("file.separator") + filename;
            absoluteFilePath = workingDirectory + File.separator + filename;
            System.out.println("Final filepath : " + absoluteFilePath);
            //****************//
            File file = new File(absoluteFilePath);
            if (file.createNewFile()) {
                System.out.println("File is created!");
            } else {
                System.out.println("File is already existed!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出

Final filepath : /home/zhu/IdeaProjects/work_test/newFile.txt
File is already existed!

new File()

一些开发者使用 new File() 来构建文件路径。

package cn.java.file;

import java.io.File;
import java.io.IOException;

public class FilePathExample2 {
    public static void main(String[] args) {
      try {

        String filename = "newFile.txt";
        String workingDirectory = System.getProperty("user.dir");

        //****************//

        File file = new File(workingDirectory, filename);

        //****************//

        System.out.println("Final filepath : " + file.getAbsolutePath());
        if (file.createNewFile()) {
            System.out.println("File is created!");
        } else {
            System.out.println("File is already existed!");
        }

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
}

输出

Final filepath : /Users/zhu/Documents/workspace/maven/fileUtils/newFile.txt
File is created!

手动指定文件分隔符

检查系统类型然后手动创建文件的路径,这种方式不推荐。

package cn.java.file;

import java.io.File;
import java.io.IOException;

public class FilePathExample3 {
    public static void main(String[] args) {
      try {

        String filename = "testing.txt";
        String workingDir = System.getProperty("user.dir");

        String absoluteFilePath = "";

        //****************//

        String your_os = System.getProperty("os.name").toLowerCase();

        if (your_os.indexOf("win") >= 0) {

            //if windows
            absoluteFilePath = workingDir + "\\" + filename;

        } else if (your_os.indexOf("nix") >= 0 || 
                           your_os.indexOf("nux") >= 0 || 
                           your_os.indexOf("mac") >= 0) {

            //if unix or mac 
            absoluteFilePath = workingDir + "/" + filename;

        }else{

            //unknow os?
            absoluteFilePath = workingDir + "/" + filename;

        }

        System.out.println("Final filepath : " + absoluteFilePath);

        //****************//

        File file = new File(absoluteFilePath);

        if (file.createNewFile()) {
            System.out.println("Done");
        } else {
            System.out.println("File already exists!");
        }

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
}

输出

Final filepath : /Users/zhu/Documents/workspace/maven/fileUtils/newFile.txt
File is created!

代码下载

https://coding.net/u/javacore/p/java-io-study/git