最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Java實(shí)現(xiàn)讀取手機(jī)文件名稱

 更新時間:2024年03月12日 09:50:32   作者:LI耳  
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取手機(jī)文件名稱,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

首先,確保你已經(jīng)連接了你的手機(jī)并已啟用 USB 調(diào)試模式。然后,你需要使用 Android Debug Bridge(ADB)工具來獲取手機(jī)文件列表。以下是一個簡單的 Java 代碼片段,使用 ProcessBuilder 調(diào)用 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";  // 如果只連接了一個設(shè)備,可以為 null

        // Android 文件路徑
        String androidPath = "/storage/emulated/0/BaiduNetdisk/我的學(xué)習(xí)";

        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<>();

        // 構(gòu)建 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);

        // 執(zhí)行 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 為你設(shè)備的 ID(可以通過 adb devices 查看),

androidPath 為你想要列出文件的 Android 設(shè)備路徑。

此代碼片段調(diào)用 ADB 命令 adb shell ls,并將輸出的文件列表讀取到 fileList 中。請確保你的手機(jī)已連接,并且 ADB 工具的路徑正確。

方法補(bǔ)充

下面小編為大家整理了Java讀取文件的方法大全,希望對大家有所幫助

1、按字節(jié)讀取文件內(nèi)容

2、按字符讀取文件內(nèi)容

3、按行讀取文件內(nèi)容

4、隨機(jī)讀取文件內(nèi)容

public class ReadFromFile {
    /**
     * 以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
     */
    public static void readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        try {
            System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
            // 一次讀一個字節(jié)
            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("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
            // 一次讀多個字節(jié)
            byte[] tempbytes = new byte[100];
            int byteread = 0;
            in = new FileInputStream(fileName);
            ReadFromFile.showAvailableBytes(in);
            // 讀入多個字節(jié)到字節(jié)數(shù)組中,byteread為一次讀入的字節(jié)數(shù)
            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) {
                }
            }
        }
    }

    /**
     * 以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件
     */
    public static void readFileByChars(String fileName) {
        File file = new File(fileName);
        Reader reader = null;
        try {
            System.out.println("以字符為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
            // 一次讀一個字符
            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("以字符為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
            // 一次讀多個字符
            char[] tempchars = new char[30];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(fileName));
            // 讀入多個字符到字符數(shù)組中,charread為一次讀取字符數(shù)
            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("以行為單位讀取文件內(nèi)容,一次讀一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次讀入一行,直到讀入null為文件結(jié)束
            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) {
                }
            }
        }
    }

    /**
     * 隨機(jī)讀取文件內(nèi)容
     */
    public static void readFileByRandomAccess(String fileName) {
        RandomAccessFile randomFile = null;
        try {
            System.out.println("隨機(jī)讀取一段文件內(nèi)容:");
            // 打開一個隨機(jī)訪問文件流,按只讀方式
            randomFile = new RandomAccessFile(fileName, "r");
            // 文件長度,字節(jié)數(shù)
            long fileLength = randomFile.length();
            // 讀文件的起始位置
            int beginIndex = (fileLength > 4) ? 4 : 0;
            // 將讀文件的開始位置移到beginIndex位置。
            randomFile.seek(beginIndex);
            byte[] bytes = new byte[10];
            int byteread = 0;
            // 一次讀10個字節(jié),如果文件內(nèi)容不足10個字節(jié),則讀剩下的字節(jié)。
            // 將一次讀取的字節(jié)數(shù)賦給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) {
                }
            }
        }
    }

    /**
     * 顯示輸入流中還剩的字節(jié)數(shù)
     */
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("當(dāng)前字節(jié)輸入流中的字節(jié)數(shù)為:" + 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、將內(nèi)容追加到文件尾部

public class AppendToFile {
    /**
     * A方法追加文件:使用RandomAccessFile
     */
    public static void appendMethodA(String fileName, String content) {
        try {
            // 打開一個隨機(jī)訪問文件流,按讀寫方式
            RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
            // 文件長度,字節(jié)數(shù)
            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 {
            //打開一個寫文件器,構(gòu)造函數(shù)中的第二個參數(shù)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");
        //顯示文件內(nèi)容
        ReadFromFile.readFileByLines(fileName);
        //按方法B追加文件
        AppendToFile.appendMethodB(fileName, content);
        AppendToFile.appendMethodB(fileName, "append end. \n");
        //顯示文件內(nèi)容
        ReadFromFile.readFileByLines(fileName);
    }
}

到此這篇關(guān)于使用Java實(shí)現(xiàn)讀取手機(jī)文件名稱的文章就介紹到這了,更多相關(guān)Java讀取手機(jī)文件名稱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于Spring Boot獲取bean的3種方式

    關(guān)于Spring Boot獲取bean的3種方式

    這篇文章主要介紹了關(guān)于Spring Boot獲取bean的3種方式,在spring中ApplicationContext這個上下文對象是獲取bean的基礎(chǔ),需要的朋友可以參考下
    2023-04-04
  • 淺談Java基于Consul創(chuàng)建分布式鎖

    淺談Java基于Consul創(chuàng)建分布式鎖

    這篇文章主要介紹了淺談基于Consul創(chuàng)建分布式鎖,Consul是HashiCorp公司推出的開源工具,用于實(shí)現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置Consul是分布式的、高可用的、可橫向擴(kuò)展的,需要的朋友可以參考下
    2023-07-07
  • JAVA獲取當(dāng)前項目和文件所在路徑的實(shí)例代碼

    JAVA獲取當(dāng)前項目和文件所在路徑的實(shí)例代碼

    這篇文章主要介紹了JAVA獲取當(dāng)前項目和文件所在路徑的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 快速排序算法在Java中的實(shí)現(xiàn)

    快速排序算法在Java中的實(shí)現(xiàn)

    這篇文章主要介紹了快速排序算法在Java中的實(shí)現(xiàn),簡單介紹了快速排序的實(shí)現(xiàn)原理,分享了兩種實(shí)現(xiàn)代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 帶你入門java雪花算法原理

    帶你入門java雪花算法原理

    SnowFlake 算法,是 Twitter 開源的分布式 id 生成算法。其核心思想就是:使用一個 64 bit 的 long 型的數(shù)字作為全局唯一 id。在分布式系統(tǒng)中的應(yīng)用十分廣泛,且ID 引入了時間戳,基本上保持自增的
    2021-06-06
  • Java 網(wǎng)絡(luò)爬蟲基礎(chǔ)知識入門解析

    Java 網(wǎng)絡(luò)爬蟲基礎(chǔ)知識入門解析

    這篇文章主要介紹了Java 網(wǎng)絡(luò)爬蟲基礎(chǔ)知識入門解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • spring boot項目fat jar瘦身的實(shí)現(xiàn)

    spring boot項目fat jar瘦身的實(shí)現(xiàn)

    這篇文章主要介紹了spring boot項目fat jar瘦身的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Java函數(shù)式編程(七):MapReduce

    Java函數(shù)式編程(七):MapReduce

    這篇文章主要介紹了Java函數(shù)式編程(七):MapReduce,本文是系列文章的第7篇,其它文章請參閱本文底部的相關(guān)文章,需要的朋友可以參考下
    2014-09-09
  • SpringBoot項目使用Liquibase數(shù)據(jù)庫版本管理方式

    SpringBoot項目使用Liquibase數(shù)據(jù)庫版本管理方式

    Liquibase是開源數(shù)據(jù)庫版本管理工具,支持多數(shù)據(jù)庫及多格式變更日志,實(shí)現(xiàn)變更追蹤、上下文執(zhí)行、文檔生成等功能,需配置目錄結(jié)構(gòu)及master.xml文件,注意@PostConstruct初始化順序問題,建議用CommandLineRunner替代
    2025-09-09
  • 詳解Java創(chuàng)建線程的五種常見方式

    詳解Java創(chuàng)建線程的五種常見方式

    Java中如何進(jìn)行多線程編程,如何使用多線程?不要擔(dān)心,本文將為你詳細(xì)介紹一下Java實(shí)現(xiàn)線程創(chuàng)建的五種常見方式,感興趣的可以跟隨小編學(xué)習(xí)一下
    2022-01-01

最新評論

扬州市| 西平县| 大庆市| 临朐县| 清镇市| 同德县| 合江县| 平江县| 禄丰县| 安乡县| 灵璧县| 镇江市| 黎平县| 康定县| 衡南县| 小金县| 尉氏县| 穆棱市| 资阳市| 淮安市| 靖宇县| 仁怀市| 蛟河市| 南木林县| 西充县| 会昌县| 焉耆| 成都市| 清原| 吴江市| 蓝山县| 鄢陵县| 都安| 锡林浩特市| 沧州市| 建瓯市| 光山县| 丹巴县| 富民县| 乐昌市| 平邑县|