客戶端,Client.java:
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、樂昌ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的樂昌網(wǎng)站制作公司
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
String host = "127.0.0.1";
socket = new Socket(host, 4444);
File file = new File("M:\\test.xml");
// Get the size of the file
long length = file.length();
byte[] bytes = new byte[16 * 1024];
InputStream in = new FileInputStream(file);
OutputStream out = socket.getOutputStream();
int count;
while ((count = in.read(bytes)) 0) {
out.write(bytes, 0, count);
}
out.close();
in.close();
socket.close();
}
}
服務(wù)端,Server.java:
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException ex) {
System.out.println("Can't setup server on this port number. ");
}
Socket socket = null;
InputStream in = null;
OutputStream out = null;
try {
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Can't accept client connection. ");
}
try {
in = socket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
try {
out = new FileOutputStream("M:\\test2.xml");
} catch (FileNotFoundException ex) {
System.out.println("File not found. ");
}
byte[] bytes = new byte[16*1024];
int count;
while ((count = in.read(bytes)) 0) {
out.write(bytes, 0, count);
}
out.close();
in.close();
socket.close();
serverSocket.close();
}
}
實(shí)現(xiàn)兩臺(tái)電腦同步文件夾的方法:
1、在登錄完成之后,客戶端會(huì)自動(dòng)彈出配置向?qū)А?/p>
2、選擇目標(biāo)文件夾之后,客戶端會(huì)自動(dòng)在該文件夾下面生成名為“”的同步文件夾。
即可實(shí)現(xiàn)成功。
給大家講講我的學(xué)習(xí)經(jīng)歷吧,開始的兩年我學(xué)JAVA就是拿書看,練習(xí)書上的例子,但是一直沒有好的效果。后來因?yàn)轫?xiàng)目需要開始正式使用JAVA開發(fā),我發(fā)現(xiàn)以前練習(xí)的東西都忘光了,所以不得不在拿起書看。也就是基本上做了無用功了?,F(xiàn)在想來因?yàn)榇蟛糠质浅瓉淼?,不怎么?dòng)腦子,所以不清楚為什么要這樣。所以我強(qiáng)烈建議你學(xué)習(xí)的時(shí)候給自己設(shè)定一個(gè)假想的項(xiàng)目目標(biāo),做一個(gè)東西出來,網(wǎng)頁(yè)也好,application也好,這樣你才會(huì)快速地學(xué)習(xí)。對(duì)java有了一定地感覺以后,你在去看他的DOCUMENT,看他的源碼,我建議你從低版本的JAVA源碼看起,因?yàn)樗惿?,看起來容易懂。然后你就?huì)知道自己以前編碼的東西那些是很幼稚的,是可以用一兩句就能搞定的。如果你已經(jīng)稍微有了些感覺,如果覺得合適也可以考慮參加一些培訓(xùn),畢竟比自學(xué)要輕松些,另外還可以學(xué)到別人的一些經(jīng)驗(yàn),比如:“動(dòng)力節(jié)點(diǎn)”或“王勇Java”一些學(xué)員反應(yīng)不錯(cuò)的,也建議你們多了解一下其它的培訓(xùn)機(jī)構(gòu)。Y.
上面 wuzhikun12同學(xué)寫的不錯(cuò),但我想還不能運(yùn)行,并且還不太完善。我給個(gè)能運(yùn)行的:(注意:文件名為:Test.java)
//要實(shí)現(xiàn)對(duì)象間的比較,就必須實(shí)現(xiàn)Comparable接口,它里面有個(gè)compareTo方法
//Comparable最好使用泛型,這樣,無論是速度還是代碼量都會(huì)減少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //學(xué)號(hào)
private String studentName; //姓名
private double englishScore; //英語(yǔ)成績(jī)
private double computerScore; //計(jì)算機(jī)成績(jī)
private double mathScore; //數(shù)學(xué)成績(jī)
private double totalScore; //總成績(jī)
//空構(gòu)造函數(shù)
public Student() {}
//構(gòu)造函數(shù)
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//計(jì)算總成績(jī)
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//計(jì)算評(píng)測(cè)成績(jī)
public double testScore() {
return sum()/3;
}
//實(shí)現(xiàn)compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重寫toString方法
public String toString(){
return "學(xué)號(hào):"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英語(yǔ)成績(jī):"+this.getEnglishScore()+" 數(shù)學(xué)成績(jī):"+this.getMathScore()+" 計(jì)算機(jī)成績(jī):"+this.getComputerScore()+" 總成績(jī):"+this.getTotalScore();
}
//重寫equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照現(xiàn)實(shí)來說,比較是不是同一個(gè)學(xué)生,應(yīng)該只是看他的學(xué)號(hào)是不是相同
return true;
} else {
return false;
}
}
/*以下為get和set方法,我個(gè)人認(rèn)為,totalScore的set的方法沒必要要,因?yàn)樗怯善渌煽?jī)計(jì)算出來的
在set方法中,沒設(shè)置一次值,調(diào)用一次sum方法,即重新計(jì)算總成績(jī)
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子類學(xué)習(xí)委員類的實(shí)現(xiàn)
class StudentXW extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的構(gòu)造函數(shù)
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子類班長(zhǎng)類的實(shí)現(xiàn)
class StudentBZ extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的構(gòu)造函數(shù)
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//測(cè)試類
public class Test {
public static void main(String[] args) {
//生成若干個(gè)student類、StudentXW類、StudentBZ類
Student student1 = new Student("s001","張三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?+ avgScore+"分");
}
}
}
運(yùn)行結(jié)果為:
張三學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?9.66666666666667分
李四學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?0.5分
王五學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?8.0分
李六學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?8.5分
朱漆學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?0.03333333333333分