java 常用代码块

慢更

一 文件相关

1 判断文件编码类型(gbk/utf-8)

原本参考了下述写法,但发现有些问题,后发现txt文件扫出来的头信息和下述提到的均不相同,于是又添加了扫描头信息的方法,根据头信息和实际的编码类型,使用时再对代码进行调整。

    public static String getFileCharsetName(String fileName) throws IOException {
        InputStream inputStream = new FileInputStream(fileName);
        byte[] head = new byte[3];
        inputStream.read(head);

        String charsetName = "GBK";//或GB2312,即ANSI
        if (head[0] == -1 && head[1] == -2 ) //0xFFFE
            charsetName = "UTF-16";
        else if (head[0] == -2 && head[1] == -1 ) //0xFEFF
            charsetName = "Unicode";//包含两种编码格式:UCS2-Big-Endian和UCS2-Little-Endian
        else if(head[0]==-27 && head[1]==-101 && head[2] ==-98)
            charsetName = "UTF-8"; //UTF-8(不含BOM)
        else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
            charsetName = "UTF-8"; //UTF-8-BOM
        else if(head[0]== 91 && head[1]==48 && head[2] == 48)
            charsetName = "UTF-8"; //UTF-8
        inputStream.close();

        //System.out.println(code);
        return charsetName;
    }

获取头信息

java.io.InputStream ios=new java.io.FileInputStream(file);
            byte[] b=new byte[3];
            ios.read(b);
            ios.close();
            System.out.println("编码为"+b[0] +"bian"+b[1] +"bian"+ b[2] );

2 文件路径转换

1 将/storage/emulated转为file:path类型

// 原始路径 
String originalPath = "/storage/emulated";
// 创建 File 对象 
File file = new File(originalPath);
// 获取 file:path 类型的路径 String fileUriPath = file.toURI().toString();
// 输出结果`
System.out.println("原始路径: " + originalPath);`
System.out.println("file:path 类型的路径: " + fileUriPath);

原始路径: /storage/emulated
file:path 类型的路径: file:///storage/emulated

2 将content://转为真实路径

例:/external/audio/media/693
音乐类

 public String getRealPathFromURI(String uriString) {
    
    
   //若已是 content://media/external/audio/media/693 形式,则无需添加前半部分
        Uri uri = Uri.parse("content://media/" + uriString);
        String[] projection = {
    
     MediaStore.Audio.Media.DATA };

        // 获取 ContentResolver
        ContentResolver contentResolver = getContentResolver();

        Cursor cursor = contentResolver.query(uri, projection, null, null, null);
        if (cursor != null) {
    
    
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            cursor.moveToFirst();
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            return filePath;  // 返回文件路径
        }
        return null;  // 没有找到路径
    }

二 字符串相关(中英文/排序等)

1 判断是否包含英文字母

public static boolean containsAllLetters(String keyword) {
    // 创建一个包含所有26个英文字母的集合
    Set<Character> allLetters = new HashSet<>();
    for (char c = 'a'; c <= 'z'; c++) {
        allLetters.add(c);
    }
    // 将 keyword 转换成小写并创建一个集合
    Set<Character> keywordLetters = new HashSet<>();
    for (char c : keyword.toLowerCase().toCharArray()) {
        if (Character.isLetter(c)) {
            keywordLetters.add(c);
        }
    }
    // 检查 keywordLetters 是否包含 allLetters 中的所有元素
    return allLetters.containsAll(keywordLetters);
}

2判断字符串是否存在于某列表中

3根据实体类的某个属性进行排序

a-z排序
只有英文/拼音
Person为实体类

 // 使用Comparator进行排序
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                return p1.getName().compareTo(p2.getName());
            }
        });

中英文混合

猜你喜欢

转载自blog.csdn.net/weixin_52282455/article/details/140851025