In ubuntu unable to write file in specified directory using java -


while trying write file in specified directory 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 create file write content it.

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

you cannot write outside of /home/ directory default.

also sudo command, cannot execute command bufferedwriter.

so, launch jar sudo java -jar yourjar.jar or launch ide in root (for eclipse sudo eclipse).

and try 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 bufferedreader         try         {             while ((line = bufferedreader.readline()) != null) // read line per line config.yml (from bufferedreader) until on             {                 stringbuilder.append(line); // add line stringbuilder                 stringbuilder.append(system.lineseparator()); // add lineseparator stringbuilder             }             allline = stringbuilder.tostring(); // allline equal stringbuilder         }                 {             bufferedreader.close(); // close bufferedreader         }          string studioname = system.getproperty("user.name"); // set studioname          filewriter filewriter = new filewriter("/var/iprotecs/idns2.0/" + studioname + ".yaml", true); // create filewriter && true append string  filewriter or false ecrase string filewriter         try         {             filewriter.write(allline ,0, allline.length()); // write allline "/var/iprotecs/idns2.0/ + studioname + .yaml"         }                 {             filewriter.close(); // close filewriter         }     } } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -