使用Java实现读取手机文件名称

 更新时间:2024年03月12日 09:50:32   作者:LI耳  
这篇文章主要为大家详细介绍了如何使用Java实现读取手机文件名称,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

首先,确保你已经连接了你的手机并已启用 USB 调试模式。然后,你需要使用 Android Debug Bridge(ADB)工具来获取手机文件列表。以下是一个简单的 Java 代码片段,使用 ProcessBuilder 调用 ADB 命令来获取文件列表:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ADBFileLister {

    public static void main(String[] args) {
        String adbPath = "path/to/adb";  // 替换为你的 adb 路径
        String deviceID = "your_device_id";  // 如果只连接了一个设备,可以为 null

        // Android 文件路径
        String androidPath = "/storage/emulated/0/BaiduNetdisk/我的学习";

        List<String> fileList = listFiles(adbPath, deviceID, androidPath);

        // 打印文件列表
        for (String file : fileList) {
            System.out.println(file);
        }
    }

    private static List<String> listFiles(String adbPath, String deviceID, String androidPath) {
        List<String> fileList = new ArrayList<>();

        // 构建 ADB 命令
        List<String> command = new ArrayList<>();
        command.add(adbPath);
        if (deviceID != null) {
            command.add("-s");
            command.add(deviceID);
        }
        command.add("shell");
        command.add("ls");
        command.add(androidPath);

        // 执行 ADB 命令
        try {
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                fileList.add(line);
            }

            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        return fileList;
    }
}

请注意

替换 adbPath 为你本地的 ADB 工具的路径,

deviceID 为你设备的 ID(可以通过 adb devices 查看),

androidPath 为你想要列出文件的 Android 设备路径。

此代码片段调用 ADB 命令 adb shell ls,并将输出的文件列表读取到 fileList 中。请确保你的手机已连接,并且 ADB 工具的路径正确。

方法补充

下面小编为大家整理了Java读取文件的方法大全,希望对大家有所帮助

1、按字节读取文件内容

2、按字符读取文件内容

3、按行读取文件内容

4、随机读取文件内容

