输入/输出流
public class Test {
private static String file;
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("主机名称"+address.getHostName());
System.out.println("ip名称"+address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
File file = new File("f://11.txt");
File fileCopy = new File("f://22.txt");
try {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(fileCopy);
byte[] array = new byte[1024];
int i = fis.read(array);
while(i!=-1){
fos.write(array, 0, i);
i = fis.read(array);
}
fos.flush();
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
String s = "往外写东西";
bw.write(s);
bw.flush();
bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream f = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(f);
BufferedReader br = new BufferedReader(reader);
String line = br.readLine();
while(line!=null){
System.out.println(line);
line = br.readLine();
}
br.close();
f.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
网络连接
private static String file;
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("主机名称"+address.getHostName());
System.out.println("ip名称"+address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8080);
System.out.println("服务器启动!");
Socket socket = server.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
System.out.println("有链接进入"+s);
} catch (IOException e) {
e.printStackTrace();
}
只能发送一句话
public class MyClient {
public static void main(String[] args) {
try {
System.out.println("客户端启动");
Socket socket = new Socket("192.168.0.41",8080);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("你好!杜\n");
bw.flush();
}catch(UnknownHostException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
互相发送
服务器程序
package com.lingzhuo.lei;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class MySever {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8080);
System.out.println("服务器启动!");
Socket socket = server.accept();
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
Scanner input = new Scanner(System.in);
while(true){
String s = br.readLine();
System.out.println(s);
String back = input.next();
bw.write(back+"\n");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端程序
package com.lingzhuo.lei;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class MyClient {
public static void main(String[] args) {
try {
System.out.println("客户端启动");
Socket socket = new Socket("192.168.0.41",8080);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
Scanner input = new Scanner(System.in);
while(true){
String s= input.next();
bw.write(s+"\n");
bw.flush();
String back = br.readLine();
System.out.println(back);
}
}catch(UnknownHostException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
collections .sort
按年龄排序
public static void main(String[] args) {
ArrayList<Student> clazz = new ArrayList<>();
clazz.add(new Student("张三",15));
clazz.add(new Student("李四",12));
clazz.add(new Student("王五",13));
clazz.add(new Student("赵六",18));
clazz.add(new Student("马七",17));
clazz.add(new Student("小明",18));
clazz.add(new Student("小红",14));
Collections.sort(clazz,new StuComparator());
for(Student stu:clazz){
System.out.println(stu.getName()+stu.getAge());
}
public class StuComparator implements Comparator <Student>{
@Override
public int compare(Student stu1,Student stu2) {
return stu2.getAge()-stu1.getAge();
}
public class Student {
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
List应用
public static void main(String[] args) {
ArrayList<Print> array = new ArrayList<>();
array.add(new PrintHP());
array.add(new PrintHP());
array.add(new PrintHP());
array.add(new PrintHP());
array.add(new PrintHP());
for(Print p :array){
}
for (int i = 0; i < array.size(); i++) {
Print p = array.get(i);
System.out.println(i);
}
}
public static void main(String[] args) {
ArrayList <String> list2 = new ArrayList<>();
ArrayList list1 = new ArrayList<>();
String s = "abc";
list1.add(s);
list2.add("dsd");
list2.add("dff");
list2.add("fsd");
list2.add(1,"abc");
System.out.println(list2);
HashMap的使用
Iterator 迭代器
![map的使用](https://img-blog.csdn.net/20150802200516791)
泛型