java - FileInputStream/DataInputStream -


so, got assignment make guessing game. program has generate number , user has guess. after users guesses right, program shows "highscore" (how many times user had guess before got right).

then should save highscore in text-file, saved on computer, , if restart computer not lost. struggeling how program supposed read file saved.

this "write-code":

try {     fileoutputstream write = new fileoutputstream      dataoutputstream out = new dataoutputstream(write);     out.writebytes(""+highscore);     write.close();} catch(ioexception ioexception ){     system.err.println( "could not write file" );     return;} 

which works fine, don't know how read again. "read-code": (i guessed, don't know if work)

try{     fileinputstream read = new fileinputstream     datainputstream in = new datainputstream(read);     in.readbytes(""+highscore);     joptionpane.showmessagedialog(null, "your highscore is" + highscore); catch(ioexception ioexception ){     system.err.println( "could not read file" );     return;} 

now, dont know if in.readbytes(""+highscore); command correct. guessed (i thought if out.writebytes(""+highscore); worked surely read has work too)

if readbytes correct command error :

the method readbytes(string) undefined type datainputstream

what supposed do?

some information: highscore , int.

for example, if highscore int, you'll want write int file. can with

dataoutputstream out = new dataoutputstream(write); out.writeint(highscore); 

and read with

datainputstream in = new datainputstream(read); int highscore = in.readint(); 

the class datainputstream not have readbytes(). why program not compile.

the whole point of dataiostream classes read , write

primitive java data types underlying [...] stream in machine-independent way.

so use methods corresponding data type of highscore.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -