一、FileInputStreamReader
FileInputStreamReader (一般使用子类 FileReader) 是 Java 中的一个用于读取文件的类。该类是 InputStreamReader 的子类,它按照指定编码方式从文件中读取字符。
FileReader相对于 InputStreamReader来说查找文件不需要指定编码方式,它会按照文件的默认编码方式读取
FileInputStreamReader常用构造函数:
public InputStreamReader(InputStream in); public InputStreamReader(InputStream in, String charsetName);
第一个构造函数是通过给定的 InputStream 对象和默认字符集来创建对象。第二个构造函数是构造一个根据指定字符集来读取流的 InputStreamReader。
二、FileInputStreamRead读取文件
FileInputStreamRead 是用于从文件中读取字节流的类,用于读取字节流时,一般会结合 BufferedReader 类和 InputStreamReader 类来使用。
读取的核心方法为 read(byte[] b) 和 read(byte[] b, int off, int len)。
第一个 read 方法从输入流中读取一些字节,并将它们存储到缓冲区数组 b 中。第二个 read 方法是从流中读取 len 个字节,并将其存储在偏移量 off 的缓冲区数组 b 中。
读取文件的代码示例:
public static void main(String[] args){ try { FileInputStream fis = new FileInputStream("test.txt"); // 创建字节输入流 byte[] data = new byte[1024]; // 创建一个 bytes 数组,用来存储读取的数据 int len = fis.read(data); // 读取数据 String result = new String(data, 0, len, "UTF-8"); // 解码读取到的数据(默认以 UTF-8 解码) System.out.println(result); // 打印结果 } catch (Exception e) { e.printStackTrace(); } }
三、FileInputStreamRead用法
FileInputStreamRead 有很多用法,下面介绍几个常用的。
1. 读取文件到字节数组中
将文件读取到字节数组中,可以自定义缓冲区大小,从而提高读取效率。
public static byte[] readFileToByteArray(File file) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int readLength; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((readLength = fis.read(buffer)) != -1) { bos.write(buffer, 0, readLength); } return bos.toByteArray(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { // Ignore exception. } } } }
2. 读取文件到字符缓冲区中
在文件较大且需要一次读取全部数据的情况下,可以使用 BufferedReader 来提高读取效率,同时可以通过指定字符集来解决读取中文时乱码的问题。
public static void readFileToStringBuffer(File file) throws IOException { FileInputStream fis = null; BufferedReader reader = null; try { fis = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(fis, "UTF-8")); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } System.out.println(sb.toString()); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // Ignore exception. } } if (fis != null) { try { fis.close(); } catch (IOException e) { // Ignore exception. } } } }
3. 读取压缩文件
利用 Java 自带的 GZIPInputStream 类,可以读取压缩后的文件。
public static void readCompressedFile(File file) throws IOException { GZIPInputStream gis = null; try { gis = new GZIPInputStream(new FileInputStream(file)); BufferedReader reader = new BufferedReader(new InputStreamReader(gis, "UTF-8")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } finally { if (gis != null) { try { gis.close(); } catch (IOException e) { // Ignore exception. } } } }
4. 读取网络文件
通过 URL 类从网络上读取文件,通常用于获取需要下载的文件的链接,然后使用 HttpURLConnection 类下载文件。
public static void readRemoteFile(String url) throws IOException { InputStream in = new URL(url).openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } in.close(); }
四、总结
FileInputStreamRead 是 Java 中用于读取文件的一个重要类,其使用范围广泛。其中,FileInputStreamReader 用于按指定编码方式从文件中读取字符,而 FileInputStreamRead 用于读取字节流。
本文介绍了 FileInputStreamRead 的一些常用用法,包括将文件读取到字节数组中、将文件读取到字符缓冲区中、读取压缩文件以及从网络上读取文件等用法。对于需要从文件读取数据的 Java 开发人员来说,掌握 FileInputStreamRead 是非常重要的。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/242269.html