Java-Android

Posted 怪歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-Android相关的知识,希望对你有一定的参考价值。

BasicsTwo(java)

反射

修改方法

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<String> list = new ArrayList<>();
        list.add("oiuoaisduf");
        list.add("asljkdf");

        Class<? extends ArrayList> aClass1 = list.getClass();       //通过对象名获取class
        Class<ArrayList> aClass2 = ArrayList.class;                 //通过集合获取class
        Class<?> aClass3 = Class.forName("java.util.ArrayList");    //通过包和类名获取class


        Method add = aClass1.getDeclaredMethod("add", Object.class);

        add.invoke(list,5.21345689756);
        add.invoke(list,123456789);
        add.invoke(list,5.21345689756);

        System.out.println(list);

    }

修改值

private static HashMap<String,User> map = new HashMap<>();
    static {
        User no1 = new User("wangyide", "5210");
        User no2 = new User("aaa", "123");
        map.put("user1",no1);
        map.put("user2",no2);
    }
    public static void main(String[] args) {
        Class<HashMap> hashMapClass = HashMap.class;
        Collection<User> values = map.values();
        Field[] declaredFields = hashMapClass.getDeclaredFields();
    }

简易IO(面向文件的增删改查)

public class Demo {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        start();
    }

    private static void start() throws Exception {
        System.out.println("1.文件目录操作");
        System.out.println("0.退出");
        System.out.println("请输入您的选择:");
        switch (scanner.nextInt())
        {
            case 1:
                System.out.println("1:D:\\\\ 2.C:\\\\");
                System.out.println("请选择路径");
                File file = new File("D:\\\\");
                File file1 = new File("C:\\\\");
                if (scanner.nextInt()==2)
                {
                    show(file1);
                }else {
                    show(file);
                }
                start();
                break;
            case 0:
                System.out.println("谢谢使用");
                System.exit(0);
                break;
            default:
                System.out.println("输入错误,请重新输入");
                start();
                break;
        }
    }

    private static void show(File file1) throws Exception {
        if (file1.isDirectory())
        {
            File[] files = file1.listFiles();
            for (File s:
             files) {
                System.out.println(s.getName());
            }
            System.out.println("1.继续打开 2.创建文件 3.创建文件夹 4.删除文件或文件夹 5.返回上一级 0.退出");
            int i = scanner.nextInt();
            if (i==1)
            {
                System.out.println("请输入文件名:");
                String name = scanner.next();
                for (File s:
                     files) {
                    if (name.equals(s.getName()))
                    {
                        show(s);
                    }
                }
            }else if (i==2)
            {
                System.out.println("请输入文件名(加后缀名如:.txt):");
                String name = scanner.next();
                File file = new File(file1+"/"+name);
                file.createNewFile();
            }else if (i==3)
            {
                System.out.println("请输入文件夹名:");
                String name = scanner.next();
                File file = new File(file1+"/"+name);
                file.mkdir();
            }else if (i==4)
            {
                System.out.println("请输入要删除的文件夹:");
                String name = scanner.next();
                File file6 = new File(file1+"/"+name);
                deleteFile(file6);
                System.out.println("删除成功");
            }else if (i==0)
            {
                System.exit(0);
            }else if (i==5)
            {
                String path = file1.getPath();
                File parentFile = file1.getParentFile();
                show(parentFile);
            }
            else {
                System.out.println("输入错误");
            }
        }
    }

    private static void deleteFile(File file) {
        if (file.exists())
        {
            if (file.isFile())
            {
                file.delete();
            }else {
                if (file.length()==0)
                {
                    file.delete();
                }
                File[] files = file.listFiles();
                for (File f:
                     files) {
                    if (f.isFile())
                    {
                        f.delete();
                    }else {
                        deleteFile(f);
                    }
                }
                file.delete();
            }
        }else {
            System.out.println("不存在此文件");
        }
    }
}

IO流

缓冲流
缓冲流是用来优化字节或字符流的,
一般字符流比字节流读写要快,但字节流可读取写入除字符以外的数据

序列化流
序列化流可以读取或写入对象,但直接写入会造成乱码

转换流
转换流用来将字节流和字符流相互替换
一般读取JSON-url中的数据获取的流为字节流
一般编程会把字节流转换为字符流
加上缓冲流达到最佳效果

(文件字节输入流)FileInputStream

		FileInputStream fileInputStream = new FileInputStream("字节流.txt");

        byte[] bs = new byte[1024];  //用来存 读到的内容
        int len = fileInputStream.read(bs);  //len:读到的长度

        String string = new String(bs,0,len);
        System.out.println(string);

        fileInputStream.close();

(文件字节输出流) FileOutputStream

		FileOutputStream fileOutputStream = new FileOutputStream("字节流.txt");
        fileOutputStream.write("你好".getBytes());
        fileOutputStream.close();

(文件字符输入流)FileReader

		FileReader fileReader = new FileReader("字符流.txt");
        char[] cs = new char[1024];  //保存读到的内容
        int len = fileReader.read(cs);
        String string = new String(cs,0,len);

        System.out.println(string);
        fileReader.close();

(文件字符输出流)FileWriter

		FileWriter fileWriter = new FileWriter("字符流.txt");
        fileWriter.write("你好");
        fileWriter.close();

【字节流复制(边读边写)】

 //读
        FileInputStream fileInputStream = new FileInputStream("D:\\\\install\\\\ideaIU-2020.1.3.exe");
        //写
        FileOutputStream fileOutputStream = new FileOutputStream("D:/aaa.exe");

        byte[] bs = new byte[1024]; //存储读取到的内容

        while (true) {
            int len = fileInputStream.read(bs);
            if (len==-1) {
                break;
            }
            fileOutputStream.write(bs, 0, len);
        }
        fileInputStream.close();
        fileOutputStream.close();

(缓冲输出字符流)BufferedWriter

		FileWriter fileWriter = new FileWriter("缓冲字符流.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        bufferedWriter.write("缓冲字符流,你好");
        bufferedWriter.close();

(缓冲输入字符流) BufferedReader

		FileReader fileReader = new FileReader("缓冲字符流.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

//		char[] cs = new char[1024];
//
//		int len = bufferedReader.read(cs);
//
//		String string = new String(cs, 0, len);
//		System.out.println(string);

        //特有的方法  读一行
//        String readLine = bufferedReader.readLine();
//        System.out.println(readLine);
		
		//一般使用方法
		String s = "";
		StringBuffer stringbuffer = new StringBuffer();
		while((s = bufferedReader.readLine())!=null){
			stringbuffer.append(s);
		}
		
		System.out.println(stringbuffer.toString());
        
        bufferedReader.close();

(字节字符转换流)

		//写
		FileOutputStream fileOutputStream = new FileOutputStream("转换流.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream , "UTF-8");

        outputStreamWriter.write("转换流你好");
        outputStreamWriter.close();
		
		//读
		FileInputStream fileInputStream = new FileInputStream("转换流.txt");

        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");

        char[] cs = new char[1024];
        int len = inputStreamReader.read(cs);
        String string = new String(cs, 0, len);
        System.out.println(string);
        inputStreamReader.close();

(序列化输入流)ObjectInputStream

		FileInputStream fileInputStream = new FileInputStream("序列化流.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

        Student readObject = (Student) objectInputStream.readObject();

        System.out.println(readObject);

        objectInputStream.close();

(序列化输出流)ObjectOutputStream

		FileOutputStream fileOutputStream = new FileOutputStream("序列化流.txt");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        Student student = new Student("张三");

        objectOutputStream.writeObject(student);
        objectOutputStream.close();

网络编程

简单群聊系统

Server类

服务类,用于循环获取客户端的连接,并创建每个连接客户端伴生的服务子线程类

public class Server {
    //list用于存储连接中的Socket(存储在线好友)一般用HashMap存储
    //再使用list存储User类
    private static ArrayList<Socket> list = new ArrayList<>();
    
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(37618);
        System.out.println("等待连接中...........");
        int i=0;
        while (true)
        {
            Socket accept = serverSocket.accept();
            list.add(accept);
            System.out.println("连接成功");
            new ServerThread(list,accept,i).start();
        }

    }
}

ServerThread类

public class ServerThread extends Thread{
    private ArrayList<Socket> arrayList;
    private Socket socket;
    private int i;

    public ServerThread(ArrayList<Socket> arrayList, Socket socket, int i) {
        Java-Android

在 Java-Android 中设置链接可点击

在 Java-Android 和 JNI 之间发送/接收音频数组

Java-Android 设备 BLE 上的数据解析

java-Android-常用十大排序算法

java-Android-常用十大排序算法