问题描述
publicstaticvoidFileWriter()throwsIOException{QueryBankMessqbm=newQueryBankMess();Datedt=newDate();SimpleDateFormatmatter=newSimpleDateFormat("yyyy-MM-dd");Stringtime=matter.format(dt);System.out.println(time);ListnewList=newArrayList();newList.add("012010"+time+"00000");newList.add("012011"+time+"00000");newList.add("012012"+time+"00000");newList.add("012013"+time+"00000");newList.add("012020"+time+"00000");newList.add("012021"+time+"00000");newList.add("012022"+time+"00000");newList.add("012023"+time+"00000");Filefile=null;FileWriterfw=null;BufferedWriterbw=null;StringfileName=null;Stringb=null;for(intk=0;k<newList.size();k++){fileName=newList.get(k).toString();file=newFile("E:\"+fileName+".txt");fw=newFileWriter(file);}bw=newBufferedWriter(fw);Listlist=qbm.queryBank();Object[]obj=null;try{for(inti=0;i<list.size();i++){obj=(Object[])list.get(i);for(intj=0;j<obj.length;j++){if("01".equals(obj[2])&&"2010".equals(obj[4])){fileName=newList.get(0).toString();}if("01".equals(obj[2])&&"2011".equals(obj[4])){fileName=newList.get(1).toString();}if("01".equals(obj[2])&&"2012".equals(obj[4])){fileName=newList.get(2).toString();}if("01".equals(obj[2])&&"2013".equals(obj[4])){fileName=newList.get(3).toString();}if("01".equals(obj[2])&&"2020".equals(obj[4])){fileName=newList.get(4).toString();}if("01".equals(obj[2])&&"2021".equals(obj[4])){fileName=newList.get(5).toString();}if("01".equals(obj[2])&&"2022".equals(obj[4])){fileName=newList.get(6).toString();}if("01".equals(obj[2])&&"2023".equals(obj[4])){fileName=newList.get(7).toString();}file=newFile("E:\"+fileName+".txt");fw=newFileWriter(file);}}bw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file),"UTF-8"));for(inti=0;i<list.size();i++){obj=(Object[])list.get(i);for(intj=0;j<obj.length;j++){b=String.valueOf(obj[j]);if(j==2||j==4){continue;}bw.append(b);bw.append("t");}bw.newLine();}bw.flush();fw.close();}catch(Exceptione){//TODO:handleexceptione.printStackTrace();}}数据库中查出来的数据obj[2]和obj[4]拼接到一起刚好是012020和012022这个方法也进入了那两个if判断但是为什么写入txt时都写入到了012020开头的那个txt文件了,没有写入到012022开头的txt文件中
解决方案
解决方案二:
publicstaticvoidFileWriter()throwsIOException{QueryBankMessqbm=newQueryBankMess();Datedt=newDate();SimpleDateFormatmatter=newSimpleDateFormat("yyyy-MM-dd");Stringtime=matter.format(dt);System.out.println(time);ListnewList=newArrayList();newList.add("012010"+time+"00000");newList.add("012011"+time+"00000");newList.add("012012"+time+"00000");newList.add("012013"+time+"00000");newList.add("012020"+time+"00000");newList.add("012021"+time+"00000");newList.add("012022"+time+"00000");newList.add("012023"+time+"00000");Filefile=null;FileWriterfw=null;BufferedWriterbw=null;StringfileName=null;Stringb=null;for(intk=0;k<newList.size();k++){fileName=newList.get(k).toString();file=newFile("E:\"+fileName+".txt");fw=newFileWriter(file);}bw=newBufferedWriter(fw);Listlist=qbm.queryBank();Object[]obj=null;try{for(inti=0;i<list.size();i++){obj=(Object[])list.get(i);for(intj=0;j<obj.length;j++){if("01".equals(obj[2])&&"2010".equals(obj[4])){fileName=newList.get(0).toString();}if("01".equals(obj[2])&&"2011".equals(obj[4])){fileName=newList.get(1).toString();}if("01".equals(obj[2])&&"2012".equals(obj[4])){fileName=newList.get(2).toString();}if("01".equals(obj[2])&&"2013".equals(obj[4])){fileName=newList.get(3).toString();}if("01".equals(obj[2])&&"2020".equals(obj[4])){fileName=newList.get(4).toString();}if("01".equals(obj[2])&&"2021".equals(obj[4])){fileName=newList.get(5).toString();}if("01".equals(obj[2])&&"2022".equals(obj[4])){fileName=newList.get(6).toString();}if("01".equals(obj[2])&&"2023".equals(obj[4])){fileName=newList.get(7).toString();}file=newFile("E:\"+fileName+".txt");fw=newFileWriter(file);}}bw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file),"UTF-8"));for(inti=0;i<list.size();i++){obj=(Object[])list.get(i);for(intj=0;j<obj.length;j++){b=String.valueOf(obj[j]);if(j==2||j==4){continue;}bw.append(b);bw.append("t");}bw.newLine();}bw.flush();fw.close();}catch(Exceptione){//TODO:handleexceptione.printStackTrace();}}
重新整理下求大神指点