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();
    }
}
發佈了32 篇原創文章 · 獲贊 0 · 訪問量 7461
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章