- package IOday02;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class FileStream {
- /**
- * @param args
- */
- public static void main(String[] args) throws IOException {
- // writeFile();
- method_1();
- // method_2();
- //method_3();
- }
- public static void method_3() throws IOException {
- FileInputStream fs = new FileInputStream("filestream.txt");
- int num = fs.available();
- // System.out.println(num);
- byte[] a = new byte[num];
- int s = 0;
- while ((s = fs.read(a)) != -1) {
- System.out.println(new String(a, 0, s));
- }
- }
- public static void method_2() throws IOException {
- FileInputStream fs = new FileInputStream("filestream.txt");
- byte[] b = new byte[1024];
- int s = 0;
- while ((s = fs.read(b)) != -1) {
- System.out.println(new String(b, 0, s));
- }
- }
- private static void writeFile() throws IOException {
- FileOutputStream fs = new FileOutputStream("filestream.txt");
- fs.write("abcdefg".getBytes());
- fs.close();
- }
- public static void method_1() throws IOException {
- FileInputStream fr = new FileInputStream("filestream.txt");
- int num = 0;
- while ((num = fr.read()) != -1) {
- System.out.println("num=" + (char) num);
- fr.close();
- }
- }
- }
复制代码 method_1就是读到一个取出一个为什么取出a后就报错了?
|
|