日期:2014-05-20 浏览次数:20700 次
import java.io.*; public class IOTest { public static void main(String[] args) throws Exception { File f1 = new File("x1.txt"); File f2 = new File("x2.txt"); InputStream is = new FileInputStream(f1); OutputStream os = new FileOutputStream(f2, true); byte[] buf = new byte[1024]; int len = 0; while((len=is.read(buf)) != 0) { if(len > 0) { os.write(buf,0,len); } if(f2.length() == f1.length()*3) { break; } } os.close(); is.close(); } }
import java.io.*; public class IOTest { public static void main(String[] args) throws Exception { File f1 = new File("x1.txt"); File f2 = new File("x2.txt"); InputStream is = new FileInputStream(f1); OutputStream os = new FileOutputStream(f2, true); byte[] buf = new byte[1024]; int len = 0; while((len=is.read(buf)) != -1) { os.write(buf,0,len); } os.flush(); os.close(); is.close(); } }
------解决方案--------------------
x2的结果是一个something is wrong.
因为你读取了x1的内容,并写入了x2.
其余的同上。不过os.flush()可以不写,因为os.close()等于flush
------解决方案--------------------
for(int i=0;i<3;i++){
while((len=is.read(buf)) != -1) {
os.write(buf,0,len,new);
}
}
就可以实现三行了
------解决方案--------------------
import java.io.*;
public class IOTest {
public static void main(String[] args) throws Exception {
File f1 = new File("x1.txt");
File f2 = new File("x2.txt");
InputStream is = new FileInputStream(f1);
OutputStream os = new FileOutputStream(f2, true);
byte[] buf = new byte[1024];
int len = 0;
while((len=is.read(buf)) != 0) {//修改成-1是没有找到内容,不等于-1说明有内容,这样应该就行了吧,你再试试 while((len=is.read(buf)) != -1) {
if(len > 0) {
os.write(buf,0,len);
}
if(f2.length() == f1.length()*3) {
break;
}
}
os.close();
is.close();
}
}