客户端,Client.java:
我们提供的服务有:网站设计制作、做网站、微信公众号开发、网站优化、网站认证、乐昌ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的乐昌网站制作公司
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();
}
}
服务端,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();
}
}
实现两台电脑同步文件夹的方法:
1、在登录完成之后,客户端会自动弹出配置向导。
2、选择目标文件夹之后,客户端会自动在该文件夹下面生成名为“”的同步文件夹。
即可实现成功。
给大家讲讲我的学习经历吧,开始的两年我学JAVA就是拿书看,练习书上的例子,但是一直没有好的效果。后来因为项目需要开始正式使用JAVA开发,我发现以前练习的东西都忘光了,所以不得不在拿起书看。也就是基本上做了无用功了。现在想来因为大部分是抄来的,不怎么动脑子,所以不清楚为什么要这样。所以我强烈建议你学习的时候给自己设定一个假想的项目目标,做一个东西出来,网页也好,application也好,这样你才会快速地学习。对java有了一定地感觉以后,你在去看他的DOCUMENT,看他的源码,我建议你从低版本的JAVA源码看起,因为他类少,看起来容易懂。然后你就会知道自己以前编码的东西那些是很幼稚的,是可以用一两句就能搞定的。如果你已经稍微有了些感觉,如果觉得合适也可以考虑参加一些培训,毕竟比自学要轻松些,另外还可以学到别人的一些经验,比如:“动力节点”或“王勇Java”一些学员反应不错的,也建议你们多了解一下其它的培训机构。Y.
上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)
//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法
//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //学号
private String studentName; //姓名
private double englishScore; //英语成绩
private double computerScore; //计算机成绩
private double mathScore; //数学成绩
private double totalScore; //总成绩
//空构造函数
public Student() {}
//构造函数
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;
}
//计算总成绩
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//计算评测成绩
public double testScore() {
return sum()/3;
}
//实现compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重写toString方法
public String toString(){
return "学号:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英语成绩:"+this.getEnglishScore()+" 数学成绩:"+this.getMathScore()+" 计算机成绩:"+this.getComputerScore()+" 总成绩:"+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())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同
return true;
} else {
return false;
}
}
/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的
在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩
*/
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子类学习委员类的实现
class StudentXW extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的构造函数
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子类班长类的实现
class StudentBZ extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的构造函数
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//测试类
public class Test {
public static void main(String[] args) {
//生成若干个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()+"学生的评测成绩为:"+ avgScore+"分");
}
}
}
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.03333333333333分