网站开发需要做什么,高端网站建设 房产,威联通如何做网站,职业教育培训网站转载于#xff1a;https://www.cnblogs.com/ll409546297/p/7197911.html java.io几种读写文件的方式 一、Java把这些不同来源和目标的数据都统一抽象为数据流。 Java语言的输入输出功能是十分强大而灵活的。 在Java类库中#xff0c;IO部分的内容是很庞大的#xff0c;因为它…转载于https://www.cnblogs.com/ll409546297/p/7197911.html java.io几种读写文件的方式 一、Java把这些不同来源和目标的数据都统一抽象为数据流。 Java语言的输入输出功能是十分强大而灵活的。 在Java类库中IO部分的内容是很庞大的因为它涉及的领域很广泛:标准输入输出文件的操作网络上的数据流字符串流对象流zip文件流。 这里介绍几种读写文件的方式 二、InputStream、OutputStream字节流 //读取文件(字节流)InputStream in new FileInputStream(d:\\1.txt);//写入相应的文件OutputStream out new FileOutputStream(d:\\2.txt);//读取数据//一次性取多少字节byte[] bytes new byte[2048];//接受读取的内容(n就代表的相关数据只不过是数字的形式)int n -1;//循环取出数据while ((n in.read(bytes,0,bytes.length)) ! -1) {//转换成字符串String str new String(bytes,0,n,GBK); #这里可以实现字节到字符串的转换比较实用System.out.println(str);//写入相关文件out.write(bytes, 0, n);}//关闭流in.close();out.close();三、BufferedInputStream、BufferedOutputStream缓存字节流使用方式和字节流差不多但是效率更高推荐使用 //读取文件(缓存字节流)BufferedInputStream in new BufferedInputStream(new FileInputStream(d:\\1.txt));//写入相应的文件BufferedOutputStream out new BufferedOutputStream(new FileOutputStream(d:\\2.txt));//读取数据//一次性取多少字节byte[] bytes new byte[2048];//接受读取的内容(n就代表的相关数据只不过是数字的形式)int n -1;//循环取出数据while ((n in.read(bytes,0,bytes.length)) ! -1) {//转换成字符串String str new String(bytes,0,n,GBK);System.out.println(str);//写入相关文件out.write(bytes, 0, n);}//清楚缓存out.flush();//关闭流in.close();out.close();四、InputStreamReader、OutputStreamWriter字符流这种方式不建议使用不能直接字节长度读写。使用范围用做字符转换 //读取文件(字节流)InputStreamReader in new InputStreamReader(new FileInputStream(d:\\1.txt),GBK);//写入相应的文件OutputStreamWriter out new OutputStreamWriter(new FileOutputStream(d:\\2.txt));//读取数据//循环取出数据byte[] bytes new byte[1024];int len -1;while ((len in.read()) ! -1) {System.out.println(len);//写入相关文件out.write(len);}//清楚缓存out.flush();//关闭流in.close();out.close();五、BufferedReader、BufferedWriter(缓存流提供readLine方法读取一行文本) //读取文件(字符流)BufferedReader in new BufferedReader(new InputStreamReader(new FileInputStream(d:\\1.txt),GBK));#这里主要是涉及中文//BufferedReader in new BufferedReader(new FileReader(d:\\1.txt)));//写入相应的文件BufferedWriter out new BufferedWriter(new OutputStreamWriter(new FileOutputStream(d:\\2.txt),GBK));//BufferedWriter out new BufferedWriter(new FileWriter(d:\\2.txt))//读取数据//循环取出数据String str null;while ((str in.readLine()) ! null) {System.out.println(str);//写入相关文件out.write(str);out.newLine();}//清楚缓存out.flush();//关闭流in.close();out.close();六、Reader、PrintWriterPrintWriter这个很好用在写数据的同事可以格式化 //读取文件(字节流)Reader in new InputStreamReader(new FileInputStream(d:\\1.txt),GBK);//写入相应的文件PrintWriter out new PrintWriter(new FileWriter(d:\\2.txt));//读取数据//循环取出数据byte[] bytes new byte[1024];int len -1;while ((len in.read()) ! -1) {System.out.println(len);//写入相关文件out.write(len);}//清楚缓存out.flush();//关闭流in.close();out.close();七、基本的几种用法就这么多当然每一个读写的使用都是可以分开的。为了更好的来使用io。流里面的读写建议使用BufferedInputStream、BufferedOutputStream转载于:https://www.cnblogs.com/SongG-blogs/p/10932086.html