简体   繁体   中英

Is it possible for a server side socket to do both read and write operations on files?

Is it possible to do both operations on files in java sockets? If so how? I tried on method in which the client sends a token such as "R" for read and "W" for write and the server examines it and carries out the requested operation. But this does not seem to work.

A relevant piece of client side code is as follows:

public static void readFile(String input, BufferedReader stdin, PrintWriter out, BufferedReader in) throws IOException
{
    System.out.println("Enter the filename that you want to read");
    stdin = new BufferedReader(new InputStreamReader(System.in));

    String input = stdin.readLine();

    out.println("R" + input);
    System.out.println("File from server \n\n" + in.readLine());

}

public static void writeFile(String input, BufferedReader stdin, PrintWriter out, BufferedReader in, Socket s) throws IOException
{
    System.out.println("Enter the filename that you want to write to");
    stdin = new BufferedReader(new InputStreamReader(System.in));

    input = stdin.readLine();

    out.println("W" + " " + input);
    System.out.println("Enter the text you want to enter in the file");
    out.println(stdin.readLine());
    System.out.println("File has been updated. The updated file from the server is: " + in.readLine());
}

In the server side , I've written a code as follows:

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);


String[] recv = in.readLine().split(" ");
String file_name = recv[1];

This is the place where I check whether I'm supposed to read or write to the file:

if(recv[0] == "R")
{
        while((file_name = recv[1]) != null)
        {
            System.out.println("Filename got from client: " + file_name);
            fileread = new BufferedReader(new FileReader(file_name));

            while((line = fileread.readLine()) != null)
            {
                out.println(line);
            }
            System.out.println("The contents of the file " + file_name + " has been sent to the client");
        }
}

if(recv[0] == "W")
{

        System.out.println("Filename got from client: " + file_name);
        File file = new File(file_name);


        filewrite = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(filewrite);

        line = in.readLine();
        System.out.println(line);
        bw.write(line);
        bw.close();
        out.println(line);

        System.out.println("The contents of the file " + file_name + " has been sent to the client");

    }
}

I do not get an explicit error, but when I run the program, the client takes the file name, the server recognizes it (I know this as I make the server print the name of the file), and then the server does nothing; it stops (without any errors or exceptions).

Can anyone tell me where I went wrong? Or is there a better method to carry out the task?

Thanks in advance.

This is a common noobie mistake in java:

recv[0] == "R"

The above is called reference comparison , the actual string value isn't compared, but the reference to the object instead.

Assuming the content of recv[0] is "R", recv[0] == "R" will most probably return false

The proper way to do string comparison is

recv[0].equals("R")

if you want to disregard character case

recv[0].equalsIgnoreCase("R")

Further into your code, since you're reading your file using readLine() , you have to also ensure your file ends with a newline. If it doesn't, the readLine() method will keep waiting. Subsequent statements will not be executed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM