0

I am trying to write contents to a file, but once the while loop prints all the lines , it get's stuck and doesn't go to the next line. The line after while loop are not being executed. My first guess was that maybe the fstream is still waiting for another line, but the while loop does check if no more data then it should come out.

Please help me out here ! Thanks.

//Code

File file;

                DataInputStream inputData = new DataInputStream(newPeerConnection.getInputStream());

                file = new File(
                        "F:\\Workspace\\PeerToPeer\\src\\ncsu\\csc\\socketClasses\\out\\out.txt");

                @SuppressWarnings("resource")
                PrintWriter fstream = new PrintWriter(new FileWriter(
                        file, true));

                if (!file.exists()) {
                    file.createNewFile();
                }

                                 System.out.println("Writing to file: ");
                while ((strval = inputData.readUTF()) != null) {
                    System.out.println(strval);
                    fstream.println(strval);
                    //fstream.flush();
                    //fstream.close();
                }
                //These lines are not executed
                System.out.println("Do you want to download another file (Y/N)?");
                wantToContinue = scannerObj.next();
                 out.writeUTF(wantToContinue);
                 out.flush();}
Sudhanshu
  • 421
  • 2
  • 6
  • 15
  • 2
    If they aren't executed, either your loop is infinite or it is blocked. – Sotirios Delimanolis Mar 31 '14 at 02:13
  • the loop is not infinite, else the values "null" would keep printing after the data of the file was read. It might be blocked, but am not sure how. – Sudhanshu Mar 31 '14 at 02:20
  • It's most likely blocked, please post the code that writes to the stream. – Jesper Fyhr Knudsen Mar 31 '14 at 02:35
  • Also, set a timeout on your stream, that way you get an exception in cases like these instead of a dead application. – Daniel B. Chapman Mar 31 '14 at 03:23
  • I tried finding the documentation for readUTF for timeout, but that doesn't have the information init. Also surprisingly it doesn't mention readUTF is a blocking call. This is what i referred to http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readUTF() – Sudhanshu Mar 31 '14 at 03:27

1 Answers1

1

I guess your while loop is blocked by inputData.readUTF().

You should ensure the input stream of newPeerConnection is closed by another port, otherwise inputData will wait for next input and inputData.readUTF() blocks.

locoyou
  • 1,697
  • 1
  • 15
  • 19
  • Yes you're correct, i figured readUTF() was a blocking call . I am trying readLine(), but that's deprecated i believe. – Sudhanshu Mar 31 '14 at 03:22