JAVA IO-文件流

来源:互联网 发布:餐馆软件 编辑:程序博客网 时间:2024/05/18 16:37

JAVA IO

输入和输出

输入和输出是相对于某个介质而言的,这里把数据的来源暂定为数据源,数据的流向地称为目标媒介。常见的数据源和目标媒介有:
1. 文件
2. 管道
3. 网络连接
4. 内存缓存

数据的输入和输出就可以这样理解,从数据源读取(输入)数据,向目标媒介输出数据;通常来说,一个媒介的输入往往是另一个媒介的输出。

流是一段连续的数据流,可以从流中输入数据,也可以从流中输出数据;流也可以理解成数据源和目标媒介的一个通道,流一般可以分为:
1. 字节流(以字节为单位进行读写)
2. 字符流(以字符为单位进行读写)

常见的字节流一般是由inputStream/outputStream进行扩展,字符流一般是由Reader/Writer进行扩展。字节流和字符流的转换,则可以通过inputStreamReader/outputStreamWriter进行转换。

由流的用途可以划分为:
1. 文件访问
2. 网络访问
3. 内存缓存访问
4. 线程内部通信(管道)
5. 缓冲
6. 过滤
7. 解析
8. 读取文本
9. 读写基本类型数据
10. 读写对象

文件流

文件是常见的数据源或者存储数据的媒介,根据数据流的流向来选取合适的文件流来对数据进行操作。

读文件

当从一个文件中读取数据时,我们可以根据我们要读取文件的数据是二进制数据或者字符型数据来选取FileInputStream或者FileReader

    /**     * FileInputStream read     *     * @param file     * @return     */    public static String readFileByByte(File file) {        StringBuilder sb = new StringBuilder();        try {            FileInputStream fileInputStream = new FileInputStream(file);            byte[] buffer = new byte[1024];            int len = -1;            while ((len = fileInputStream.read(buffer)) != -1) {                sb.append(new String(buffer, 0, len));            }            fileInputStream.close();        } catch (Exception e) {            e.printStackTrace();        }        return sb.toString();    }    /**     * FileReader read     *     * @param file     * @return     */    public static String readFileByChar(File file) {        StringBuilder sb = new StringBuilder();        try {            FileReader fileReader = new FileReader(file);            int c = -1;            while ((c = fileReader.read()) != -1) {                sb.append((char) c);            }            fileReader.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return sb.toString();    }

写文件

当从一个文件中写入数据时,我们可以根据我们要写入的数据时二进制数据或者字符型数据来选取FileOutputStream或者FileWriter

    /**     * FileOutputStream write     *     * @param file     * @param content     * @return     */    public static boolean writeFileByByte(File file, String content) {        if (file == null || !file.exists() || isEmpty(content))            return false;        try {            //Whether or not covered            FileOutputStream fileOutputStream = new FileOutputStream(file, false);            fileOutputStream.write(string2ByteArray(content));            fileOutputStream.close();            return true;        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return false;    }    public static boolean isEmpty(String content) {        if (content == null || content.length() == 0)            return true;        return false;    }    public static byte[] string2ByteArray(String content) {        byte[] buf = new byte[content.length()];        char[] chars = content.toCharArray();        for (int i = 0; i < chars.length; i++) {            buf[i] = (byte) chars[i];        }        return buf;    }    /**     * FileWriter write     * @param file     * @param content     * @return     */    public static boolean writeFileByChar(File file, String content) {        if (file == null || !file.exists() || isEmpty(content))            return false;        try {            FileWriter fileWriter = new FileWriter(file, false);            fileWriter.write(content.toCharArray());            //Flushes the stream and push data            fileWriter.flush();            fileWriter.close();            return true;        } catch (IOException e) {            e.printStackTrace();        }        return false;    }

随机文件读写

当需要从文件中或者向文件中随机的读取或者写入文件时,这时可以考虑使用RandAccessFile来对文件进行读取和写入

/**     * RandomAccessFile read     * @param file     * @return     */    public static String readFileByRandomAccess(File file) {        if (file == null || !file.exists())            return null;        StringBuilder sb = new StringBuilder();        try {            RandomAccessFile readAccessFile = new RandomAccessFile(file, "r");            int len = -1;            byte[] buf = new byte[1024];            while ((len = readAccessFile.read(buf)) != -1) {                sb.append(new String(buf, 0 ,len));            }            readAccessFile.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return sb.toString();    }    /**     * RandomAccessFile write into the file tail     * @param file     * @param content     * @return     */    public static boolean writeFileByRandomAccess(File file, String content) {        if (file == null || file.length() == 0 || isEmpty(content))            return false;        try {            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");            //insert into the file tail            randomAccessFile.seek(file.length());            randomAccessFile.writeUTF(content);            randomAccessFile.close();            return true;        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return false;    }
0 0
原创粉丝点击