java中IO流复制文件的不解?
巴扎黑
巴扎黑 2017-04-17 16:12:02
0
3
263
FileReader fr = null; FileWriter fw = null; int ch = 0; fr = new FileReader("file.txt"); fw = new FileWriter("file_copy.txt"); while((ch=fr.read()) != -1){ fw.write(ch); System.out.print(ch);

上述代码实现对file.txt,文件的复制时,fr.read()方法返回的ch是int,我将ch打印出来是一串数字,但为什么fw却能将字符而不是一串数字写入文档?fw是如何获取fr.read()所读取到的字符?

巴扎黑
巴扎黑

reply all (3)
伊谢尔伦

如果你了解C语言,很容易理解字符本质是数字这个说法。你强制转型ch

System.out.println((char)ch);

输出的就是字符

另外,fw你可以看成是一条通向文件的管道,它并没有获得fr.read()读取的字符,而是ch获取到了,然后你把ch作为参数扔进了这个管道里面。

    巴扎黑

    字符的本质即数字

      阿神

      字符都是可以用ASCII码表示的8位的ASCII码

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!