各位老师帮忙看看,我是刚接触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 }

转载于:https://www.cnblogs.com/xhyx/archive/2013/02/21/java201302211105.html

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

  1. python合并两个excel文件_Python将多个excel文件合并为一个文件

    利用Python,将多个excel文件合并为一个文件 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中 ...

  2. 如何将多个文件夹中的文件合并到一个文件夹中

    Question to say "I can!" 如何将多个文件夹中的文件合并到一个文件夹中 2011-11-27 这个算是小小的技巧吧!有两种方法,一种是传统的命令行,另一种是用 ...

  3. linux下将多个文件去除文件头合并_shell命令实现当前目录下多个文件合并为一个文件的方法...

    当前目录下多个文件合并为一个文件 1.将多个文件合并为一个文件没有添加换行符 find ./ -name "iptv_authenticate_201801*" | xargs c ...

  4. 如何利用python整合excel_Python将多个excel文件合并为一个文件

    Python将多个excel文件合并为一个文件 这篇文章主要为大家详细介绍了Python将多个excel文件合并为一个文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 思路 利用python x ...

  5. python处理多个excel文件-Python将多个excel文件合并为一个文件

    利用Python,将多个excel文件合并为一个文件 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中 ...

  6. 多个pdf文件如何合并为一个文件?怎样将多个pdf文件合并到一个文件?

    怎样把多个pdf文件合并到一个文件? 现在PDF文件是我们日常办公中比较常见的一种文件格式, 我们在处理PDF文件的时候,难免会遇到各种各样的问题.有时候我们需要把多个pdf合并成一个pdf,但是还有 ...

  7. 利用Python将多个excel文件合并为一个文件

    利用Python,将多个excel文件合并为一个文件 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中 ...

  8. 将一个文件夹下的所有pdf文件合并为一个文件

    文章目录 1.将一个文件夹下的所有pdf文件合并为一个文件 这里还有一个 2.重新生成PDF文件 如题,每次都要重新写脚本很烦人,放在CSDN当做工具吧! 1.将一个文件夹下的所有pdf文件合并为一个 ...

  9. python 整合excel_Python将多个excel文件合并为一个文件

    # -*- coding: utf-8 -*- #将多个Excel文件合并成一个 import xlrd import xlsxwriter #打开一个excel文件 def open_xls(fil ...

  10. python合并两个excel文件_利用Python将多个excel文件合并为一个文件

    # -*- coding: utf-8 -*- #导入需要使用的包 import xlrd  #读取Excel文件的包 import xlsxwriter   #将文件写入Excel的包 #打开一个e ...

最新文章

  1. 监控MySQL数据库的主从状态的shell脚本
  2. SQL的OPENROWSET开启和使用方法
  3. 树莓派AI视觉云台——6、Linux常用命令及vim编辑器的使用
  4. 七牛云徐晶:低延迟互动时代看好WebRTC和SRT
  5. 你所不知道的setTimeout
  6. JavaWeb的web.xml标签元素(一)
  7. linux查看默认启动服务
  8. 浏览器中performance的基本使用
  9. 盖洛普优势识别器2.0-《现在.发现你的优势》升级版
  10. 你有没有玩命爱过一个姑娘
  11. gmtime() php,C语言gmtime()函数:把clock中的时间转换为格林尼治标准时间
  12. 18-09-20 关于Xlrd和Xlwt的初步学习
  13. 5分钟学会使用Excel插入数据统计图
  14. Matlab中的Smith 预估器
  15. 使用Matlab pcode的风险
  16. 现在做电商晚了吗?电商可以赚零花钱吗?
  17. 这个儿童节,我们和小时候有什么区别?
  18. 【Spring框架一】——Spring框架简介
  19. java 由低位到高位倒序输出
  20. 基于视觉的车道线识别技术在智能车导航中的应用研究

热门文章

  1. 当局为器,人民为水。当局什么样,人民就什么样
  2. 博客V7之后,喜庆排名进入2000之内
  3. 微型计算机主要特点有哪些,微型计算机的主要特点是什么呢?
  4. C# label控件竖直显示 label控件垂直显示
  5. 关于用C#调用C++的dll中的函数,获取字符串返回值的一些细节
  6. 联想服务器如何进入pe系统,如何制作支持Legacy BIOS和UEFI BIOS两种模式启动的Windows PE...
  7. mysql资源限制_超出了MariaDB / MySQL资源限制
  8. dede rss.php,[经验]dede全站RSS订阅静态输出的办法
  9. django mysql secure_auth_MySQL8.0的用户密码加密方式Django2.1兼容。
  10. 俄勒冈州立大学计算机科学专业,美国俄勒冈州立大学专业介绍