马上就要过年了,今天是二月最后一天上班了,心情有些激动,恨不得立马就到家去。再来公司的时候就是3.1号了,所以在离开之前把2月份的小程序们储存起来。虽然很多都是参考了网上的小程序练习,但是自己有自己的风格,感觉存起来意义很大的,这样坚持了两个月不到,感觉很多基础的东西有了更加清晰的概念,解决很多问题的时候都会到原点去想问题了。
package everyworkdayprogramming._2015_2_02;/** * * 将字符串还原成:“我要学编程”。如:我...我.要.... * * */public class Java_2_2 { public static void main(String[] args) { String str = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程"; str = str.replaceAll("\\.+", ""); System.out.println("去点以后--->"+str); str=str.replaceAll("(.)\\1+", "$1"); System.out.println(str); }}
package everyworkdayprogramming._2015_2_03;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;/** * 模拟UDP协议做一个聊天工具 * * * */public class Java_2_3_server { public static void main(String[] args) throws IOException { // 创建发送服务端 DatagramSocket ds = new DatagramSocket(); // 创建键盘录入对象,并用转换流转换后高效处理 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); String s = null; while ((s = bufferedReader.readLine()) != null) { // 把每一行数据进行封装 byte[] b = s.getBytes(); String str = "192.168.1.221"; String[] ipStr = str.split("\\."); byte[] ipBuf = new byte[4]; for (int i = 0; i < 4; i++) { System.out.println(Integer.parseInt(ipStr[i])&0xff); ipBuf[i] = (byte) (Integer.parseInt(ipStr[i]) & 0xff); } DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByAddress(ipBuf), 9900); ds.send(dp); } ds.close(); bufferedReader.close(); }}
package everyworkdayprogramming._2015_2_03;import java.net.DatagramPacket;import java.net.DatagramSocket;public class Java_2_3_receiver { public static void main(String[] args) throws Exception { // 创建接收端的服务 DatagramSocket ds = new DatagramSocket(9900); // 接收数据 while (true) { byte[] bye = new byte[1024]; // 调用接收方法接收数据 DatagramPacket dp = new DatagramPacket(bye, bye.length); ds.receive(dp); // 打印数据 System.out.println(dp.getAddress()+ "发来" + new String(dp.getData(), 0, dp.getLength())); } }}
package everyworkdayprogramming._2015_2_04;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class Java_2_4_server { /** * * * 模拟Tcp协议的传输过程 * * */ public static void main(String[] args) throws Exception { // 定义服务器接收端口 ServerSocket serverSocket = new ServerSocket(65533); // 获取服务器Socket接口 Socket s = serverSocket.accept(); // 获取读取客户端的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 定义输出客户端数据的流 PrintWriter printWriter = new PrintWriter(System.out, true); // 获取反馈给客户端的流 PrintWriter pw = new PrintWriter(s.getOutputStream(), true); // 写入到客户端的控制台 String str = null; while ((str = br.readLine()) != null) { printWriter.println(str); // 返回数据 pw.println("收到"); } serverSocket.close(); s.close(); }}
package everyworkdayprogramming._2015_2_04;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;public class Java_2_5_client { public static void main(String[] args) throws Exception { // 定义客户端端口 Socket socket = new Socket("192.168.1.221", 65533); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); OutputStream out = socket.getOutputStream(); PrintWriter pw = new PrintWriter(out, true); // 定义读取服务器的字符流 BufferedReader br1 = new BufferedReader(new InputStreamReader( socket.getInputStream())); String s1 = null; // 写入服务器 while ((s1 = br.readLine()) != null) { if ("over".equals(s1)) { break; } System.out.println("---------->"+s1); pw.println(s1); // 读取服务器的数据 String s3 = br1.readLine(); System.out.println(s3); } pw.close(); br.close(); br1.close(); socket.close(); }}
package everyworkdayprogramming._2015_2_05;import java.net.InetAddress;import java.net.UnknownHostException;/** * * 通过主机名和IP地址获得对方 * * */public class Java_2_5 { public static void main(String[] args) throws UnknownHostException { InetAddress id1 = InetAddress.getByName("mpc-pc"); InetAddress id = InetAddress.getByName("192.168.1.221"); String ip = id.getHostAddress(); String name = id.getHostName(); System.out.println(ip + "-----------------" + name); System.out.println(id1.getHostAddress() + "-----------------" + id1.getHostName()); }}
package everyworkdayprogramming._2015_2_06;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;/** * * java的DataOutputStream和DataInputStream * * */public class Java_2_6 { public static void main(String[] args) throws Exception { Member member = new Member("糜鹏程", 23); DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream("d:/member.mpc")); dataOutputStream.writeUTF(member.getName()); dataOutputStream.writeInt(member.getAge()); // 所有数据到目的地 dataOutputStream.flush(); // 关闭流 dataOutputStream.close(); DataInputStream dataInputStream = new DataInputStream(new FileInputStream("d:/member.mpc")); System.out.println(dataInputStream.readUTF()); dataInputStream.close(); }}
package everyworkdayprogramming._2015_2_06;public class Member { private String name; private int age; 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 Member(String name, int age) { super(); this.name = name; this.age = age; } public Member() { super(); }}
package everyworkdayprogramming._2015_2_09;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOError;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.Reader;public class Java_2_9 { /** * * 实现一个包装类,让读出的每一行自动加上行号和:号 * * @throws FileNotFoundException * * */ public static void main(String[] args) throws Exception { File file = new File("d:" + File.separator + "hello2.txt"); Test read = new Test(new InputStreamReader(new FileInputStream(file))); File file2 = new File("d:" + File.separator + "hello22.txt"); PrintWriter printWriter = new PrintWriter(file2); String tem ; while((tem=read.myReadLine())!=null){ printWriter.println(tem); } read.close(); printWriter.close(); file.delete(); file2.renameTo(file); }}class Test extends BufferedReader { private int lineNumber; public Test(Reader in) { super(in); } public String myReadLine() throws IOException { lineNumber++; String tme; if ((tme = super.readLine()) != null) { return lineNumber + ":" + tme; } else { return null; } }}
package everyworkdayprogramming._2015_2_10;import java.io.FileInputStream;import java.io.FileWriter;import java.io.PrintWriter;import java.util.Properties;import java.util.Set;public class Java_2_10 { /** * * 把Properties.txt中键为count的值改为100 * * */ public static void main(String[] args) throws Exception { FileInputStream fs = new FileInputStream("C:/Users/mpc/workspace/basicknowledge/src/everyworkdayprogramming/_2015_2_10/Properties.txt"); Properties pp = new Properties(); pp.load(fs); fs.close(); System.out.println(pp.get("name")); Set
package everyworkdayprogramming._2015_2_11;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;public class Java_2_11 { /** * * 键盘输入转大写在控制台显示 * * */ public static void main(String[] args) throws Exception { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); PrintWriter printWriter = new PrintWriter(System.out, true); String line = null; while ((line = bufferedReader.readLine()) != null) { if (line.equals("break")) break; printWriter.println(line.toUpperCase()); } bufferedReader.close(); printWriter.close(); }}
package everyworkdayprogramming._2015_2_13;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;public class Java_2_13 { /** * * 用字符缓冲流复制一张图片并记算花费时间. * * */ public static void main(String[] args) throws Exception { BufferedInputStream in = new BufferedInputStream(new FileInputStream( "d:result.png")); BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream("d:result1.png")); long l1 = System.currentTimeMillis(); byte[] bye = new byte[1024]; int available = in.available(); int i = 0; while ((in.read(bye)) != -1) { if (i < available / bye.length) { out.write(bye); out.flush(); i++; } else { out.write(bye, 0, available % bye.length); out.flush(); } } long l2 = System.currentTimeMillis(); System.out.println(l2 - l1); in.close(); out.close(); }}
package everyworkdayprogramming._2015_2_15;import java.io.IOException;import java.io.InputStreamReader;import java.io.Reader;public class Java_2_15 { /** * * 自己模拟BufferedReader对象 * * */ public static void main(String[] args) throws IOException { MyBufferedReader bufferedReader = new MyBufferedReader(); bufferedReader.setReader(new InputStreamReader(System.in)); System.out.println(bufferedReader.myReadLine()); bufferedReader.myClose(); }}class MyBufferedReader { private Reader reader; public Reader getReader() { return reader; } public void setReader(Reader reader) { this.reader = reader; } // 模拟一次读取一行数据的方法 public String myReadLine() throws IOException { StringBuilder builder = new StringBuilder(); int ch = 0; while ((ch = reader.read()) != -1) { if (ch == '\r') { continue; } if (ch == '\n') { return builder.toString(); } else { builder.append((char) ch); } } // 没有换行的情况 if (builder.length() > 0) { return builder.toString(); } return null; } // 模拟关闭方法 public void myClose() throws IOException { reader.close(); }}