这种方法好点:java
package cglib;数组
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class StringNumber {
public static void main(String args[]) throws Exception{
FileManager a=new FileManager("D:\\文件\\QC处理\\2016年11月\\a.txt",new char[]{'\n'});
FileManager b=new FileManager("D:\\文件\\QC处理\\2016年11月\\b.txt",new char[]{' ','\n'});
FileWriter c=new FileWriter("D:\\文件\\QC处理\\2016年11月\\c.txt");
String aWord = null;
String bWord = null;
while ((aWord = a.nextWord()) != null) {
c.write(aWord);
bWord = b.nextWord();
if (bWord != null) {
c.write(bWord);
}
}
if (bWord != null) {
c.write(bWord);
}
c.close();
System.out.println("finish");
}
}app
class FileManager{
String[] words = null;
int pos = 0;
@SuppressWarnings("resource")
public FileManager(String fileName, char spilt[]) throws Exception {
File file = new File(fileName);
FileReader fr = new FileReader(file);
char buf[] = new char[(int) file.length()];
int len = fr.read(buf);
String bufString = new String(buf, 0, len);
StringBuffer temp = new StringBuffer("");
temp.append(spilt[0]);
if (spilt.length > 1) {
int posl = 2;
while (posl <= spilt.length) {
temp.append("|");
temp.append(spilt[posl - 1]);
posl++;
}
}
String bs = temp.toString();
words = bufString.split(bs);
}
public String nextWord() {
if (pos == words.length) {
return null;
} else {
return words[pos++];
}
}
}it
第二种:io
package cglib;class
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class StringNumber {
public static void main(String args[]) throws Exception{
try{
FileManager a=new FileManager("D:\\文件\\QC处理\\2016年11月\\a.txt",new char[]{'\n'});
FileManager b=new FileManager("D:\\文件\\QC处理\\2016年11月\\b.txt",new char[]{' ','\n'});
FileWriter c=new FileWriter("D:\\文件\\QC处理\\2016年11月\\c.txt");
String aWord= null;
String bWord= null;
//读取一个aWord,调用c写入,读取一个bWord,调用 c写入
while((aWord= a.nextWord()) !=null ){
System.out.println("aWord="+aWord);
c.write(aWord+ "\n");
bWord= b.nextWord();
System.out.println("bWord="+bWord);
if(bWord!= null){
c.write(bWord+ "\n");
}
}
//还得考虑a.txt内容读取完,b.txt还有内容没弄完
while((bWord= b.nextWord()) != null){
c.write(bWord+ "\n");
}
c.close();
System.out.println("finish");
}catch(Exception e){
e.printStackTrace();
}
}
}
class FileManager{
String[] words =null;
int pos = 0;
//把文件转换成String类型,而后分割成String[]
@SuppressWarnings("resource")
public FileManager(String filename,char[] seperators) throws Exception{
File f = new File(filename);
FileReader reader = new FileReader(f);
//声明一个char数组缓冲区
char[] buf =new char[(int)f.length()]; //char占用两个字节
//调用reader读取,放入char数组中
int len =reader.read(buf);
String results = new String(buf,0,len);
//声明一个regex表达式null,而后进行赋值
String regex= null;
if(seperators.length>1 ){
regex= "" + seperators[0] + "|" + seperators[1];
}else{
regex= "" + seperators[0];
}
words =results.split(regex);
}
public String nextWord(){
if(pos ==words.length)
return null;
return words[pos++];
}
}
import