更新时间:2021年03月18日 08时58分24秒 来源:黑马程序员论坛
字节流 节点流FileInputStream的使用 构造方法: FileInputStream fis = new FileInputStream("文件目录"); FileInputStream fis = new FileInputStream(文件对象); 常用方法 int fis.read() 读取一个字节的数据,并返回int类型,如果返回-1表示文件读完了 int fis.read(byte[] bys) 将读取的内容装进字节数组内,字节数组要提前初始化,一般设置为1024的整数倍,返回的是当次读取到的字节数 节点流FileOutputStream的使用 构造方法: 同上 常用方法 void fos.write(int b) 将指定字节b写入文件 void fos.write(byte[] bys) 将字节数组写入文件 void fos.write(byte[] bys, int off, int len) 将字节数组部分写入文件,并设置偏移量和长度,本方法常用! 文件拷贝 public static void copy(File src,File dest) throws IOException { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dest); byte[] bys = new byte[1024]; //自定义缓冲区 int len = 0; while((len = fis.read(bys))!=-1) { fos.write(bys,0,len); } fos.close(); fis.close(); } 套接流BufferedInputStream的使用 带缓冲区的字节流 构造法方法: BufferedInputStream bis = new BufferedInputStream(InputStream的实现对象); BufferedInputStream bis = new BufferedInputStream(InputStream的实现对象, int size); //自定义缓冲区大小,默认是8192字节8KB 常用方法: 使用方法同FileInputStream,但是由于自带缓冲区,读写效率极大提高,建议使用带缓冲区的流 套接方式: 该类可套接FileInputStream 套接流BufferedOutputStream的使用 构造方法 BufferedOutputStream bos = new BufferedOutputStream(InputStream的实现对象); BufferedOutputStream bos = new BufferedOutputStream(InputStream的实现对象, int size); //自定义缓冲区大小,默认是8192字节8KB 常用方法: 使用方法同FileOutStream,但是由于自带缓冲区,读写效率极大提高,建议使用带缓冲区的流 该类可套接FileOutputStream BufferedOutputStream和自定义缓冲区的FileOutputStream区别是 BufferedOutputStream在缓冲区不满的情况下不会写入操作,需要flush()方法 文件拷贝四种方法 public class Test { public static void main(String[] args) throws IOException{ File srcFile = new File("C:\\Users\\liweihao\\Documents\\就业班课程15\\day9\\视频\\15_IO流小结.mp4"); File destFile = new File("myLesson\\qq.avi"); long beginTime = System.currentTimeMillis(); copy_02(srcFile,destFile); long endTime = System.currentTimeMillis(); System.out.println("一共耗时:"+(endTime-beginTime)); } public static void copy_01(File srcFile, File destFile) throws IOException { //method 1 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); int byt; while((byt=fis.read())!=-1) { fos.write(byt); } fis.close(); fos.close(); } public static void copy_02(File srcFile, File destFile) throws IOException { //method 1 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); int len; byte[] byts = new byte[8192]; while((len=fis.read(byts))!=-1) { fos.write(byts,0,len); } fis.close(); fos.close(); } public static void copy_03(File srcFile, File destFile) throws IOException { //method 1 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); int bys; while((bys=bis.read())!=-1) { bos.write(bys); } bis.close(); bos.close(); } public static void copy_04(File srcFile, File destFile) throws IOException { //method 1 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); int len; byte[] byts = new byte[1024]; while((len=bis.read(byts))!=-1) { bos.write(byts,0,len); } bis.close(); bos.close(); } } 字符流 套接流InputStreamReader 构造方法 InputStreamReader isr = new InputStreamReader(InputStream的实现对象); InputStreamReader isr = new InputStreamReader(InputStream的实现对象,String charset); //指定编码方式,重要 套接方式: 该类可套接FileInputStream 套接流OutputStreamWriter 构造方法 OutStream不写了,。同上 该类可套接FileOutputStream 套接流FileReader和FileWriter 是InputStreamReader和OutputStreamWriter的继承类,本身不提供新方法,但是其构造函数可以直接传文件或文件目录,方便初始化流,一般作为中间件使用 构造方法: 套接流BufferedReader 字符缓冲流,不仅自带缓冲区,还提供了一些特有的方法 构造方法,传Reader类的对象,一般使用FileReader BufferedReader br = new BufferedReader(FileReader("文件路径")); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputstream("文件路径"))); 特有方法:readLine( ) //读一行,但不包括换行符 套接流BufferedWriter 字符缓冲流,不仅自带缓冲区,还提供了一些特有的方法 构造方法,同上 特有方法: write(String s) //可以直接写一行字符串,如果要换行则需要追加\r\n,但是不同操作系统换行符不一样,因此使用下面的方法更好 newLine() //根据不同的操作系统产生对应的换行符 public class BufferedCopyDemo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("myLesson\\f.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("myLesson\\f1.txt")); String str; while((str = br.readLine())!=null) { //注意,读到文件末尾会返回null而不是-1 bw.write(str); bw.newLine(); bw.flush(); } br.close(); bw.close(); } } 编码解码问题 字符串->字节数组 编码过程 字节数组->字符串 解码过程 public static void main(String[] args) throws UnsupportedEncodingException { String s = "中国"; byte[] bys = s.getBytes("GBK");//指定编码字符集 System.out.println(Arrays.toString(bys)); String s2 = new String(bys,"GBK");//指定解码字符集 System.out.println(s2); } 流的编码和解码问题 只能用字符流来处理编码问题 只有InputStreamReader/OutputStreamWriter 类可以处理,在调用构造函数时传入字符集名称作为第二参数 注意:所有自带缓冲区的流,在缓冲区不满/不空的时候是不会进行硬盘的访问操作的 标准输入流 常用的System.in本质上是InputStream,是一个字节流,可以外套字符流处理 可以手动使用套节后的字符流处理一行输入信息,不过Scanner类为我们封装好了这样的方法,使用的时候直接new Scanner(System.in)即可 标准输出流 PrintStream 打印流 构造方法:传入OutputStream的实现类 方法: 除了基本的write方法外,还有各种print方法,可以按数据实际类型打印 PrintWriter 字符打印流 构造方法:传入Writer的实现类 特点:可以在构造的时候指定自动刷新,再结合println方法,可以替代传统的write三连 bw.write(""); bw.newLine(); bw.flush(); 特有方法:println() //直接输出一行,还会自动刷新(需要构造函数指定) 复制文件 public static void main(String[] args) throws IOException { File srcFile = new File("C:\\Users\\liweihao\\Pictures\\wallhaven-673719.jpg"); File destFile = new File("myWork\\pic.jpg"); FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int len; while((len=fis.read(buffer))!=-1) { fos.write(buffer,0,len); } fos.close(); fis.close(); } 递归遍历文件夹中所有文件 public class ShowDirectory { public static void main(String[] args) { File file = new File("C:\\Users\\liweihao\\Documents\\就业班课程15"); show(file); } public static void show(File file) { File[] files = file.listFiles(); for(File f:files) { if(f.isDirectory()) { show(f); }else if(f.isFile()) { System.out.println(f.getAbsolutePath()); } } } } |
推荐了解热门学科
java培训 | Python人工智能 | Web前端培训 | PHP培训 |
区块链培训 | 影视制作培训 | C++培训 | 产品经理培训 |
UI设计培训 | 新媒体培训 | 产品经理培训 | Linux运维 |
大数据培训 | 智能机器人软件开发 |
传智播客是一家致力于培养高素质软件开发人才的科技公司,“黑马程序员”是传智播客旗下高端IT教育品牌。自“黑马程序员”成立以来,教学研发团队一直致力于打造精品课程资源,不断在产、学、研3个层面创新自己的执教理念与教学方针,并集中“黑马程序员”的优势力量,针对性地出版了计算机系列教材50多册,制作教学视频数+套,发表各类技术文章数百篇。
传智播客从未停止思考
传智播客副总裁毕向东在2019IT培训行业变革大会提到,“传智播客意识到企业的用人需求已经从初级程序员升级到中高级程序员,具备多领域、多行业项目经验的人才成为企业用人的首选。”
中级程序员和初级程序员的差别在哪里?
项目经验。毕向东表示,“中级程序员和初级程序员最大的差别在于中级程序员比初级程序员多了三四年的工作经验,从而多出了更多的项目经验。“为此,传智播客研究院引进曾在知名IT企业如阿里、IBM就职的高级技术专家,集中研发面向中高级程序员的课程,用以满足企业用人需求,尽快补全IT行业所需的人才缺口。
何为中高级程序员课程?
传智播客进行了定义。中高级程序员课程,是在当前主流的初级程序员课程的基础上,增加多领域多行业的含金量项目,从技术的广度和深度上进行拓展。“我们希望用5年的时间,打造上百个高含金量的项目,覆盖主流的32个行业。”传智播客课程研发总监于洋表示。
黑马程序员热门视频教程【点击播放】
Python入门教程完整版(懂中文就能学会) | 零起点打开Java世界的大门 |
C++| 匠心之作 从0到1入门学编程 | PHP|零基础入门开发者编程核心技术 |
Web前端入门教程_Web前端html+css+JavaScript | 软件测试入门到精通 |