File Management Server in JAVA

橙三吉。 提交于 2019-12-12 04:48:23

问题


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 

回答1:


Here in this line

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.



来源:https://stackoverflow.com/questions/16075467/file-management-server-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!