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

时间:2015-06-13 07:57:00

标签: java ubuntu io

尝试在指定目录中写入文件时,我遇到异常。

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)

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

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

同样sudo是一个命令,你不能从BufferedWriter执行命令。

因此,使用sudo java -jar yourJar.jar启动jar或在root中启动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
        }
    }
}

答案 3 :(得分:1)

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

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