Java I/O第一篇 之 (字符流&字节流)

1.关于InputStream 和 OutputStream相关问题的思考

 

  1 import java.io.*;
  2 
  3 public class program1 {
  4 
  5     public static void main(String[] args) throws Exception{
  6         // TODO Auto-generated method stub
  7     /*    program1 IO1=new program1();
  8         IO1.ArrayInput("E:\\IOtest.txt");*/
  9         /* InputStream调用处
 10         ArrayInput("E:\\IOtest.txt");
 11         ArrayOutput("E:\\IOtest.txt");
 12         strWrite("E:\\IOtest.txt");
 13         copy("E:\\IOtest.txt","E:\\IOtest2.txt");    
 14         charToString("E:\\IOtest.txt");    
 15         stringToChar("E:\\IOtest.txt");*/
 16         ArrayInput("E:\\IOtest.txt");
 17     }
 18     /**
 19      * InputStream(读进来)
 20      * @throws IOException 
 21      * */
 22     public static void ArrayInput(String fileName) throws IOException{
 23         File f=new File(fileName);       //通过File类找到文件
 24         InputStream fn=null;
 25         try {
 26             fn=new FileInputStream(f);
 27         } catch (FileNotFoundException e) {
 28             // TODO Auto-generated catch block
 29             e.printStackTrace();
 30         }
 31         int len=0;       //文件长度
 32         int temp;        //临时存储数据(temprory)
 33         byte b[]=new byte[1024]; 
 34         //准备读取文件内容
 35         /*while((temp=fn.read())!=-1){
 36             b[len]=(byte)temp;
 37             len++;
 38         }*/
 39         len=fn.read(b);  //读取文件内容
 40         fn.close();      //关闭文件流
 41         //打印 文件内容
 42         System.out.println(new String(b,0,len));
 43         //String(a.b,c)转换字符串函数,将吧b,c位置之间的字节转换为String
 44     }
 45     /**
 46      * OutputStream(写进去)
 47      * @throws Exception 
 48      * */
 49     public static void ArrayOutput(String fileName) throws Exception{
 50         /*
 51         File f=new File(fileName);
 52         OutputStream out=null;                 
 53         out=new FileOutputStream(f);
 54         */
 55         FileOutputStream out=null;                
 56         out=new FileOutputStream(fileName);       //先创建类,再动态赋予内存空间
 57         String add="this my write!";
 58         byte[] b=add.getBytes();    //String转换为byte Array
 59         out.write(b);               //写入字节数组
 60         out.close();                //关闭文件流
 61         System.out.println("文件写入成功。");
 62     }
 63     
 64     /**
 65      * Write(字符串写进去)
 66      * */
 67     public static void strWrite(String fileName) throws Exception{
 68         File f=new File(fileName);
 69         Writer out=null;
 70         out=new FileWriter(f);
 71         
 72         String str="这是我想写入文件的内容。";
 73         out.write(str);
 74         out.close();
 75         System.out.println("写入文件成功");
 76     }
 77     /**
 78      * Reader(String 读出来)  文件内容Copy
 79      * */
 80     public static void copy(String fileName,String traget) throws IOException{
 81         //判断文件名是否相同 
 82         if(fileName==traget){
 83             System.out.println("文件相同,退出程序!");
 84             System.exit(-1);         //Java虚拟机退出程序,int参数非0为非正常退出
 85         }
 86         File in1=new File(fileName);
 87         File out1=new File(traget);
 88         //创建输出/输入对象流
 89         FileReader in=null;
 90         FileWriter out=null;
 91         //判断要复制的文件是否存在
 92         if(!in1.exists()){
 93             System.out.println(fileName+"文件不存在!程序关闭!");
 94             System.exit(-1);
 95         }
 96         try {
 97             in=new FileReader(in1);
 98         } catch (FileNotFoundException e) {
 99             // TODO Auto-generated catch block
100             e.printStackTrace();
101         }
102         try {
103             out=new FileWriter(out1);          //若traget文件不存在,FileWrite会自动创建该文件
104         } catch (IOException e) {
105             // TODO Auto-generated catch block
106             e.printStackTrace();
107         }
108         try{
109             int temp ; 
110             while((temp=in.read())!=-1){       //
111                 out.write(temp);               //
112             }
113             in.close();                        //关闭输入输出流
114             out.close();
115             System.out.println("Copy完成。");
116         }catch(Exception e1){
117             System.out.println("文件拷贝失败!");
118         }
119     }

 

2.     InputStreamWrite  OutputStreamReader 之 流类型的转换。

        我们在程序中会遇到字符流与字节流之间的相互装换问题,比如,我需要将程序中的字节流以字符流形式保存在硬盘中, 或者是要把硬盘文件转化为字节形式提供给程序使用。

 

 1  /**
 2      * 字符流与字节流之间的相互转化 InputStreamWrite OutputStreamReader
 3      * */
 4     //InputStreamWrite  字节流转换字符流
 5     public static void charToString(String fileName)throws Exception{   //抛出所有异常
 6         File f=new File(fileName);
 7         //创建字符流
 8         Writer out=null;
 9         out=new OutputStreamWriter(new FileOutputStream(f));      
10         //将字节流 FileOutputStream  ->  OutputStreamWrite
11         out.write("这是一个将字节流 FileOutputStream  ->  OutputStreamWrite");
12         out.close();
13         System.out.println("文件写入成功!");
14     }
15     //OutputStreamReader  字节流转换字符流
16     public static void stringToChar(String fileName) throws IOException{
17         File f=new File(fileName);
18         Reader in=null;
19         try {
20             in=new InputStreamReader(new FileInputStream(f));
21         } catch (FileNotFoundException e) {
22             // TODO Auto-generated catch block
23             e.printStackTrace();
24         }
25         //读取打印文件内容
26         char a[]=new char[1024];
27         int len=in.read(a);              //读取in内容到a中
28         in.close();
29         System.out.println(a);
30     }   

 (遗留问题:如何将字符流转换为字节流?)

 

 

3. DataInpuStream  DataOutputStream  数据操作流。能够方便的存取数据,不用考虑数据类型。

 

 1 /**
 2      * DataInputStream  DataOutputStream
 3      * 数据操作流
 4      * @throws IOException 
 5      * */
 6     //写入数据
 7     public static void DataOutputStreamDemo(String fileName) throws IOException{
 8         DataOutputStream dataIn=null;
 9         File f=new File(fileName);
10         dataIn=new DataOutputStream(new FileOutputStream(f));
11         String name[]={"衣服一","衣服二","衣服三"};
12         int price[]={12,56,56};
13         char marker[]={'n','s','t'};
14         for(int i=0;i<name.length;i++){
15             dataIn.writeChars(name[i]);
16             dataIn.writeInt(price[i]);
17             dataIn.writeChar(marker[i]);
18         }
19         System.out.println("数据写入完成");
20         dataIn.close();
21     }
22     //读取数据
23     public static void DataInputStreamDemo(String fileName) throws IOException{
24         DataInputStream dataIn=null;
25         File f=new File(fileName);
26         dataIn=new DataInputStream(new FileInputStream(f));
27         char[] a=new char[1024];
28         int len=0;
29         char c=0;
30         while((c=dataIn.readChar())!='t'){
31             a[len]=c;
32             len++;
33         }
34         String str=new String(a,0,len);
35         System.out.println(str);
36         dataIn.close();
37     }

 

4:PrintStream  PrintWrite  字符/字节  打印流(占位符类型于C语法)

 

 1 /**
 2      * PrintStream  PrintWrite 打印流 往文件里面写东西
 3      * @throws FileNotFoundException 
 4      * */
 5     public static void printIO(String fileName) throws FileNotFoundException{
 6         PrintStream ps=null;
 7         File f=new File(fileName);
 8         ps=new PrintStream(new FileOutputStream(f));
 9         String name="陈扬";
10         int age=22;
11         ps.printf("名字:%s ;年龄:%d",name,age );
12         System.out.println("写入成功");
13         ps.close();
14     }

 

5:BufferedReader  输入缓冲流。用于处理键盘输入数据存储,通过存储在自身内存空间上。

 

 1 /**
 2      * BufferedReader  输入缓冲流
 3      * @throws IOException 
 4      * */
 5     public static void BufferedReaderDemo() throws IOException{
 6         BufferedReader br=null;
 7         br=new BufferedReader(new InputStreamReader(System.in));
 8         System.out.println("请输入内容");
 9         String str=    br.readLine();
10         System.out.println("你输入的内容为:"+str);
11         br.close();
12         
13     }

(遗留问题:缓冲流读取和写出数据,在写出数据时失败!)。

posted @ 2015-03-08 16:15  木头同学  阅读(141)  评论(0编辑  收藏  举报