1

在下载 (Java) 脚本中,您可以将位置设置为%appdata%%home%等吗?我尝试以多种不同的方式添加此脚本,但我想出的只是错误。我是否需要.bat事先启动一个文件来设置目录cd以及所有内容?

4

1 回答 1

1

You can set the path to an environment variable using System.getenv() (no .bat script required):

File dir = new File(System.getenv("APPDATA"), "DataFolder");

To make sure the folder is created:

if (!dir.exists())
{
    try
    {
        dir.mkdirs();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

To make a file in the folder and make sure it is created:

File file = new File(dir, "log.txt");
if (!file.exists())
{
    try
    {
        file.createNewFile();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
于 2013-05-27T02:10:20.413 回答