0

While trying to write file in specified directory i am getting exception.

Java code :-

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)
1
  • 1
    I guess you do not have a "sudo " folder in your current folder. Commented Jun 13, 2015 at 8:17

4 Answers 4

2

Do not name the file sudo var/... but only /var/.... sudo is a shell command.

Sign up to request clarification or add additional context in comments.

Comments

2

sudo is not a file you want to write to, It is a program that is used to temporarily elevate privileges. I think you need something like:

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

Comments

1

You cannot write outside of the /home/ directory by default.

Also sudo is a command, you cannot execute a command from a BufferedWriter.

So, launch your jar with sudo java -jar yourJar.jar or launch your IDE in root (for 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
        }
    }
}

Comments

1

You need to launch Eclipse in sudo mode from your terminal.

It is always the same if you need to write a file outside of /home or /media.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.