io - Writing to a text file issue Java -


i coding function check if file cq.txt exists , if so, read it. have been having issue have declared printwriter outside of while loop. later in application, supposed write question 1: correct or question 2: correct file.

but, not. creates text file when check it, shows empty.

here code, thank in advance:

package receiverhost;  import java.io.*; import java.net.*;  public class receiverhost { public static void main(string[] args) throws exception {      //set socket on port 5000     serversocket server = new serversocket(5000);      //set "cookie"/ text file written to/ read     writer db = new bufferedwriter(new outputstreamwriter(new fileoutputstream("cq.txt"), "utf-8"));      system.out.println("tcpserver waiting client on port 5000");      while(true){          //declare string fromclient         string fromclient;          socket connected = server.accept();         system.out.println(" client" + " " + connected.getinetaddress() + ":" + connected.getport() + " connected ");          //read/ retrieve client output stream         bufferedreader infromclient = new bufferedreader(new inputstreamreader(connected.getinputstream()));              fromclient = infromclient.readline();             if(fromclient.equals("q1c")){                 //write cq.txt                 db.write("q1: correct");             }              if(fromclient.equals("q2c")){                 //write cq.txt                 db.write("q2: correct");             }         }     } } 

you should call 'write' method on bufferedwriter, , later 'close' it. here example:

how write text file java


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -