Java输入流
抽象基本组件是InputStream类。我们有FileInputStream,ByteArrayInputStream和PipedInputStream,FilterInputStream的具体类。超类InputStream包含从输入流读取数据的基本方法,所有具体类都支持这些方法。
对输入流的基本操作是从其读取数据。 InputStream类中定义的一些重要方法在下面列出:
- read()
读取一个字节并将读取的字节作为int返回。当到达输入流的结尾时,它返回-1。
- read(byte[] buffer)
读取最大值直到指定缓冲区的长度。它返回在缓冲区中读取的字节数。如果到达输入流的结尾,则返回-1。
- read(byte [] buffer,int offset,int length)
读取最大值到指定长度字节。数据从偏移索引开始写入缓冲区。 它返回读取的字节数或-1,如果到达输入流的结束。
- close ()
关闭输入流
- available()
返回可以从此输入流读取但不阻塞的估计字节数。
Java文件输入流
在Java I/O中,流意味着数据流。流中的数据可以是字节,字符,对象等。要从文件读取,我们需要创建一个FileInputStream类的对象,它将表示输入流。如果文件不存在,FileInputStream类的构造函数将抛出FileNotFoundException异常。要处理这个异常,我们需要将你的代码放在try-catch块中。
try { FileInputStream fin = new FileInputStream(srcFile); }catch (FileNotFoundException e){ // The error handling code goes here }
读取数据
FileInputStream类有一个重载的read()方法从文件中读取数据。我们可以一次读取一个字节或多个字节。read()方法的返回类型是int,虽然它返回一个字节值。如果到达文件的结尾,则返回-1。我们需要将返回的int值转换为一个字节,以便从文件中读取字节。通常,我们在循环中一次读取一个字节。最后,我们需要使用close()方法关闭输入流。close()方法可能抛出一个 io Exception,因此,我们需要在try-catch块中包含这个调用。通常,我们在try块中构造一个输入流,并在finally块中关闭它,以确保它在我们完成后总是关闭。所有输入/输出流都可自动关闭。我们可以使用try-with-resources来创建它们的实例,所以无论是否抛出异常,它们都会自动关闭,避免需要 显式 地调用它们的close()方法。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String dataSourceFile = "asdf.txt";
try (FileInputStream fin = new FileInputStream(dataSourceFile)) {
byte byteData;
while ((byteData = (byte) fin.read()) != -1) {
System.out.print(( char ) byteData);
}
} catch (FileNotFoundException e) {
;
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java缓冲输入流
BufferedInputStream通过缓冲数据向输入流添加功能。它维护一个内部缓冲区以存储从底层输入流读取的字节。我们创建缓冲区输入流如下:
String srcFile =“test.txt";BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
以下代码显示如何使用BufferedInputStream从文件读取。
import java.io.BufferedInputStream; import java.io.FileInputStream; public class Main { public static void main(String[] args) { String srcFile = "test.txt"; try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream( srcFile))) { // Read one byte at a time and display it byte byteData; while ((byteData = (byte) bis.read()) != -1) { System.out.print((char) byteData); } } catch (Exception e2) { e2.printStackTrace(); } } }
Java推回输入流
PushbackInputStream向输入流添加功能,允许我们使用其unread()方法推回读取的字节。有三个版本的unread()方法。一个让我们推回一个字节,另外两个让我们推回多个字节。
import java.io.FileInputStream;
import java.io.PushbackInputStream;
public class Main {
public static void main(String[] args) {
String srcFile = "test.txt";
try (PushbackInputStream pis = new PushbackInputStream(new FileInputStream(
srcFile))) {
byte byteData;
while ((byteData = (byte) pis.read()) != -1) {
System.out.print((char) byteData);
pis.unread(byteData);
// Reread the byte we unread
byteData = (byte) pis.read();
System.out.print((char) byteData);
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
Java数据输入流
DataInputStream可以从输入流中读取Java基本数据类型值。DataInputStream类包含读取数据类型值的读取方法。例如,要读取int值,它包含一个readInt()方法;读取char值,它有一个readChar()方法等。它还支持使用readUTF()方法读取字符串。
import java.io.DataInputStream; import java.io.FileInputStream; public class Main { public static void main(String[] args) { String srcFile = "primitives.dat"; try (DataInputStream dis = new DataInputStream(new FileInputStream(srcFile))) { // Read the data in the same order they were written int intValue = dis.readInt(); double doubleValue = dis.readDouble(); boolean booleanValue = dis.readBoolean(); String msg = dis.readUTF(); System.out.println(intValue); System.out.println(doubleValue); System.out.println(booleanValue); System.out.println(msg); } catch (Exception e) { e.printStackTrace(); } } }