08-02 文件IO流 网络通信(TCP) 集合 泛型
来源:互联网 发布:java接口怎么用 编辑:程序博客网 时间:2024/06/06 05:11
文件写入、读出
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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
IP地址
public class Test { public static void main(String[] args) { try { InetAddress address=InetAddress.getLocalHost(); //获取主机名 System.out.println("主机名:"+address.getHostName()); //获取IP地址 System.out.println("IP地址:"+address.getHostAddress()); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
客户端、服务器
//客户端(Client)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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}//服务器(Server)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) { // TODO Auto-generated catch block e.printStackTrace(); } }}
集合
public class Test { public static void main(String[] args) { //ArrayList、Set 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); } //Map遍历 /* //有参数的构造器 public Student(String name){ this.name=name; } */ 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()); } //Collection排序 /* //有参数的构造器 public Student(String name,int age){ this.name=name; this.age=age; } */ 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(); }}
0 0
- 08-02 文件IO流 网络通信(TCP) 集合 泛型
- 08-02 文件IO流 网络通信(TCP) 集合 泛型
- java tcp网络通信 传输文件
- Linux网络通信编程(套接字模型TCP\UDP与IO多路复用模型select\poll\epoll)
- 网络编程(2)--TCP通信
- 基于TCP的网络通信(SCOKET)
- 网络通信(TCP和UDP)
- Linux之—网络通信(TCP)
- Linux之—网络通信(TCP)
- 网络通信简单代码(TCP)
- 【网络编程】socket通信流程图(tcp)
- socket网络通信(tcp)
- socket网络通信(tcp)
- TCP网络通信
- tcp 网络通信基本原理
- 网络通信3-TCP
- 网络通信--TCP
- 网络通信 Tcp
- 理解IP地址和端口号
- 不同年龄的表白
- redhat yum破解方法
- MediaMetadataRetriever的用法
- mac下使用APKtool,dex2jar,jd-gui反编译APK
- 08-02 文件IO流 网络通信(TCP) 集合 泛型
- 数据结构之满二叉树和完全二叉树的区别
- Merge Two Sorted Lists
- js浮点运算bug的解决办法
- 奇葩调试记
- sqlite3编程相关注意点
- Emacs 学习(二)
- Code Forces 451 E. Devu and Flowers(组合数学)
- 08-03 网络通信(UDP) URL XML解析器 JSON、DOM、SAX 枚举