文件写入、读出
public class Test {
public static void main(String[] args) {
File file=new File("d://s.txt");
try {
FileOutputStream fos=new FileOutputStream(file);
OutputStreamWriter writer=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(writer);
bw.write("九步踏天");
bw.flush();
bw.close();
writer.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream fis=new FileInputStream(file);
InputStreamReader reader=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(reader);
String line=br.readLine();
while(line!=null){
System.out.println(line);
line=br.readLine();
}
br.close();
reader.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件复制
public class Test {
public static void main(String[] args) {
File file=new File("d://s.txt");
File filecopy=new File("d://a.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();
}
fos.flush();
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
IP地址
public class Test {
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 class MyClient {
public static void main(String[] args) {
try {
Socket socket=new Socket("192.168.0.141", 8080);
System.out.println("客户端启动");
InputStream is=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
OutputStream os=socket.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
Scanner scanner=new Scanner(System.in);
while(true){
String s=scanner.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();
}
}
}
public class MyServer {
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);
OutputStream os=socket.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
Scanner scanner=new Scanner(System.in);
while(true){
String s=br.readLine();
System.out.println("客户端:"+s);
String back=scanner.next();
bw.write(back+"\n");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
集合
public class Test {
public static void main(String[] args) {
ArrayList<String> list1=new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("b");
list1.add(1, "d");
System.out.println(list1.get(0));
System.out.println(list1.indexOf("b"));
System.out.println(list1.lastIndexOf("b"));
for (int i = 0; i < list1.size(); i++) {
System.out.println(list1.get(i));
}
System.out.println(list1.contains("a"));
ArrayList<String> list2=new ArrayList<>();
list2.addAll(list1);
list2.remove(0);
list2.remove("a");
for (int i = 0; i < list2.size(); i++) {
System.out.println(list2.get(i));
}
HashSet<Integer> set=new HashSet<>();
Random random=new Random();
int count=0;
while(set.size()<10){
int i=random.nextInt(90);
System.out.println(count+++" " +"随机得到数据:"+i);
set.add(i);
}
Iterator<Integer> it=set.iterator();
while(it.hasNext()){
int i=it.next();
System.out.println(i);
}
System.out.println("已经放进去了"+set.size());
HashSet<String> set1=new HashSet<>();
set1.add("a");
set1.add("b");
for(String s:set1){
System.out.println(s);
}
HashMap<String, Student> map=new HashMap<>();
Student zhangsan=new Student("张三");
String name="张三";
map.put(name, zhangsan);
map.put(null, new Student("key是空的"));
map.put("lisi",new Student("李斯"));
map.put("wangwu", new Student("王五"));
System.out.println(map.size());
Set<String> keys=map.keySet();
Iterator<String> it=keys.iterator();
while(it.hasNext()){
String key=it.next();
Student stu=map.get(key);
System.out.println(stu.getName());
}
ArrayList<Student> clazz1=new ArrayList<>();
clazz1.add(new Student("张三",18));
clazz1.add(new Student("李斯",23));
clazz1.add(new Student("王五",21));
clazz1.add(new Student("小明",17));
clazz1.add(new Student("小红",22));
Collections.sort(clazz1,new StuComparator());
for(Student stu:clazz1){
System.out.println(stu.getName()+stu.getAge());
}
public class StuComparator implements Comparator<Student>{
@Override
public int compare(Student stu1, Student stu2) {
return stu1.getAge()-stu2.getAge();
}
}
泛型
public class NanZhuang {
}
public class NvZhuang {
}
public class Cat extends Pat{
public void voice(){
System.out.println("喵喵");
}
}
public class Dog extends Pat{
public void voice(){
System.out.println("汪汪");
}
}
public class Pat {
}
public class Teacher<T,E> {
private String name;
private int age;
private T clothes;
private E pat;
public T getClothes() {
return clothes;
}
public void setClothes(T clothes) {
this.clothes = clothes;
}
public E getPat() {
return pat;
}
public void setPat(E pat) {
this.pat = pat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Test {
public static void main(String[] args) {
Teacher<NanZhuang,Dog> lisi=new Teacher<>();
NanZhuang nan=new NanZhuang();
lisi.setName("李斯");
lisi.setAge(18);
lisi.setClothes(nan);
lisi.setPat(new Dog());
lisi.getPat();
Dog dog=lisi.getPat();
}
}