Java新手求助,将两个文件合并为一个文件执行程序是发生的问题

各位老师帮忙看看,我是刚接触Java的新手,下面程序代码是昨天在学习中根据书本上的程序修改过的,将文件写与读分两个类,可是在执行程序时出错,出错代码提示说问题是出在第39和104行中,我看出来就是在合并文件流时出错,经过认真的研究觉得应该是错在我使用方法时传递参数不对,由于经验不足还是没有搞懂,所以来园子求助,有知道的老师请指导一下,先谢谢了!

附上出错截图

复制代码
  1 import java.io.*;
  2 public class WRDemo
  3 {
  4     public static void main(String[] args) throws IOException
  5     {
  6         //实例化WriterFile和ReaderFile对象
  7         WriterFile w=new WriterFile();
  8         ReaderFile r=new ReaderFile();
  9         //声明两个文件读入流
 10         FileInputStream in1=null,in2=null;
 11         File inputFile=null;
 12         try {
 13                 String [] filename={"d:\\java_test\\1.txt","d:\\java_test\\2.txt","d:\\java_test\\12.txt"};
 14                 for(int i=0;i<filename.length-1;i++)
 15                 {            
 16                     inputFile=new File(filename[i]);
 17                     if(inputFile.exists())
 18                     {
 19                         System.out.println(inputFile.getName()+"文件存在!删除重新建立文件");
 20                         inputFile.delete();
 21                         w.run(inputFile,i+1);
 22                         if(i==0)
 23                             in1=r.run(inputFile);
 24                         if(i==1)
 25                             in2=r.run(inputFile);
 26                     }
 27                     else {
 28                         System.out.println(inputFile.getName()+"文件不存在!创建文件");
 29                         w.run(inputFile,i+1);
 30                         if(i==0)
 31                             in1=r.run(inputFile);
 32                         if(i==1)
 33                             in2=r.run(inputFile);
 34                     }
 35                 } 
 36                 //构造一个输出文件
 37                 File outputfile=new File(filename[2]); 
 38                 //将两个文件合为一个输入流
 39                 w.run1(in1,in2,outputfile);
 40                 System.out.println("ok...");
 41                 in1.close();
 42                 in2.close();
 43 
 44             }
 45             catch (IOException ex) {
 46                 ex.printStackTrace();
 47             }
 48             finally{
 49                 if(in1!=null)
 50                     try 
 51                     {
 52                         in1.close();
 53                     }
 54                     catch (IOException ex) 
 55                     {
 56                         ex.printStackTrace();
 57                     }
 58                     if(in2!=null)
 59                     try 
 60                     {
 61                         in2.close();
 62                     }
 63                     catch (IOException ex) 
 64                     {
 65                         ex.printStackTrace();
 66                     }        
 67             }
 68     }
 69 }
 70 class WriterFile 
 71 {
 72     public FileOutputStream run(File f,int i)
 73     {
 74         //构造一个输出流
 75         FileOutputStream out=null;
 76         try {
 77             out=new FileOutputStream(f); 
 78         }
 79         catch (IOException ex) {
 80             ex.printStackTrace();
 81         }
 82         String str="我是第"+i+"个文件"+i+".txt"+"\r\n"; //这两个转义字符在Windows系统中代表换行"\r\n"
 83         System.out.println("向第"+i+"个文件写入:\r\n"+str);
 84         byte[] f1=str.getBytes();
 85         try {
 86                 //将str写入文件
 87                 out.write(f1);
 88                 out.close();
 89         }
 90         catch (IOException ex) {
 91                 ex.printStackTrace();
 92         }    
 93         
94 } 95 public void run1(FileInputStream in1,FileInputStream in2,File outputFile) 96 { 97 SequenceInputStream s=null; 98 FileOutputStream out=null; 99 try 100 { 101 s=new SequenceInputStream(in1,in2); 102 out=new FileOutputStream(outputFile); 103 int c; 104 while((c=s.read())!=-1) 105 out.write(c); 106 s.close(); 107 out.close(); 108 } 109 catch (IOException ex) 110 { 111 ex.printStackTrace(); 112 } 113 finally { 114 if(s!=null) 115 try 116 { 117 s.close(); 118 } 119 catch (IOException ex) 120 { 121 ex.printStackTrace(); 122 } 123 if(out!=null) 124 try 125 { 126 out.close(); 127 } 128 catch (IOException ex) 129 { 130 ex.printStackTrace(); 131 } 132 } 133 } 134 } 135 136 class ReaderFile 137 { 138 public FileInputStream run(File f) 139 { 140 //声明一个文件读入流 141 FileInputStream in =null; 142 try { 143 in=new FileInputStream(f); 144 in.close(); 145 } 146 catch (IOException ex) { 147 ex.printStackTrace(); 148 } 149 finally{ 150 if(in!=null) 151 try { 152 in.close(); 153 } 154 catch (IOException ex) { 155 ex.printStackTrace(); 156 } 157 } 158 return in; 159 } 160 }
复制代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值