如何正确使用 writeUTF() 方法?

how to use writeUTF() method the right way?

我正在尝试制作交换文件的简单客户端和服务器,因此,我正在尝试使用 writeUtf() 方法将文件名作为 String 发送,但它不起作用完全没有。

 public class Client {
    public static void main(String[] args) throws Exception {
    @SuppressWarnings("resource")
    Socket client = new Socket("localhost", 1241);
Path currentRelativePath = Paths.get("");
String currentPath = currentRelativePath.toAbsolutePath().toString();
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
DataInputStream dataInFromServer = new DataInputStream(client.getInputStream());
Scanner keyboardInput=new Scanner(System.in);
System.out.println("Enter Operation Type(PUt, Get, Or Exit)");
String OperationType=keyboardInput.nextLine();
switch(OperationType){
case "PUT":
    outToServer.writeChar('P');
    System.out.println("File Or Directory name:");
    String FileOrDirName=keyboardInput.nextLine();
    //outToServer.writeUTF(FileOrDirName);
    //outToServer.write((short)FileOrDirName.length());
        outToServer.writeUTF(FileOrDirName);
    File fileOrDir=new File(FileOrDirName);
    if(fileOrDir.getParent()==currentPath || fileOrDir.getParent()==null){
    if(fileOrDir.isDirectory()){
        outToServer.writeInt(1);
        FileOperations.zipDir(fileOrDir);
        outToServer.write(FileOperations.readFileAsByteArray(FileOrDirName+".zip"));

        File tmp=new File(FileOrDirName+".zip");
        tmp.delete();
    }
    else {
        outToServer.writeInt(0);

        byte toSend[]=FileOperations.readFileAsByteArray(FileOrDirName);
        outToServer.writeDouble(toSend.length);
        for(int i=0; i<toSend.length; i++)
                outToServer.writeByte(toSend[i]);
    //  outToServer.write(ToSend,0,ToSend.length);
        //outToServer.write(FileOperations.readFileAsByteArray(FileOrDirName));
}
    outToServer.flush();
    outToServer.close();
    keyboardInput.close();
    client.close();
}   
    }

}
    }

这是服务器:

        import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.file.Path;
import java.nio.file.Paths;

class TcpFileServerThread extends Thread{
    private Socket client;
    public TcpFileServerThread(Socket client) {
        this.client = client;
    }
    public void run() {
        try {
            DataOutputStream outToClient = new DataOutputStream(client.getOutputStream());
            DataInputStream dataInFromClient = new DataInputStream(client.getInputStream()) ;
            Path currentRelativePath = Paths.get("");
            String currentPath = currentRelativePath.toAbsolutePath().toString();
            //File f;
            //System.out.println(dataInFromClient.readInt());
            /** client request PUT **/
            switch(dataInFromClient.readChar()){
            case 'P':
            int lengthRecvd= (int)dataInFromClient.readDouble();
            byte[] toRecv=new byte[lengthRecvd];

            for(int i=0; i<lengthRecvd; i++){
                toRecv[i]=dataInFromClient.readByte();
            }

        //  dataInFromClient.read(file_or_dir_from_client);
            //FileOrDirName="uu.txt";
            String FileOrDirName=dataInFromClient.readUTF();
        //  int arrLen=dataInFromClient.readShort();
        /** for(int i=0; i<arrLen; i++){
                char o=dataInFromClient.readChar();
                System.out.println(o);
            }**/

            System.out.println(FileOrDirName);
            File f=new File(FileOrDirName);
            if(f.getParent()==null || f.getParent()==currentPath){

            FileOperations.writeFromByteArrayToFile(toRecv, FileOrDirName); 
            if(dataInFromClient.readInt()==1){
                File zipFl=new File(FileOrDirName+".zip");
            FileOperations.unzip(zipFl);
                zipFl.delete();
            }   
            }

            }



        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    }

我得到 FileNotFoundException 并且服务器中 System.out.println(FileOrDirName) 的输出是一个空行,因为它是空的。

只有一种使用方法,不是这个。

int lengthRecvd= (int)dataInFromClient.readDouble();

您在这里阅读的是 double 尚未编写的内容。因此,您将获得输入中接下来的 8 个字节的双精度值。

        byte[] toRecv=new byte[lengthRecvd];
        for(int i=0; i<lengthRecvd; i++){
            toRecv[i]=dataInFromClient.readByte();
        }

在这里您正在读取那么多字节的输入,可以是任何内容,也没有被写入。

删除所有这些。

        String FileOrDirName=dataInFromClient.readUTF();

在这里你终于读取了数据,这是唯一实际写入的数据。

令人惊讶的是这段代码没有抛出某种 IOException

你只能用DataInputStream读到DataOutputStream相应方法实际写入的数据,除了单纯的字节数组。