在 ubuntu 中无法使用 java 在指定目录中写入文件

In ubuntu unable to write file in specified directory using java

尝试在指定目录中写入文件时出现异常。

Java 代码:-

public void jsonToYaml(JSONObject json, String studioName)
        throws JSONException, org.codehaus.jettison.json.JSONException,
        IOException {
    Yaml.dump(Yaml.dump(JsonToMap.jsonToMap(json)), new File("config.yml"));
    BufferedReader br = new BufferedReader(new FileReader("config.yml"));
    String line;
    studioName = studioName.toLowerCase();
    File writeFile = new File("sudo /var/iprotecs/idns2.0","" + studioName + ".yaml");
    FileOutputStream fos = new FileOutputStream(writeFile);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    try {
        while ((line = br.readLine()) != null) {
            String line1 = line.replace("\"", "");
            String line2 = line1.replaceAll("!java.util.HashMap", "");
            String line3 = line2.replaceAll("---", "");
            String line4 = line3.replace("|", "");
            System.out.println(line4);
            bw.write(line4);
            bw.newLine();
        }
    } catch (FileNotFoundException e) {
        System.out.println(e);
    }
}

异常:-

如何创建文件写入内容。

java.io.FileNotFoundException: sudo /var/iprotecs/idns2.0/asia.yaml (No such file or directory)

不要将文件命名为 sudo var/...,而只命名为 /var/...sudo 是一个 shell 命令。

sudo 不是您要写入的文件,它是一个用于临时提升权限的程序。我想你需要这样的东西:

File writeFile = new File("/var/iprotecs/idns2.0", studioName + ".yaml");

默认情况下,您不能在 /home/ 目录之外写入。

另外sudo是命令,不能从BufferedWriter执行命令。

因此,使用 sudo java -jar yourJar.jar 启动您的 jar 或在根目录中启动您的 IDE(对于 eclipse sudo eclipse)。

然后尝试类似的操作:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;


class jsonToYaml
{
    public static void main(String args[]) throws Exception
    {
        String line, allLine;
        StringBuilder stringBuilder = new StringBuilder();

        BufferedReader bufferedReader = new BufferedReader(new FileReader("config.yml")); // Add config.yml into the BufferedReader
        try
        {
            while ((line = bufferedReader.readLine()) != null) // Read line per line config.yml (from the BufferedReader) until it is over
            {
                stringBuilder.append(line); // add the line into stringBuilder
                stringBuilder.append(System.lineSeparator()); // add a lineSeparator into stringBuilder
            }
            allLine = stringBuilder.toString(); // allLine is equal to stringBuilder
        }
        finally
        {
            bufferedReader.close(); // Close the BufferedReader
        }

        String studioName = System.getProperty("user.name"); // set studioName

        FileWriter fileWriter = new FileWriter("/var/iprotecs/idns2.0/" + studioName + ".yaml", true); // create a FileWriter && true for append a String into  your FileWriter or false for ecrase a String into your FileWriter
        try
        {
            fileWriter.write(allLine ,0, allLine.length()); // Write allLine into "/var/iprotecs/idns2.0/ + studioName + .yaml"
        }
        finally
        {
            fileWriter.close(); // close the FileWriter
        }
    }
}

您需要从终端以 sudo 模式启动 Eclipse。

如果你需要在/home 或/media 之外写一个文件总是一样的。