java文件服务器,JAVA中的文件管理服务器

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值