JAVA中的文件管理服务器

问题描述:

我有一个应用程序(SWT),我需要在服务器端管理一个文件。通过管理i表示3件事情,写入内容到文件,在中应用读/写锁定机制和 c $ c> TextArea 。我需要创建一个多线程服务器来实现这一点,因为我的应用程序(实际上是一个基于eclipse的插件)接受多个用户。我是新的这个客户端服务器的东西和套接字编程,我已经阅读了几个教程,但仍然找到任何最佳的解决方案。我不需要代码(互联网上有很多),而是我需要的方式或步骤来做。
谢谢。

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.

我要重新创建的文件要保留作者的姓名。

服务器程序:

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);

                    }

    }

客户代码:

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);
    }

我真正需要的是在服务器上维护文件并显示文件内容在textArea(historyArea)上。

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.

想要的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我得到的只是第一个版本:

Ouptut i'm getting is just the first revision:

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


此行中的

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

您以写入模式打开文件,因此写入操作将覆盖现有内容。
而不是这样

you are opening the file in write mode, so a write operation will overwrite the existing contents. Instead do this

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

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

This will open the file in append mode.