在JAVA文件管理服务器(File Management Server in JAVA)

2019-10-29 11:31发布

I have an application(SWT) where i need to manage a file at the server end. By managing i mean 3 things, writing contents to the file, applying read/write lock mechanism to it and displying the same in the TextArea. I need to create a multithreaded server to achieve this as my application(which actually is a eclipse based plugin) accepts multiple users. I'm new to this client-server thing and socket programming and i've read few tutorials but still havent found any optimum solution to it. I do not need the code(there are plenty on the internet), rather i need the way or the steps to do it. Thanks.

Also, i found some server code which actually works fine. However not displaying the desired results.

What i rellay want to do with this file is to maintain the author's name. revision number and related destils as the SVN do on the server side.

Server Program:

public void onServer() throws Exception {
        String sentByClient;
        String line1 = null;
        ServerSocket socket = new ServerSocket(6789);
        while(true) {

    System.out.println("Listening...");
        Socket connectionSocket = socket.accept();
        BufferedReader inFromClient =
        new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
        sentByClient = inFromClient.readLine();

        System.out.println("Received: " + sentByClient);

        File file=new File("HistoryFile.txt");//------------------server file
        BufferedWriter writ=new BufferedWriter(new FileWriter(file));
        writ.write(sentByClient);
        writ.close();
        BufferedReader read=new BufferedReader(new FileReader(file));

        while((line1=read.readLine())!=null) {
            System.out.println(line1);

        }
        outToClient.writeBytes(line1);

                    }

    }

Client code:

public void onClient(String param) throws Exception {
        Socket clientSocket = new Socket("localhost", 6789);
          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          //sentence = inFromUser.readLine();
          sentence=param; // Here i'm sending the author name, revision details, etc from svn to my server 
          outToServer.writeBytes(sentence);
          newSentence = inFromServer.readLine();
          System.out.println("FROM SERVER: " + newSentence);
          historyArea.append(newSentence);
    }

What i actually need is maintaining a file on the server and displaying the file contents on the textArea(historyArea). I'm sending the history data from SVN to the file.

Desired Ouptut:

Revision Number: 1
Author: a
Time:xxxx
Changed Path:xxxx 
-------------------
Revision Number: 2
Author: a
Time:xxxx
Changed Path:xxxx
------------------
Revision Number: 3
Author: a
Time:xxxx
Changed Path:xxxx

Ouptut i'm getting is just the first revision:

Revision Number: 1
    Author: a
    Time:xxxx
    Changed Path:xxxx 

Answer 1:

在这里,在这条线

BufferedWriter writ=new BufferedWriter(new FileWriter(file));

你是开在写入模式文件,所以写操作将覆盖现有的内容。 相反,这样做

BufferedWriter writ=new BufferedWriter(new FileWriter(file,true));

这将打开追加模式的文件。



文章来源: File Management Server in JAVA