欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用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)文章

  • java中main函數(shù)你知道多少

    java中main函數(shù)你知道多少

    大家好,本篇文章主要講的是java中main函數(shù)你知道多少,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • 最有價值的50道java面試題 適用于準(zhǔn)入職Java程序員

    最有價值的50道java面試題 適用于準(zhǔn)入職Java程序員

    這篇文章主要為大家分享了最有價值的50道java面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Spring事務(wù)失效問題分析及解決方案

    Spring事務(wù)失效問題分析及解決方案

    這篇文章主要介紹了Spring事務(wù)失效問題分析及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • java集合map取key使用示例 java遍歷map

    java集合map取key使用示例 java遍歷map

    這篇文章主要介紹了java集合map取key使用示例,需要的朋友可以參考下
    2014-02-02
  • Java基礎(chǔ)之引用相關(guān)知識總結(jié)

    Java基礎(chǔ)之引用相關(guān)知識總結(jié)

    今天聊聊Java的引用,大多數(shù)時候我們說引用都是強(qiáng)引用,只有在對象不使用的情況下才會釋放內(nèi)存,其實(shí)Java 內(nèi)存有四種不同的引用.一起看看吧,,需要的朋友可以參考下
    2021-05-05
  • java類比C++的STL庫詳解

    java類比C++的STL庫詳解

    這篇文章主要介紹了java類比C++的STL庫詳解,標(biāo)準(zhǔn)模板庫,是C++標(biāo)準(zhǔn)庫的重要組成部分,中文可譯為標(biāo)準(zhǔn)模板庫或者泛型庫,其包含有大量的模板類和模板函數(shù),STL 是一些容器、算法和其他一些組件的集合,需要的朋友可以參考下
    2023-08-08
  • 淺談在JAVA項(xiàng)目中LOG4J的使用

    淺談在JAVA項(xiàng)目中LOG4J的使用

    下面小編就為大家?guī)硪黄獪\談在JAVA項(xiàng)目中LOG4J的使用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • IntelliJ IDEA2020.1 Mac maven sdk 全局配置

    IntelliJ IDEA2020.1 Mac maven sdk 全局配置

    這篇文章主要介紹了IntelliJ IDEA2020.1 Mac maven sdk 全局配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • SpringBoot集成極光推送完整實(shí)現(xiàn)代碼

    SpringBoot集成極光推送完整實(shí)現(xiàn)代碼

    本文主要介紹了SpringBoot集成極光推送完整實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • java String.split 無法使用小數(shù)點(diǎn)分割的問題

    java String.split 無法使用小數(shù)點(diǎn)分割的問題

    這篇文章主要介紹了java String.split 無法使用小數(shù)點(diǎn)分割的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評論