java:题目二在你计算机的一个盘比如D盘根目录下创建一个文件名字叫做file01txt。保存以下内容要求是单字节字符比如字母或者数字file01txt1 abcdef123456要求:1 使用字节输入流一次读取一个字节的方法将file01txt的文件读取并打印2 使用字节输入流一次读取多个字节的方法将file01txt的文件读取并打印
- 使用字节输入流一次读取一个字节的方法将file01.txt的文件读取并打印
import java.io.FileInputStream;
import java.io.IOException;
public class ByteStreamDemo1 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("D:/file01.txt");
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出:
1 abcdef123456
- 使用字节输入流一次读取多个字节的方法将file01.txt的文件读取并打印
import java.io.FileInputStream;
import java.io.IOException;
public class ByteStreamDemo2 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("D:/file01.txt");
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, length));
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出:
1 abcdef123456
``
原文地址: https://www.cveoy.top/t/topic/frIh 著作权归作者所有。请勿转载和采集!