java学习笔记之文件输出输入流

节点流类型:

举个栗子:

 1 package io;
 2 
 3 import java.io.*;
 4 
 5 public class TestFileInputStream {
 6     public static void main(String[] args) {
 7         int b = 0;
 8         FileInputStream in = null;
 9         try {
10             in = new FileInputStream("d:/1.txt");
11         } catch (FileNotFoundException e) {
12             System.out.println("找不到指定文件");
13             System.exit(-1);
14         }
15 
16         try {
17             long num = 0;
18             while ((b = in.read()) != -1) {
19                 System.out.print((char) b);
20                 num++;
21             }
22             in.close();
23             System.out.println();
24             System.out.println("共读取了 " + num + " 个字节");
25         } catch (IOException e1) {
26             System.out.println("文件读取错误");
27             System.exit(-1);
28         }
29     }
30 }

 搞不明白的是为什么非要用一个int型的b去接收,不接收又不行,为啥

 1 package io;
 2 
 3 import java.io.*;
 4 public class TestFileOutputStream {
 5   public static void main(String[] args) {
 6       int b = 0;
 7       FileInputStream in = null;
 8       FileOutputStream out = null;
 9       try {
10         in = new FileInputStream("d:/share/java/HelloWorld.java");
11         out = new FileOutputStream("d:/share/java/io/HW.java");
12         while((b=in.read())!=-1){
13           out.write(b);
14         }
15         in.close(); 
16         out.close();
17       } catch (FileNotFoundException e2) {
18         System.out.println("找不到指定文件"); System.exit(-1);
19       } catch (IOException e1) {
20         System.out.println("文件复制错误"); System.exit(-1);
21       }
22       System.out.println("文件已复制");
23   }
24 }

 

 

posted @ 2013-09-11 21:22  董文博  阅读(153)  评论(0编辑  收藏  举报