java - Problem creating a properties file -


this ended doing. works great use fine tuning.

file file = new file("c:/users/mike home/desktop/"+filename+".properties"); fileinputstream instream = null; fileoutputstream outstream = null; properties config = new properties();          try{             if (file.exists()){//checks if exists.             instream = new fileinputstream(file);                 if (instream.available() >= 0){//cheacks if has in it.                     config.load(instream);                     system.out.println(config);                 }             }              config.setproperty(property , score);//new property             outstream = new fileoutputstream(file);             config.store(outstream, "property");//names properties in file property             config.list(system.out);//prints out properties.           } catch (ioexception ioe){//handles problems             system.out.println("you pooped pants");         } finally{//closes both input , output streams if open             try {                 if(instream != null)                     instream.close();                 if (outstream != null)                     outstream.close();             } catch (ioexception e) {             }         } } 

i have 2 sets of code. 1 writes property file , 1 bit more in depth. think should both writing fill if doesn't exist 1 of them does. here each code extras. i'm using console right , i'm messing around not flashy @ all.

private void properties() {     system.out.println("what name .properties file?");     stest.stringreader();     string filename =stest.getstring();// name of file.      system.out.println("what property change?");     stest.stringreader();        string property= stest.getstring();     system.out.println("what change " + property + " to?");     stest.stringreader();     string score = stest.getstring();          try {             file file = new file("c:/users/mike home/desktop/"+filename+".properties");             fileinputstream instream = new fileinputstream(file);             properties config = new properties();             config.load(instream);             // create new property             config.setproperty(property , score);             fileoutputstream outstream = new fileoutputstream(file);             config.store(outstream, "property");             instream.close();             outstream.close();             config.list(system.out);         } catch (ioexception ioe){             system.out.println("chould not write file.");         } } 

and here 1 writes property without adding thing it. method 1 creates file feel "file file = new file" should both. again feelings don't account when programming. i'd love explanation. thanks.

private void mywrite() {     system.out.println("what name file?");     stest.stringreader();     string filename =stest.getstring();// name of file.      system.out.println("what put in file?");     stest.stringreader();     string letswrite = stest.getstring();         try {             file file = new file("c:/users/mike home/desktop/"+filename+".properties");             fileoutputstream filestream = new fileoutputstream(file);             write(filestream, letswrite);         } catch (ioexception ioe){             system.out.println("could not write file" + ioe);         }     } 

redo on code help:

system.out.println("what change " + property + " to?");     stest.stringreader();     string score = stest.getstring();      try {         file file = new file("c:/users/mike home/desktop/"+filename+".properties");         fileinputstream instream = new fileinputstream(file);         properties config = new properties();         config.load(instream);         instream.close();     } catch (ioexception ioe){         system.out.println("chould not read file.");     }      try{         file file = new file("c:/users/mike home/desktop/"+filename+".properties");         fileoutputstream outstream = new fileoutputstream(file);         properties config = new properties();         config.setproperty(property , score);         config.store(outstream, "property");         outstream.close();     } catch (ioexception ioe){         system.out.println("chould not write file.");     } 

it throws ioexception write file. how close in block? haven't used yet. how load file first? i'm still working on wanted while still on. help.

i'm still not getting well. have put in notes of think doing. i'll asking friend later tonight going on more of matlab guy.

file file = new file("c:/users/mike home/desktop/"+filename+".properties");//new instance of these file     fileinputstream instream = null; //creates variable out side try block     fileoutputstream outstream = null;//creates variable out side try block     properties config = new properties();//creates variable out side try block     try {         instream = new fileinputstream(file); //loads file input stream         config.load(instream); //loads config in instream.      } catch (ioexception ioe){//handles problems         system.out.println("chould not read file.");     }      try{         //instream = new fileinputstream(file);//do need again?          //config.load(instream);// need again?           //creates new property         config.setproperty(property , score);          outstream = new fileoutputstream(file); // preps output stream write file         config.store(outstream, "property");//names properties in file property         config.list(system.out);//prints out properties.      } catch (ioexception ioe){//handles problems         system.out.println("chould not write file.");     } finally{//closes both input , output streams         try {             instream.close();//it says these need in try/catch block also.          } catch (ioexception e) {         }         try {             outstream.close();         } catch (ioexception e) {         }     } 

a file object represents file path. creating file instance doesn't create file on file system. opening fileoutputstream , writing operation creates file on file system.

your first code snippet tries read , write from/to file @ same time. should read file, close input stream, , open output stream, write , close output stream. reading file if doesn't exist throw ioexception, have handle possibility. , input/output stream should closed in block.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -