新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > Android之文件讀寫工具類

        Android之文件讀寫工具類

        作者: 時間:2016-09-12 來源:網絡 收藏

        本工具類永久維護,永久更新,如果各位讀者發現有bug或者不合理之處,歡迎指正,博主將第一時間改正。

        本文引用地址:http://www.104case.com/article/201609/303738.htm

        以下是主要內容,本類主要功能有:

        1.創建文件功能;

        2.向文件中寫入字節數組;

        3.向文件中寫入字符串;

        4.從文件中讀取字節數組;

        5.從文件中讀取字符串;

        import java.io.File;

        import java.io.FileInputStream;

        import java.io.FileOutputStream;

        /**

        * 文件

        *

        * @author bear

        *

        */

        public class FileUtil {

        /**

        * 如果文件不存在,就創建文件

        *

        * @param path 文件路徑

        * @return

        */

        public static String createIfNotExist(String path) {

        File file = new File(path);

        if (!file.exists()) {

        try {

        file.createNewFile();

        } catch (Exception e) {

        System.out.println(e.getMessage());

        }

        }

        return path;

        }

        /**

        * 向文件中寫入數據

        *

        * @param filePath

        * 目標文件全路徑

        * @param data

        * 要寫入的數據

        * @return true表示寫入成功 false表示寫入失敗

        */

        public static boolean writeBytes(String filePath, byte[] data) {

        try {

        FileOutputStream fos = new FileOutputStream(filePath);

        fos.write(data);

        fos.close();

        return true;

        } catch (Exception e) {

        System.out.println(e.getMessage());

        }

        return false;

        }

        /**

        * 從文件中讀取數據

        *

        * @param file

        * @return

        */

        public static byte[] readBytes(String file) {

        try {

        FileInputStream fis = new FileInputStream(file);

        int len = fis.available();

        byte[] buffer = new byte[len];

        fis.read(buffer);

        fis.close();

        return buffer;

        } catch (Exception e) {

        System.out.println(e.getMessage());

        }

        return null;

        }

        /**

        * 向文件中寫入字符串String類型的內容

        *

        * @param file

        * 文件路徑

        * @param content

        * 文件內容

        * @param charset

        * 寫入時候所使用的字符集

        */

        public static void writeString(String file, String content, String charset) {

        try {

        byte[] data = content.getBytes(charset);

        writeBytes(file, data);

        } catch (Exception e) {

        System.out.println(e.getMessage());

        }

        }

        /**

        * 從文件中讀取數據,返回類型是字符串String類型

        *

        * @param file

        * 文件路徑

        * @param charset

        * 讀取文件時使用的字符集,如utf-8、GBK等

        * @return

        */

        public static String readString(String file, String charset) {

        byte[] data = readBytes(file);

        String ret = null;

        try {

        ret = new String(data, charset);

        } catch (Exception e) {

        System.out.println(e.getMessage());

        }

        return ret;

        }

        }



        關鍵詞: Android 讀寫工具

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 隆林| 巴南区| 常熟市| 青神县| 安岳县| 黑河市| 惠来县| 湟源县| 海南省| 哈巴河县| 古丈县| 拉萨市| 高邑县| 长海县| 高碑店市| 辽中县| 西青区| 苗栗县| 平昌县| 老河口市| 建宁县| 文安县| 石屏县| 福清市| 汉沽区| 奉节县| 黑龙江省| 东乡族自治县| 二手房| 黄冈市| 临城县| 冷水江市| 普洱| 苍山县| 丘北县| 永昌县| 视频| 乌兰察布市| 隆昌县| 通渭县| 乳源|