Java和Mac – 如何将文件写入相对路径

huangapple go评论83阅读模式
英文:

Java & Mac - How to write a file to relative path

问题

我在一台Mac上使用一个写文件到“文档”文件夹的脚本。但我想要能够以一种方式来写文件,以便我可以发送给我的朋友,而他们不必更改文件的路径。

File myFile = new File("Users/MyName/Documents/Test/user.text/");

有没有一种方法可以使它相对路径,而不必更改“MyName”为下一个人的电脑用户名?
英文:

I'm on a mac with a script that's writing files to the Documents folder. But I want to be able to write it where I can send it to my friends and they don't have to change the file's path.

File myFile = new File("Users/MyName/Documents/Test/user.text/");

Is there a way where I can make it relative where I don't have to switch around the MyName to whatever the next person PC's name is?

答案1

得分: 0

系统属性'user.home'是你正在寻找的魔法。不幸的是,没有一种与操作系统无关的方法来确定'文档'文件夹,可能是因为这不是一个普遍的概念。

你可以随时快速检查它是否存在,如果是的话,就使用它。此外,还有一个新的文件API,我建议你使用它。

将其组合在一起:

Path p = Paths.get(System.getProperty("user.home"));
Path candidate = p.resolve("Documents");
if (!Files.isDirectory(candidate)) candidate = p.resolve("My Documents");
if (!Files.isDirectory(candidate)) candidate = p;
p = p.resolve("Test").resolve("user.text");
Files.createDirectories(p.getParent());
Files.write(p, "Hello!");
英文:

The system property 'user.home' is the magic you're looking for. Unfortunately, there is no OS-agnostic way to determine 'the documents' folder, probably because that is not a universal concept.

You can always just do a quick check if it is there, and if so, use it. Also, there's a new file API, I suggest you use that.

Putting it together:

Path p = Paths.get(System.getProperty("user.home"));
Path candidate = p.resolve("Documents");
if (!Files.isDirectory(candidate)) candidate = p.resolve("My Documents");
if (!Files.isDirectory(candidate)) candidate = p;
p = p.resolve("Test").resolve("user.text");
Files.createDirectories(p.getParent());
Files.write(p, "Hello!");

答案2

得分: 0

以下是翻译好的部分:

对于任何遇到 @rzwitserloot 给出的 ("user.home") 解决方案有困难的人,这里是对我有效的方法。它有点凌乱,但它有效。

public class WritingFile {

    static String data = "这是来自jar包的输出文件中的数据。它是否工作!?!!";
    //static String paths;
    static String full;
    //static String tester = "/Users/226331/Documents/Test/filesname.txt/";
    public static void main(String[] args) {
        
        pathMaker();
        makeFile();
    }

    public static void pathMaker() {
        //Path path = new Paths.get("test.txt");
        String paths = System.getProperty("user.home");
        System.out.println(paths);
        //System.getenv(paths);
        //System.out.println(paths);
        
        full = paths + "/Documents/Test/filesname.txt/";
        System.out.println(full);
    }
    public static void makeFile() {
        try {
              // 创建FileWriter
            File myFile = new File(full);
            FileWriter output = new FileWriter(myFile);

              // 将字符串写入文件
              output.write(data);

              // 关闭写入器
              output.close();
            }

            catch (Exception e) {
              e.getStackTrace();
              e.printStackTrace();
            }
    }
    
}
英文:

For anyone who's having trouble with @rzwitserloot answer with the ("user.home") solution, Here's what works for me. It's a little messy but it's what works.

public class WritingFile {

	static String data = "This is THe DATA in the output file from a jar. Did it work!?!!";
	//static String paths;
	static String full;
	//static String tester = "/Users/226331/Documents/Test/filesname.txt/";
	public static void main(String[] args) {
		
		pathMaker();
		makeFile();
	}

	public static void pathMaker() {
		//Path path = new Paths.get("test.txt");
		String paths = System.getProperty("user.home");
		System.out.println(paths);
		//System.getenv(paths);
		//System.out.println(paths);
		
		full = paths + "/Documents/Test/filesname.txt/";
		System.out.println(full);
	}
	public static void makeFile() {
		try {
		      // Creates a FileWriter
		    	File myFile = new File(full);
		    FileWriter output = new FileWriter(myFile);

		      // Writes the string to the file
		      output.write(data);

		      // Closes the writer
		      output.close();
		    }

		    catch (Exception e) {
		      e.getStackTrace();
		      e.printStackTrace();
		    }
	}
	
}

huangapple
  • 本文由 发表于 2020年8月4日 09:50:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63239154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定