欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第八章

程序员文章站 2022-07-12 17:59:00
...

1>选择题
(1)a(2)a(3)b(4)c(5)d
2>

package jbit.io.homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReaderAndWriterFile {
	public static void main(String[] args) {
		 BufferedReader reader=null;
		 BufferedWriter writer=null;
		 try {
			//创建 FileReader对象和FileWriter对象.
			FileReader fr  = new FileReader("c:\\source.txt");  
			FileWriter fw = new FileWriter("d:\\target.txt");
           //创建 输入、输入出流对象.
           reader = new BufferedReader(fr);
           writer = new BufferedWriter(fw);
           String line = null;
           StringBuffer sbf=new StringBuffer();  
           //循环读取并追加字符
           while ((line = reader.readLine()) != null) {
               sbf.append(line);  
           }
           writer.write(sbf.toString());  //写入文件  
       } catch (FileNotFoundException e) {
    	   System.out.println("无此文件");
       }catch (IOException e) {
           e.printStackTrace();
       }finally{
    	   //关闭 reader 和 writer.
           try {
        	   if(reader!=null)
        		   reader.close();
        	   if(writer!=null)
        		   writer.close();  
           }catch (IOException e) {
				e.printStackTrace();
		   } 		
       }
	}
}

package jbit.io.homework;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;

public class ReadIinFile {
	public static void main(String[] args) {
	  FileInputStream fis = null;
      DataInputStream dis = null;
	  try {
		  //创建输出流对象
	      fis = new FileInputStream("C:\\Windows\\win.ini");
	      dis = new DataInputStream(fis);
	      //创建输入流对象
	      int temp;
	      //读取文件并写入文件
	      while ( (temp = dis.read()) != -1) {
	        System.out.print((char)temp);
	        
	      }
	    }catch (FileNotFoundException e) {
	    	System.out.println("无此文件");
	    }catch (IOException ioe) {
	      ioe.printStackTrace();
	    }finally{
	    	try {
	    		if(fis!=null)
	    			fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }
	}
}

上一篇: 第八章

下一篇: 变易算法 - STL算法