文件合并单词,放到文件c中
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 10:43 点击:次
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;
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);
if(file.exists()){
char[]fileSize=new char[(int)file.length()];//文件太大应该如何应付
BufferedReader br=new BufferedReader(new FileReader(file));
br.read(fileSize);
br.close();
if(fileSize!=null && fileSize.length>0){
String tempString=new String(fileSize,0,fileSize.length);
StringTokenizer strToken=new StringTokenizer(tempString,",|| ||.|| ,||?||!||。");
strArray=new String[strToken.countTokens()];
&n
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);
if(file.exists()){
char[]fileSize=new char[(int)file.length()];//文件太大应该如何应付
BufferedReader br=new BufferedReader(new FileReader(file));
br.read(fileSize);
br.close();
if(fileSize!=null && fileSize.length>0){
String tempString=new String(fileSize,0,fileSize.length);
StringTokenizer strToken=new StringTokenizer(tempString,",|| ||.|| ,||?||!||。");
strArray=new String[strToken.countTokens()];
&n
相关新闻>>
- 发表评论
-
- 最新评论 更多>>