测试
package cn.test; import java.util.Scanner; public class Test01 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.print("请输入一串字符:"); String str = sc.next(); str = str.replace("wqnmlgb", "*"); str = str.replace("ndy", "*"); str = str.replace("dsb", "*"); System.out.println(str); } }
package cn.test; import java.util.Scanner; public class Test02 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入要一组字符:"); String old = sc.next(); String[] gd = old.split(""); StringBuffer xin = new StringBuffer(" "); for(int i = 0;i < gd.length;i++) { if(gd[i].matches("[0-9]")) { xin.append(gd[i]); } } System.out.println(String.valueOf(xin)); } }
不会
package cn.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Test04 { public static void main(String[] args) { //读 BufferedReader br = null; InputStreamReader isr = null; FileInputStream fis = null; //写 BufferedWriter bw = null; FileOutputStream fos = null; OutputStreamWriter osw = null; try { fis = new FileInputStream("F:\\a.java"); isr = new InputStreamReader(fis); br = new BufferedReader(isr); fos = new FileOutputStream("F:\\b.java"); osw = new OutputStreamWriter(fos); bw = new BufferedWriter(osw); String str=null; while((str=br.readLine())!=null) { bw.write(str); } bw.flush(); System.out.println("copy完成"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { bw.close(); osw.close(); fos.close(); br.close(); isr.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package cn.test; import java.text.SimpleDateFormat; import java.util.Date; public class Test05 { public static void main(String[] args) { while(true) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss");//yyyy-MM-dd System.out.println(sdf.format(date)); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package cn.test; public class TestThread061 extends Thread{ public void run() { for(int i = 0;i <100;i++){ System.out.println(Thread.currentThread().getName()+"说:你是谁啊?"); } } } package cn.test; public class TestThread062 implements Runnable{ @Override public void run() { for(int i = 0;i < 100;i++) { System.out.println(Thread.currentThread().getName()+"说:我是查水表。"); } } } package cn.test; public class Test06 { public static void main(String[] args) { TestThread061 r = new TestThread061(); TestThread062 j = new TestThread062(); Thread r1 = new Thread(r,"rose"); Thread j1 = new Thread(j,"jack"); r1.setPriority(10); j1.setPriority(9); r1.start(); j1.start(); } }
不能正常对话(待完善)
package cn.test; public class Penguin { private String name; private String sex; public Penguin() { super(); } public Penguin(String name, String sex) { super(); this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } package cn.test; import java.util.ArrayList; import java.util.List; public class Test07 { static { } public static void main(String[] args) { Penguin p1 = new Penguin ("欧欧", "Q仔"); Penguin p2 = new Penguin ("亚亚", "Q妹"); Penguin p3 = new Penguin ("菲菲", "Q妹"); Penguin p4 = new Penguin ("美美", "Q妹"); List<Penguin>list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); System.out.println("共有"+list.size()+"只企鹅"); for(Penguin qie:list) { System.out.println(qie.getName()+"\t"+qie.getSex()); } for(int i = 0;i < list.size();i++) { if(list.get(i).getName().equals("菲菲")) { list.remove(i); } } for(int i = 0;i < list.size();i++) { if(list.get(i).getName().equals("美美")) { list.remove(i); } } System.out.println("删除后还有"+list.size()+"只企鹅"); for(Penguin qie:list) { System.out.println(qie.getName()+"\t"+qie.getSex()); } for(int i = 0;i < list.size();i++) { if(!list.get(i).getName().equals("美美")) { System.out.println("不存在美美的信息"); break; } } } }
package cn.test; import java.util.Scanner; public class Test08 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println("******欢迎进入注册系统******"); System.out.print("请输入用户名:"); String name= sc.next(); for(;name.length()<3;name=sc.next()) { System.out.print("用户名长度小于3,请重新输入用户名:"); } System.out.print("请输入注册密码:"); String code1 = sc.next(); for(;code1.length()<6;code1=sc.next()) { System.out.print("密码长度小于6位,请重新输入注册密码:"); } System.out.println("请再次输入密码:"); String code2 = sc.next(); for(;!code1.equals(code2);code2=sc.next()) { System.out.print("两次密码输入不一致,请重新输入密码:"); } System.out.print("注册成功,请牢记用户名和密码"); } }
package cn.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class Test09C { public static void main(String[] args) { try { //创建客户端 //发送请求到服务器 Socket socket = new Socket("localhost",5000); //通过输出流发送请求 String info = "用户名:Levi;密码:123456"; OutputStream os = socket.getOutputStream();//字节输出流 byte[] infos = info.getBytes(); os.write(infos); //输入流接受服务器的响应 socket.shutdownOutput(); InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String reply; while((reply = br.readLine())!=null) { System.out.println("服务器说:"+reply); } //释放资源 br.close(); is.close(); os.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package cn.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Test09S { public static void main(String[] args) { //创建一个Socket try { //接受客户端请求 ServerSocket ss = new ServerSocket(5000);//5000端口 //使用accept()侦听并接收到ServerSocket的连接 Socket socket = ss.accept(); //获得输入流,获得用户的请求 InputStream is= socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is));//? String info; while((info = br.readLine())!=null) { System.out.println("客户端说"+info); } //给客户端一个响应 String reply = "欢迎登录"; //通过输出流将相应发送到客户端 OutputStream os = socket.getOutputStream(); os.write(reply.getBytes()); os.close(); br.close(); is.close(); socket.close(); ss.close(); } catch (IOException e) { e.printStackTrace(); } } }
package cn.test; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Test10 { public static void main(String[] args) { FileInputStream fis = null; DataInputStream dis = null; FileOutputStream fos = null; DataOutputStream dos = null; try { //读出来 fis = new FileInputStream("F:\\专业\\a.png"); dis= new DataInputStream(fis); //写进去 fos = new FileOutputStream("F:\\专业\\b.png"); dos = new DataOutputStream(fos); int temp= -1; while((temp=dis.read())!=-1) { dos.write(temp); } dos.flush(); System.out.println("复制完成"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { fos.close(); dis.close(); fis.close(); dos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
部分没有完成的看看其他同学的