package lianxi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Test2 {
public static void main(String[] args) {
File a = new File("E:/", "a.txt");
File b = new File("E:/" ,"b.txt");
File c = new File("E:/", "c.txt");
try{
if(a==null){
a.createNewFile();
}
if(b==null){
b.createNewFile();
}
if(c==null){
c.createNewFile();
}
}
catch (Exception e) {
e.printStackTrace();
}
copy(c, b, a);
}
/**
* 将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中
*/
public static void copy(File c,File a,File b){
//读取a文件中的单词
String astr = getMsg(a);
//读取b文件中的单词
String bstr = getMsg(b);
//把a文件中的单词放在数组aw中
String aw[] = getWord(astr);
//把b文件中的单词放在数组bw中
String bw[] = getWord(bstr);
int total = aw.length+bw.length;
//构建新的数组t,长度为aw和bw数组长度之和
String t[] = new String[total];
t = getNewString(t, aw, bw);
copy(c, t);
}
/**
* 读取文件中的内容
*/
public static String getMsg(File file){
FileReader reader=null;
String results = "";
try {
reader = new FileReader(file);
char[] buf = new char[(int)file.length()];
int len = reader.read(buf);
results = new String(buf,0,len);
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(reader!=null){
reader.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return results;
}
public static void main(String[] args) {
File a = new File("E:/", "a.txt");
File b = new File("E:/" ,"b.txt");
File c = new File("E:/", "c.txt");
try{
if(a==null){
a.createNewFile();
}
if(b==null){
b.createNewFile();
}
if(c==null){
c.createNewFile();
}
}
catch (Exception e) {
e.printStackTrace();
}
copy(c, b, a);
}
/**
* 将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中
*/
public static void copy(File c,File a,File b){
//读取a文件中的单词
String astr = getMsg(a);
//读取b文件中的单词
String bstr = getMsg(b);
//把a文件中的单词放在数组aw中
String aw[] = getWord(astr);
//把b文件中的单词放在数组bw中
String bw[] = getWord(bstr);
int total = aw.length+bw.length;
//构建新的数组t,长度为aw和bw数组长度之和
String t[] = new String[total];
t = getNewString(t, aw, bw);
copy(c, t);
}
/**
* 读取文件中的内容
*/
public static String getMsg(File file){
FileReader reader=null;
String results = "";
try {
reader = new FileReader(file);
char[] buf = new char[(int)file.length()];
int len = reader.read(buf);
results = new String(buf,0,len);
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(reader!=null){
reader.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return results;
}
/**
* 按StringTokenizer默认进行截取字符串
* @param str
* @return String数组
*/
public static String[] getWord(String str){
StringTokenizer st = new StringTokenizer(str);
String str1[] = new String[st.countTokens()];
int count =0;
while(st.hasMoreTokens()){
str1[count] = st.nextToken();
count++;
}
return str1;
}
/**
* 根据aw,bw数组构建新的数组t
* @param t
* @param aw
* @param bw
* @return String数组
*/
public static String[] getNewString(String t[],String aw[],String bw[]){
int j=0;
int n=0;
for(int i = 0;i<t.length;i++){
if(i%2==0){
while(j<aw.length){
t[i]=aw[j];
break;
}
j++;
}
else{
while(n<bw.length){
t[i]=bw[n];
break;
}
n++;
}
}
return t;
}
/**
* 把t数组里面的内容写入c文件
* @param c
* @param t
*/
public static void copy(File c,String t[]){
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(c);
for(String t1:t){
t1 = t1+" ";
outputStream.write(t1.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
* 按StringTokenizer默认进行截取字符串
* @param str
* @return String数组
*/
public static String[] getWord(String str){
StringTokenizer st = new StringTokenizer(str);
String str1[] = new String[st.countTokens()];
int count =0;
while(st.hasMoreTokens()){
str1[count] = st.nextToken();
count++;
}
return str1;
}
/**
* 根据aw,bw数组构建新的数组t
* @param t
* @param aw
* @param bw
* @return String数组
*/
public static String[] getNewString(String t[],String aw[],String bw[]){
int j=0;
int n=0;
for(int i = 0;i<t.length;i++){
if(i%2==0){
while(j<aw.length){
t[i]=aw[j];
break;
}
j++;
}
else{
while(n<bw.length){
t[i]=bw[n];
break;
}
n++;
}
}
return t;
}
/**
* 把t数组里面的内容写入c文件
* @param c
* @param t
*/
public static void copy(File c,String t[]){
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(c);
for(String t1:t){
t1 = t1+" ";
outputStream.write(t1.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转载于:https://blog.51cto.com/zhaohaiyang/514360