问题:定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印
出来(byte数组的大小限制为5)。 我是这样写的public static void PrintFile() throws Exception
{
//定义一个文件输入流
FileInputStream input = new FileInputStream(new File("exercise.txt"));
//定义一个临时的文件输出流。
FileOutputStream output  = new FileOutputStream(new File("t5.txt"));
//调用read(byte[] b)方法
byte[] b = new byte[5];
int con = 0;
while((con = input.read(b))!=-1)
{
output.write(b, 0, con);
}

//因为要求打印出来,我用的是读取t5.txt文件实现的。感觉很别扭,不知道还有没有其它方法。 //打印exercise.txt文件。t5.txt与exercise.txt文件一样
FileReader  fr = new FileReader("t5.txt");
int re = 0;
  while((re= fr.read())!=-1)
  {
  System.out.print((char)re);
  }
 
  input.close();
  output.close();
  fr.close();
}
}请问一下。既然是读取.txt文件,我用FileReader就可以,为什么题目,要求定义一个文件输入流。

解决方案 »

  1.   

    贴个范例(注释很清楚),可能LZ跟我以前一样,对读、写,输入、输出有点混淆:import java.io.* ;
    public class Copy{
    public static void main(String args[]){
    if(args.length!=2){ // 判断是否是两个参数
    System.out.println("输入的参数不正确。") ;
    System.out.println("例:java Copy 源文件路径 目标文件路径") ;
    System.exit(1) ; // 系统退出
    }
    File f1 = new File(args[0]) ; // 源文件的File对象
    File f2 = new File(args[1]) ; // 目标文件的File对象
    if(!f1.exists()){
    System.out.println("源文件不存在!") ;
    System.exit(1) ;
    }
    InputStream input = null ; // 准备好输入流对象,读取源文件
    OutputStream out = null ; // 准备好输出流对象,写入目标文件
    try{
    input = new FileInputStream(f1) ;
    }catch(FileNotFoundException e){
    e.printStackTrace() ;
    }
    try{
    out = new FileOutputStream(f2) ;
    }catch(FileNotFoundException e){
    e.printStackTrace() ;
    }
    if(input!=null && out!=null){ // 判断输入或输出是否准备好
    int temp = 0 ;
    try{
    while((temp=input.read())!=-1){ // 开始拷贝
    out.write(temp) ; // 边读边写
    }
    System.out.println("拷贝完成!") ;
    }catch(IOException e){
    e.printStackTrace() ;
    System.out.println("拷贝失败!") ;
    }
    try{
    input.close() ; // 关闭
    out.close() ; // 关闭
    }catch(IOException e){
    e.printStackTrace() ;
    }
    }
    }
    }
      

  2.   

    贴个范例(注释很清楚),可能LZ跟我以前一样,对读、写,输入、输出有点混淆:import java.io.* ;
    public class Copy{
    public static void main(String args[]){
    if(args.length!=2){ // 判断是否是两个参数
    System.out.println("输入的参数不正确。") ;
    System.out.println("例:java Copy 源文件路径 目标文件路径") ;
    System.exit(1) ; // 系统退出
    }
    File f1 = new File(args[0]) ; // 源文件的File对象
    File f2 = new File(args[1]) ; // 目标文件的File对象
    if(!f1.exists()){
    System.out.println("源文件不存在!") ;
    System.exit(1) ;
    }
    InputStream input = null ; // 准备好输入流对象,读取源文件
    OutputStream out = null ; // 准备好输出流对象,写入目标文件
    try{
    input = new FileInputStream(f1) ;
    }catch(FileNotFoundException e){
    e.printStackTrace() ;
    }
    try{
    out = new FileOutputStream(f2) ;
    }catch(FileNotFoundException e){
    e.printStackTrace() ;
    }
    if(input!=null && out!=null){ // 判断输入或输出是否准备好
    int temp = 0 ;
    try{
    while((temp=input.read())!=-1){ // 开始拷贝
    out.write(temp) ; // 边读边写
    }
    System.out.println("拷贝完成!") ;
    }catch(IOException e){
    e.printStackTrace() ;
    System.out.println("拷贝失败!") ;
    }
    try{
    input.close() ; // 关闭
    out.close() ; // 关闭
    }catch(IOException e){
    e.printStackTrace() ;
    }
    }
    }
    }
      

  3.   

    import java.io.* ;
    public class Copy{
        public static void main(String args[]){
            if(args.length!=2){        // 判断是否是两个参数
                System.out.println("输入的参数不正确。") ;
                System.out.println("例:java Copy 源文件路径 目标文件路径") ;
                System.exit(1) ;    // 系统退出
            }
            File f1 = new File(args[0]) ;    // 源文件的File对象
            File f2 = new File(args[1]) ;    // 目标文件的File对象
            if(!f1.exists()){
                System.out.println("源文件不存在!") ;
                System.exit(1) ;
            }
            InputStream input = null ;        // 准备好输入流对象,读取源文件
            OutputStream out = null ;        // 准备好输出流对象,写入目标文件
            try{
                input = new FileInputStream(f1) ;
            }catch(FileNotFoundException e){
                e.printStackTrace() ;
            }
            try{
                out = new FileOutputStream(f2) ;
            }catch(FileNotFoundException e){
                e.printStackTrace() ;
            }
            if(input!=null && out!=null){    // 判断输入或输出是否准备好
                int temp = 0 ;    
                try{
                    while((temp=input.read())!=-1){    // 开始拷贝
                        out.write(temp) ;    // 边读边写
                    }
                    System.out.println("拷贝完成!") ;
                }catch(IOException e){
                    e.printStackTrace() ;
                    System.out.println("拷贝失败!") ;
                }
                try{
                    input.close() ;        // 关闭
                    out.close() ;        // 关闭
                }catch(IOException e){
                    e.printStackTrace() ;
                }
            }
        }    
    }
     
      

  4.   

    额,你用FileReader当然也能读入啊,只是它这题目要求了后面的read(byte[])方法,这个就得用字节流了啊。