public class ReadFromFile {
    /**
     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
     */
    public static void readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        try {
            System.out.println("以字节为单位读取文件内容,一次读一个字节:");
            // 一次读一个字节
            in = new FileInputStream(file);
            int tempbyte;
            while ((tempbyte = in.read()) != -1) {
                System.out.write(tempbyte);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        try {
            System.out.println("以字节为单位读取文件内容,一次读多个字节:");
            // 一次读多个字节
            byte[] tempbytes = new byte[100];
            int byteread = 0;
            in = new FileInputStream(fileName);
            ReadFromFile.showAvailableBytes(in);
            // 读入多个字节到字节数组中,byteread为一次读入的字节数
            while ((byteread = in.read(tempbytes)) != -1) {
                System.out.write(tempbytes, 0, byteread);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 以字符为单位读取文件,常用于读文本,数字等类型的文件
     */
    public static void readFileByChars(String fileName) {
        File file = new File(fileName);
        Reader reader = null;
        try {
            System.out.println("以字符为单位读取文件内容,一次读一个字节:");
            // 一次读一个字符
            reader = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
                // 但如果这两个字符分开显示时,会换两次行。
                // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
                if (((char) tempchar) != '\r') {
                    System.out.print((char) tempchar);
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            System.out.println("以字符为单位读取文件内容,一次读多个字节:");
            // 一次读多个字符
            char[] tempchars = new char[30];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(fileName));
            // 读入多个字符到字符数组中,charread为一次读取字符数
            while ((charread = reader.read(tempchars)) != -1) {
                // 同样屏蔽掉\r不显示
                if ((charread == tempchars.length)
                        && (tempchars[tempchars.length - 1] != '\r')) {
                    System.out.print(tempchars);
                } else {
                    for (int i = 0; i < charread; i++) {
                        if (tempchars[i] == '\r') {
                            continue;
                        } else {
                            System.out.print(tempchars[i]);
                        }
                    }
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 以行为单位读取文件,常用于读面向行的格式化文件
     */
    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 随机读取文件内容
     */
    public static void readFileByRandomAccess(String fileName) {
        RandomAccessFile randomFile = null;
        try {
            System.out.println("随机读取一段文件内容:");
            // 打开一个随机访问文件流,按只读方式
            randomFile = new RandomAccessFile(fileName, "r");
            // 文件长度,字节数
            long fileLength = randomFile.length();
            // 读文件的起始位置
            int beginIndex = (fileLength > 4) ? 4 : 0;
            // 将读文件的开始位置移到beginIndex位置。
            randomFile.seek(beginIndex);
            byte[] bytes = new byte[10];
            int byteread = 0;
            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
            // 将一次读取的字节数赋给byteread
            while ((byteread = randomFile.read(bytes)) != -1) {
                System.out.write(bytes, 0, byteread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomFile != null) {
                try {
                    randomFile.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 显示输入流中还剩的字节数
     */
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("当前字节输入流中的字节数为:" + in.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String fileName = "C:/temp/newTemp.txt";
        ReadFromFile.readFileByBytes(fileName);
        ReadFromFile.readFileByChars(fileName);
        ReadFromFile.readFileByLines(fileName);
        ReadFromFile.readFileByRandomAccess(fileName);
    }
}

5、将内容追加到文件尾部

public class AppendToFile {
    /**
     * A方法追加文件:使用RandomAccessFile
     */
    public static void appendMethodA(String fileName, String content) {
        try {
            // 打开一个随机访问文件流,按读写方式
            RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
            // 文件长度,字节数
            long fileLength = randomFile.length();
            //将写文件指针移到文件尾。
            randomFile.seek(fileLength);
            randomFile.writeBytes(content);
            randomFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * B方法追加文件:使用FileWriter
     */
    public static void appendMethodB(String fileName, String content) {
        try {
            //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String fileName = "C:/temp/newTemp.txt";
        String content = "new append!";
        //按方法A追加文件
        AppendToFile.appendMethodA(fileName, content);
        AppendToFile.appendMethodA(fileName, "append end. \n");
        //显示文件内容
        ReadFromFile.readFileByLines(fileName);
        //按方法B追加文件
        AppendToFile.appendMethodB(fileName, content);
        AppendToFile.appendMethodB(fileName, "append end. \n");
        //显示文件内容
        ReadFromFile.readFileByLines(fileName);
    }
}

到此这篇关于使用Java实现读取手机文件名称的文章就介绍到这了,更多相关Java读取手机文件名称内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring Boot集成 Spring Boot Admin 监控

    Spring Boot集成 Spring Boot Admin 监控

    这篇文章主要介绍了Spring Boot集成 Spring Boot Admin 监控,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • @Schedule 如何解决定时任务推迟执行

    @Schedule 如何解决定时任务推迟执行

    这篇文章主要介绍了@Schedule 如何解决定时任务推迟执行问题。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • SpringBoot中事务失效的原因详解

    SpringBoot中事务失效的原因详解

    这篇文章主要介绍了SpringBoot中事务失效的原因详解,spring中的事务是依赖AOP的,AOP是通过动态代理实现的,只有通过代理类访问的方法才能被拦截,需要的朋友可以参考下
    2023-10-10
  • Java利用StampedLock实现读写锁的方法详解

    Java利用StampedLock实现读写锁的方法详解

    在jdk8以后,java提供了一个性能更优越的读写锁并发类StampedLock,该类的设计初衷是作为一个内部工具类,用于辅助开发其它线程安全组件。本文就来和大家一起学习下StampedLock的功能和使用
    2022-10-10
  • elasticsearch索引的创建过程index create逻辑分析

    elasticsearch索引的创建过程index create逻辑分析

    这篇文章主要介绍了elasticsearch索引核心index create,索引的创建过程解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • Redis实现商品秒杀功能页面流程

    Redis实现商品秒杀功能页面流程

    这篇文章主要介绍了Redis实现商品秒杀功能的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • Java订单30分钟未支付自动取消该怎么实现

    Java订单30分钟未支付自动取消该怎么实现

    在开发中往往会遇到一些关于延时任务的需求,例如生成订单30分钟未支付,则自动取消,下面这篇文章主要给大家介绍了关于Java订单30分钟未支付自动取消该怎么实现的相关资料,需要的朋友可以参考下
    2023-03-03
  • SpringBoot自定义Starter及使用

    SpringBoot自定义Starter及使用

    这篇文章主要介绍了SpringBoot自定义Starter及使用,Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境进行自动配置,需要的朋友可以参考下
    2023-07-07
  • redis之基于SpringBoot实现Redis stream实时流事件处理方式

    redis之基于SpringBoot实现Redis stream实时流事件处理方式

    这篇文章主要介绍了redis之基于SpringBoot实现Redis stream实时流事件处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Java编程在ICPC快速IO实现源码

    Java编程在ICPC快速IO实现源码

    这篇文章主要介绍了Java Fast IO in ICPC实现源码,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09

最新评论