绍兴市高速公路建设指挥部网站,电子商务网站建设购物车,wordpress 仿微博模板,成都小程序开发目录
分类
字节流
InputStream
OutputStream
文件拷贝
字符流
FileReader
FileWriter
处理流
BufferedReader
BufferedWriter
文本拷贝 流是从起源到接受的有序数据#xff0c;通过流的方式允许程序使用相同的方式来访问不同的输入/输出源。
分类
按数据…目录
分类
字节流
InputStream
OutputStream
文件拷贝
字符流
FileReader
FileWriter
处理流
BufferedReader
BufferedWriter
文本拷贝 流是从起源到接受的有序数据通过流的方式允许程序使用相同的方式来访问不同的输入/输出源。
分类
按数据单位分字节流(二进制文件) 字符流(文本文件
按数据流向分输入流 输出流
按流的角色分节点流 处理流 IO流的40多个类都是由这四个抽象基类派生的 字节流
InputStream
常用子类
FileInputStream 文件输入流BufferedInputStream 缓冲字节输入流ObjectInputStream 对象字节输入流
import java.io.FileInputStream;public class FileInputStream_ {public static void main(String[] args) {readFile01();System.out.println();readFile02();}public static void readFile01(){String pathD:\\javacode\\IO\\src\\test.txt;int read;FileInputStream fisnull;try {fisnew FileInputStream(path);while((readfis.read())!-1)//读取一个字节的数据读到末尾返回-1{System.out.print((char)read);//一个一个读}} catch (Exception e) {e.printStackTrace();}finally {try {fis.close();} catch (Exception e) {e.printStackTrace();}}}public static void readFile02(){String pathD:\\javacode\\IO\\src\\test.txt;byte[] bufnew byte[8];FileInputStream fisnull;int len;try {fisnew FileInputStream(path);while((lenfis.read(buf))!-1)//读取一个字节的数据读到末尾返回-1{System.out.print((new String(buf,0,len)));//一次最多读入buf.length字节的数据存入字节数组buf}} catch (Exception e) {e.printStackTrace();}finally {try {fis.close();} catch (Exception e) {e.printStackTrace();}}}
}
OutputStream
子类FileOutputStream
import java.io.FileOutputStream;public class FileOutputStream_ {public static void main(String[] args) {writeFile01();}public static void writeFile01(){String pathD:\\javacode\\IO\\src\\test.txt;FileOutputStream fosnull;try {//fosnew FileOutputStream(path);//覆写fosnew FileOutputStream(path,true);//加true,追加文本fos.write(6);fos.write(helloworld.getBytes());//获取str对应的字节数组} catch (Exception e) {e.printStackTrace();}finally {try {fos.close();} catch (Exception e) {e.printStackTrace();}}}
}
#字节流中文会乱码这是因为只能存一个字节一个字节输出
文件拷贝
import java.io.FileInputStream;
import java.io.FileOutputStream;public class FileCopy {public static void main(String[] args) {//文件拷贝FileInputStream fileInputStreamnull;//输入流FileOutputStream fileOutputStreamnull;//输出流//源文件和目标文件String srcpathC:\\Users\\Elysia\\Pictures\\Saved Pictures\\illust_86465238_20210124_003335.jpg;String destpathD:\\javacode\\IO\\src\\pic.jpg;try {fileOutputStreamnew FileOutputStream(destpath,true);fileInputStreamnew FileInputStream(srcpath);byte[] bufnew byte[1024];int len;while((lenfileInputStream.read(buf))!-1){fileOutputStream.write(buf,0,len);}} catch (Exception e) {e.printStackTrace();}finally {try {fileOutputStream.close();fileInputStream.close();}catch (Exception e){e.printStackTrace();}}}
}字符流
FileReader
import java.io.FileReader;public class FileReader_ {public static void main(String[] args) {FileReader fileReadernull;String pathD:\\javacode\\IO\\src\\test.txt;try {fileReadernew FileReader(path);char[] bufnew char[8];//用char接收int len;while((lenfileReader.read(buf))!-1){System.out.print(new String(buf,0,len));}
// int read;//单个读取
// while((read fileReader.read())!-1){
// System.out.print((char)read);
// }} catch (Exception e) {e.printStackTrace();}finally {try {fileReader.close();//关闭文件}catch (Exception e){e.printStackTrace();}}}
}
FileWriter
import java.io.FileWriter;public class FileWriter_ {public static void main(String[] args) {FileWriter fileWriternull;String pathD:\\javacode\\IO\\src\\test.txt;try{fileWriternew FileWriter(path,true);//加true表示追加fileWriter.write(\n成功写入);}catch (Exception e){e.printStackTrace();}finally {try{fileWriter.close();//必须 一定 关闭流才能真正的写入}catch (Exception e){e.printStackTrace();}}}
}
必须 一定 关闭/flush流才能真正的写入 处理流 将“流”包装在其之上为程序提供更强大的读写功能 节点流的数据源是特定的而处理流则利用了向上转型可以封装多种“流”减小了节点流之间的差异。
优势
使用处理流进行 读写操作更简单处理流增加了缓冲提高效率
#在关闭包装流的时候会自动关闭里面封装的字节流
BufferedReader 从字符输入流读取文本缓冲字符以提供字符数组和行的高效读取。
import java.io.BufferedReader;
import java.io.FileReader;public class BufferedReader_ {public static void main(String[] args) throws Exception{String pathD:\\javacode\\IO\\src\\test.txt;BufferedReader bufferedReadernew BufferedReader(new FileReader(path));String line;//按行读取效率高while((linebufferedReader.readLine())!null){System.out.println(line);//读取一行}bufferedReader.close();//只需要关闭包装流}
}
BufferedWriter 文本写入字符输出流缓冲字符以提供单个字符数组和字符串的高效写入。
import java.io.BufferedWriter;
import java.io.FileWriter;public class BufferedWriter_ {public static void main(String[] args) throws Exception{String pathD:\\javacode\\IO\\src\\test.txt;BufferedWriter bufferedWriternew BufferedWriter(new FileWriter(path,true));bufferedWriter.newLine();//插入一个换行bufferedWriter.write(成功写入);bufferedWriter.close();}
}
文本拷贝
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;public class TxtCopy {public static void main(String[] args) throws Exception{String destD:\\javacode\\IO\\src\\dest.txt;String srcD:\\javacode\\IO\\src\\test.txt;BufferedReader bufferedReadernew BufferedReader(new FileReader(src));BufferedWriter bufferedWriternew BufferedWriter(new FileWriter(dest));String line;while((linebufferedReader.readLine())!null){bufferedWriter.write(line);//末尾没有换行符bufferedWriter.newLine();//每读取一行插入换行符}bufferedWriter.close();bufferedReader.close();}
} 文章转载自: http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.zwpzy.cn.gov.cn.zwpzy.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.qjrjs.cn.gov.cn.qjrjs.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.fkwp.cn.gov.cn.fkwp.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.wqfzx.cn.gov.cn.wqfzx.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.dodoking.cn.gov.cn.dodoking.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn http://www.morning.qlznd.cn.gov.cn.qlznd.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn