文件合并单词,放到文件c中
package lianxi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
public class HeBingFile {
public HeBingFile(String file1,String file2,String toFileDir){
FileManger filemanger1=new FileManger(file1);
FileManger filemanger2=new FileManger(file2);
System.out.println((filemanger1.getStringArray().length)+":"+(filemanger2.getStringArray().length));
try {
copyContent(toFileDir,filemanger1.getStringArray(),filemanger2.getStringArray());
}
catch (IOException e) {
e.printStackTrace();
}
}
public void copyContent(String filePath,String[] file1,String[] file2) throws IOException{
File file =new File(filePath);
if(!file.exists()){
File tempDir=file.getParentFile();
if(!tempDir.exists()){
tempDir.mkdirs();
}
file.createNewFile();
}
BufferedWriter bw=new BufferedWriter(new FileWriter(file));
int maxSize=file1.length>file2.length?file1.length:file2.length;
for(int i=0;i<maxSize;i++){
if(i<file1.length){
bw.write(file1[i]+'\n');
}
if(i<file2.length){
bw.write(file2[i]+" ");
}
}
bw.close();
}
private class FileManger{
String filePath;
String[]strArray;
public FileManger(String filePath){
this.filePath=filePath;
try {
transFileContent(this.filePath);
}
catch (IOException e) {
e.printStackTrace();
}
}
private void transFileContent(String filePath) throws IOException{
File file=new File(filePath);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>