实验七  输入输出流
一、     实验目的和要求
    1掌握使用输入输出流进行文件的读写操作
二、     实验内容
1、按程序模板要求,将注释处替换为Java程序代码。实现文件加密。
2、按程序模板要求,将注释处替换为Java程序代码。给文件的内容添加行号.
    3、串行化对象Student到本地文件,并在下一次运行程序时用来初始化。(选做)
三、     实验环境
硬件:
1.       PC
软件:
1.       Windows XP Professional 中文版
2.       JDK 1.7
3.       JCreator 4.5 英文版
 
一、     附录:
内容一代码:
import java.io.*;
public class SecretExample{
         public static void main(String arg[]){
                   File fileOne=new File("hello.txt"),
                            fileTwo=new File("hello.secret");
                   char b[]=new char[100];
                   try{
                            FileReader in=new FileReader(fileOne); // 创建指向fileOne的字符输入流
                            FileWriter out=new FileWriter(fileTwo); // 创建指向fileTwo字符输出流
                            int n=-1;
                            while((n=in.read(b))!=-1){
                                     for(int i=0;i<n;i++){
                                               b[i]=(char)(b[i]^'a');
                                     }
                                     out.write(b,0,n);    //  out将数组b的前n单元写到文件
                            }
                            out.close();       // out关闭
                            in=new FileReader(fileTwo);             // 创建指向fileTwo的字符输入流
                            System.out.println("加密后的文件内容:");
                            while((n=in.read(b))!=-1){
                                     String str=new String(b,0,n);
                                     System.out.println(str);
                            }
                            in=new FileReader(fileTwo);
                            System.out.println("解密后的文件内容:");
                            while((n=in.read(b))!=-1){
                                     for(int i=0;i<n;i++){
                                               b[i]=(char)(b[i]^'a');
                                     }
                                     System.out.printf(new String(b,0,n));
                            }
                            in.close();          // in关闭
                   }
                   catch(IOException e){
                            System.out.println(e);
                   }
         }
}
 
内容二代码:
import java.io.*;
public class ReadExample{ 
   public static void main(String args[ ]){
      File file=new File("c:/1000","hello.txt");
      File tempFile=new File("temp.text");
      try{
           FileReader  inOne=new FileReader(file);         // 创建指向文件file的输入流
           BufferedReader inTwo=new BufferedReader(inOne);       //创建指向inOne file的输入流
           FileWriter  tofile=new FileWriter(tempFile);  //创建指向文件tempFile的输出流
           BufferedWriter out=new BufferedWriter(tofile);      // 创建指向tofile的输出流
           String s=null;
           int i=0;
           s=inTwo.readLine();                        // inTwo读取一行
           while(s!=null)
           {
               i++;
               out.write(i+" "+s);
               out.newLine();
               s=inTwo.readLine();                    // inTwo读取一行
           }
           inOne.close();
           inTwo.close();
           out.flush();
           out.close();
           tofile.close();
           inOne=new FileReader(tempFile);    // 创建指向文件tempFile的输入流
           inTwo=new BufferedReader(inOne);     // 创建指向inOne file的输入流
           tofile=new FileWriter(file);       // 创建指向文件file的输出流
           out=new BufferedWriter(tofile);          // 创建指向tofile的输出流
           while((s=inTwo.readLine())!=null)   // inTwo读取一行
           {
              out.write(s);
              out.newLine();
           }
           inOne.close();
           inTwo.close();
           out.flush();
           out.close();
           tofile.close();
           inOne=new FileReader(file);      // 创建指向文件file的输入流
           inTwo=new BufferedReader(inOne);    // 创建指向inOne file的输入流
            while((s=inTwo.readLine())!=null) // inTwo读取一行
           {
             System.out.println(s);
           }
           inOne.close();
           inTwo.close();
           tempFile.delete();
      }
      catch(IOException e)
      {
           System.out.println(e);
      } 
   }
}