• <li id="00i08"><input id="00i08"></input></li>
  • <sup id="00i08"><tbody id="00i08"></tbody></sup>
    <abbr id="00i08"></abbr>
  • 新聞中心

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

    Android之文件讀寫工具類

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

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

    本文引用地址:http://www.czjhyjcfj.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 讀寫工具

    評論


    相關推薦

    技術專區

    關閉
    主站蜘蛛池模板: 龙州县| 张掖市| 朝阳县| 伊金霍洛旗| 蕉岭县| 南安市| 德昌县| 武汉市| 莫力| 临夏市| 阿瓦提县| 张家川| 岑巩县| 高邮市| 定西市| 古田县| 灵寿县| 常山县| 政和县| 桂平市| 蒲城县| 沧源| 红桥区| 侯马市| 宝山区| 三明市| 寻乌县| 洪洞县| 蛟河市| 广南县| 微山县| 梧州市| 正镶白旗| 通海县| 文水县| 高邮市| 许昌县| 罗江县| 肥城市| 哈尔滨市| 仙居县|