08-02 文件IO流 网络通信(TCP) 集合 泛型

文件写入、读出

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值