0

Aim: The server reads in data from a text file sent by a client. The server stores this data in another text file.

Problem: I am able to read in the text file and print it to the console however, when i run my code with the BufferedWriter and open the new textfile after, the file is empty. I am not entirely sure whether i have used the BufferedWriter function incorrectly or if i am missing any key functions out?

Code:

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

String fromServer;
String fromUser;

while (true) {

        fromUser = stdIn.readLine();

        if (fromUser != null) {

            FileReader file = new FileReader("client-temp.txt");
            BufferedReader tc = new BufferedReader(file);
            BufferedWriter bw = new BufferedWriter(new FileWriter("datastore.txt"));

            String line;

            while ((line = tc.readLine()) != null)
            {
                String[] data = line.split(",");
                String sensortype = data[0];
                String date = data[1];
                String time = data[2];
                String reading = data[3];

                String newdata = sensortype + date + time + reading;
                System.out.println(line);

                if (line != null)
                {
                    out.write(line);
                    out.flush();
                }

                System.out.println("Data sent to file");

            }


            System.out.println(EmsClientID + " sending " + fromUser + " to EmsServer");
            out.println(fromUser);
        }
        fromServer = in.readLine();
        System.out.println(EmsClientID + " received " + fromServer + " from EmsServer");
    }
user3009232
  • 11
  • 1
  • 8

2 Answers2

0

Your code is incomplete. I'm gonna go out on a whim here and assume your problem.

Add

out.close();

at the end of your code.

David.Jones
  • 1,413
  • 8
  • 16
0

You never call flush or close on the instance of BufferedWriter, in fact, you ignore it completely. Also, you resource management is none existent. If you open a resource, you should close it.

For example...

FileReader file = new FileReader("client-temp.txt");
try (BufferedReader tc = new BufferedReader(file)) {
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("datastore.txt"))) {

        String line;

        while ((line = tc.readLine()) != null)
        {
            String[] data = line.split(",");
            String sensortype = data[0];
            String date = data[1];
            String time = data[2];
            String reading = data[3];

            String newdata = sensortype + date + time + reading;
            System.out.println(line);

            if (line != null)
            {
                bw.write(line);
                bw.newLine();
            }

            System.out.println("Data sent to file");
        }
    } catch (IOException exp) {
        exp.printStackTrace();
    }
} catch (IOException exp) {
    exp.printStackTrace();
}

See The try-with-resources Statement for more details

(ps: You can compound the try-with-resource statement, opening multiple resources within the same try (...) { section, but I wanted to demonstrate the basic concept)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366