简体   繁体   English

在Ubuntu中无法使用Java在指定目录中写入文件

[英]In ubuntu unable to write file in specified directory using java

While trying to write file in specified directory i am getting exception. 尝试在指定目录中写入文件时,出现异常。

Java code :- 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);
    }
}

Exception :- 例外:-

How to create file write content to it. 如何创建文件写入内容。

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

Do not name the file sudo var/... but only /var/... . 不要将文件命名为sudo var/...而只能命名为/var/... sudo is a shell command. sudo是一个shell命令。

sudo is not a file you want to write to, It is a program that is used to temporarily elevate privileges. sudo不是您要写入的文件,它是一个用于临时提升特权的程序。 I think you need something like: 我认为您需要以下内容:

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

You cannot write outside of the /home/ directory by default. 默认情况下,您不能在/ home /目录之外进行写操作。

Also sudo is a command, you cannot execute a command from a BufferedWriter. sudo也是命令,您不能从BufferedWriter执行命令。

So, launch your jar with sudo java -jar yourJar.jar or launch your IDE in root (for eclipse sudo eclipse ). 因此,使用sudo java -jar yourJar.jar启动jar或以root sudo java -jar yourJar.jar启动IDE(对于Eclipse sudo eclipse )。

And try something like that: 尝试这样的事情:

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
        }
    }
}

You need to launch Eclipse in sudo mode from your terminal. 您需要从终端以sudo模式启动Eclipse。

It is always the same if you need to write a file outside of /home or /media. 如果您需要在/ home或/ media之外写入文件,则始终相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM