黑马程序员技术交流社区
标题:
文件复制请高手解决
[打印本页]
作者:
超级小小二郎
时间:
2013-7-3 22:23
标题:
文件复制请高手解决
本帖最后由 杜光 于 2013-7-5 07:30 编辑
编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。
作者:
王靖远
时间:
2013-7-3 22:35
交替合并是存一个a里的单词再存一个b里的单词再存a里的单词这样循环来存?
作者:
hadexs
时间:
2013-7-3 23:15
package cn.itcast;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class MainClass{
public static void main(String[] args) throws Exception{
FileManager a = new FileManager("a.txt",new char[]{'\n'});
FileManager b = new FileManager("b.txt",new char[]{'\n',' '});
FileWriter c = new FileWriter("c.txt");
String aWord = null;
String bWord = null;
while((aWord = a.nextWord()) !=null ){
c.write(aWord + "\n");
bWord = b.nextWord();
if(bWord != null)
c.write(bWord + "\n");
}
while((bWord = b.nextWord()) != null){
c.write(bWord + "\n");
}
c.close();
}
}
class FileManager{
String[] words = null;
int pos = 0;
public FileManager(String filename,char[] seperators) throws Exception{
File f = new File(filename);
FileReader reader = new FileReader(f);
char[] buf = new char[(int)f.length()];
int len = reader.read(buf);
String results = new String(buf,0,len);
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++];
}
}
复制代码
作者:
王靖远
时间:
2013-7-3 23:40
hadexs 发表于 2013-7-3 23:15
:handshake给力
作者:
张承富
时间:
2013-7-4 00:47
给点注释吧,看着费劲
作者:
杨兴庭
时间:
2013-7-4 07:36
为了更好的维护论坛的学习氛围,如果您的的问题“已经解决”请即时将帖子状态改成“已解决”
-------------------坚持学习,黑马因你而精彩---------------
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2