import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class FileCombine {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileManager afilemg = new FileManager("a.txt",new char[]{'\n'});
FileManager bfilemg = new FileManager("b.txt",new char[]{'\n',' '});
FileWriter cfile = new FileWriter("c.txt");
String aword = null;
String bword = null;
while((aword = afilemg.nextWord())!=null){
cfile.write(aword+"\n");
bword = bfilemg.nextWord();
if(bword != null)
cfile.write(bword+"\n");
}
while((bword = bfilemg.nextWord()) != null)
cfile.write(bword);
cfile.close();
}
}
class FileManager{
String[] words = null;
int pos = 0;
public FileManager(String filename, char[]separators)throws Exception{
File f = new File(filename);
FileReader reader = new FileReader(f);
char[] buf = new char[(int)f.length()];
int len = reader.read(buf);
reader.close();
String result = new String(buf,0,len);
String regex = null;
if(separators.length>1)
regex = ""+separators[0]+"|"+separators[1];
else regex = ""+separators[0];
words = result.split(regex);
}
public String nextWord(){
if(pos==words.length)
return null;
else return words[pos++];
}
}
Exception in thread "main" java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileReader.(Unknown Source)
at excises.FileManager.(FileCombine.java:35)
at excises.FileCombine.main(FileCombine.java:11)
请问这是什么问题???
把你文件的具体路径打印出来,看看是否正确,是否有特殊符号在里面等等
找不到a.txt,你看看你把a.txt放哪了?是不是程序目录下?
找不到a.txt,并且肯定也找不到b.txt,建议将a.txt和b.txt放在程序的bin目录下。
跟class文件放在一起
很有可能就是路径问题,检查下你a、b 文件的路径
打印一下当前路径的位置,然后对比一下,对应的确定a、b的位置
a.txt要放在工程的根目录: 工程路径/a.txt, 检查一下a.txt路径是否正确
怎么弄得我也出错了
文件地址要用全局的绝对路径,你测试一下你的路径,看看对不
其实吧你 在 工程目录下 放一个 代码目录下放一下 成功了再删就是了 实在 不行 用绝对路径好了