有关io的一个小问题
以下是我用io写的一个小的copy方法,但是在我测试的时候发现下面的打印语句总是把最后一个数组的内容多打印一次,就是比如说把a.txt中的内容copy到b.txt中,a.txt有127个字符,最后三个是字母是arw,那么在打印的时候arw总要另起一行多打印一次,为什么呢??
public static void copy(File from, File to) throws Exception {
InputStreamReader in = new InputStreamReader(new FileInputStream(from));
OutputStreamWriter ou = new OutputStreamWriter(new FileOutputStream(to));
int n;
char[] c = new char[4];
while ((n = in.read(c)) != -1) {
ou.write(c, 0, n);
System.out.print(c[0]+""+c[1]+""+c[2]+""+c[3]);//此打印语句
}
in.close();
ou.close();
}
----------------解决方案--------------------------------------------------------
我试了,可是没有发现多打印啊????????
有关io的一个小问题
以下是我用io写的一个小的copy方法,但是在我测试的时候发现下面的打印语句总是把最后一个数组的内容多打印一次,
就是比如说把a.txt中的内容copy到b.txt中,a.txt有127个字符,最后三个是字母是arw,那么在打印的时候arw总要另起一行多打印一次,为什么呢??
public static void copy(File from, File to) throws Exception {
InputStreamReader in = new InputStreamReader(new FileInputStream(from));
OutputStreamWriter ou = new OutputStreamWriter(new FileOutputStream(to));
int n;
char[] c = new char[4];
while ((n = in.read(c)) != -1) {
ou.write(c, 0, n);
System.out.print(c[0]+""+c[1]+""+c[2]+""+c[3]);//此打印语句
}
in.close();
ou.close();
}
arw
----------------解决方案--------------------------------------------------------
回复 楼主 syg5434
可我这确实是这样的,打印结果如下:最后的rqw在源文件中是不存在的.....中华人民共和国asdfasdfas
asdfasdfas
asdfasdfasdfasdfsd
qwerq
werqwerrrqwerqwer
qwerqwerqwerqwe
qwreqwrqwerqwerqwerwerqw
rqw
----------------解决方案--------------------------------------------------------