以下是创建InputStream的构造函数-
InputStream in=new DataInputStream(InputStream in);
一旦有了 DataInputStream 对象,便会出现一系列帮助程序方法,可用于读取流或对该流执行其他操作。
Sr.No. | Method & Remark |
---|---|
1 | public final int read(byte [] r,int off,int len) throws IOException 从输入流中读取最多len个字节的数据到一个字节数组中。 |
2 | public final int read(byte[] b) throws IOException 从输入流中读取一些字节,并将其存储到字节数组中。 |
3 | (a) public final Boolean readBooolean()throws IOException (b) public final byte readByte()throws IOException (c) public final short readShort()throws IOException (d) public final Int readInt()throws IOException 这些方法将从包含的InputStream中读取字节。 链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-datainputstream.html 来源:LearnFk无涯教程网 |
4 | public String readLine() throws IOException 从输入流中读取下一行文本,它读取连续的字节,将每个字节分别转换为一个字符,直到遇到行终止符或文件结尾为止。 |
以下是演示DataInputStream和DataOutputStream的示例。 本示例读取文件test.txt中给出的5行,并将这些行转换为大写字母,最后将它们复制到另一个文件test1.txt中。
import java.io.*; public class DataInput_Stream { public static void main(String args[])throws IOException { //将字符串写入编码为修改后的 UTF-8 的文件 DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("E:\\file.txt")); dataOut.writeUTF("learnfk"); //从同一个文件中读取数据 DataInputStream dataIn = new DataInputStream(new FileInputStream("E:\\file.txt")); while(dataIn.available()>0) { String k = dataIn.readUTF(); System.out.print(k+" "); } } }
以下是上述程序的示例运行-
learnfk
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Python实战 · 从0到1搭建直播视频平台 -〔Barry〕