(JAVA)IO缓冲区

package IODemo;import java.io.*;
import java.nio.charset.StandardCharsets;/*** @author Alina* @date 2021年10月18日 9:57 下午* 一、  1.IO流关于缓冲区,*      2.输出流缓冲区:BufferedOutputStream(OutputStream out)*      3.输入流缓冲区:BufferedInputStream(InputStream in)*      4.提高流对象传递效率***/
public class FileIOStream {public static void main(String[] args) {long startime = System.currentTimeMillis();try {method_copy3();} catch (Exception e) {e.printStackTrace();}long endtime = System.currentTimeMillis();System.out.println(endtime - startime);}public static void method_bufferedout() throws Exception {//先创建OutputStream()对象,缓冲区方法依赖于流对象FileOutputStream fos = new FileOutputStream("/Users/yuzhang/Desktop/test.txt");BufferedOutputStream bos = new BufferedOutputStream(fos);bos.write("你好".getBytes(StandardCharsets.UTF_8));bos.close();}public static void method_bufferedin() throws Exception {FileInputStream fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");BufferedInputStream bis = new BufferedInputStream(fis);
//        int s = 0;
//        while ((s = bis.read()) != -1){
//            System.out.print((char)s);
//        }
//        bis.close();byte[] bytes = new byte[1024];int x = 0;while ((x = bis.read(bytes)) != -1) {System.out.print(new String(bytes, 0, x));}bis.close();}public static void method_copy1()  {//先创建两个流向,先赋值为空,以避免作用域不同导致无法使用FileInputStream fis = null;FileOutputStream fop = null;try {fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");fop = new FileOutputStream("src/IOdemo/test1.txt");//创建字节int bytes = 0;while ((bytes = fis.read()) != -1) {fop.write(bytes);}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("复制失败");} finally {try {if (fop !=null) {fop.close();}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("复制失败");} finally {try{if (fis != null) {fis.close();}}catch (Exception e ){e.printStackTrace();throw new RuntimeException("关闭数据源失败");}}}}public static void method_copy2(){//读写字节数组FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");fos = new FileOutputStream("src/IOdemo/test1.txt");byte[] bytes = new byte[1024];int len = 0;while ((len = fis.read(bytes))!= -1){fos.write( bytes,0,len);}}catch (Exception e){e.printStackTrace();throw new RuntimeException("复制失败");}finally {try{if(fos !=null ){fos.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("关闭资源失败");}finally {try {if (fis != null){fis.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("关闭资源失败");}}}}public static void method_copy3(){BufferedInputStream bis = null;BufferedOutputStream bos = null;try{bis = new BufferedInputStream(new FileInputStream("/Users/yuzhang/Desktop/test.txt"));bos = new BufferedOutputStream(new FileOutputStream("src/IOdemo/test1.txt"));byte[] bytes = new byte[1024];int len = 0;while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}}catch (Exception e ){e.printStackTrace();throw new RuntimeException(" 复制失败");}finally {try{if (bos != null){bos.close();}}catch (Exception e ){e.printStackTrace();throw new RuntimeException("资源关闭异常");}finally {try{if (bis != null){bis.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("资源关闭异常");}}}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421743.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php正则表达式如何找到匹配模式中的最后一组

转载于:https://www.cnblogs.com/MyFlora/archive/2013/06/07/3124073.html

(JAVA)IO流之读写单个字节和复制文本文件

package IODemo;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;/*** author Alina* date 2021年10月15日 8:19 下午* read()方法特点* 1.每次只读取一个文件* 2.只运行一次&#xff0c…

sql where in 排序问题

直接上代码了 对于int类型的需要转化一下 select * from cvProducts where ID in(972,687,678,962) order by charindex(cast(ID as varchar),972,687,678,962) 对于varchar的直接使用 select * from cvProducts where MouldNo in(C62859,C63417,C32283) order by charindex(…

(JAVA)FileWriter

package IODemo;import java.io.BufferedWriter; import java.io.FileWriter;/*** author Alina* date 2021年10月31日 10:48 下午* FileWrite 写入文本文件的便捷类,方便快捷* 默认查询编码表,不能指定编码表* BufferedWriter 字符输出流的缓冲对象**/ …

JavaWeb中验证码的实现

在Web程序中,验证码是经常使用的技术之一。Web程序永远面临未知用户和未知程序的探测。为了防止恶意脚本的执行,验证码技术无疑是首选方案之一。本文将讨论如何在JSP和Servlet中使用验证码技术。 验证码的产生思路很简单,在Servlet中随机产生…

IO流复制图片

package IODemo;/*** author Alina* date 2021年11月14日 4:32 下午* 复制文件到指定目录**/ import java.io.*; public class IOcopyfile {public static void main(String[] args) {CopyDir(new File(“源文件”),new File(“目标文件”));}public static void CopyDir(File …

苏教版国标本小学语文第一册汉字笔画

苏教版国标本小学语文第一册汉字笔画 转载于:https://www.cnblogs.com/shangdawei/archive/2013/06/09/3129240.html

IO流与对象结合

package IODemo;import java.io.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Iterator;/*** author Alina* date 2021年11月08日 9:13 下午* 把Arraylist 里存储的内容存储到本地文件* 1.遍历集合。遍历一次存储一次*/ public…

正则表达式引擎的构建——基于编译原理DFA(龙书第三章)——1 概述

说明:本系列文章介绍的算法均来自编译原理(龙书)一书,如果读者对代码没有兴趣,只想了解算法思路,完全可以阅读龙书相关章节内容,比我讲得清晰透彻。 序: 啃编译原理半年以来&#xf…

(JAVA)复制文件test.txt,并且排序。文件重新命名为test1.txt

//获取文件内的内容并排序public static void copyFile() throws Exception{//创建File 对象File sourece new File("/Desktop/pic/a.txt");//创建读字符流对象BufferedReader br new BufferedReader(new FileReader(sourece));//一次读取一行String str br.read…

C#如何使用httpwebrequest通过代理访问网页

string urlStr "http://www.itstrike.cn"; //设定要获取的地址 HttpWebRequest hwr (HttpWebRequest)HttpWebRequest.Create(urlStr); //建立HttpWebRequest对象 hwr.Timeout 60000; …

(JAVA)装饰流

package IODemo;/*** author Alina* date 2021年11月15日 11:48 下午* 设计思想:设计模式,装饰模式* JAVA中有23种设计思想,全部基于面向对象* 装饰设计模式,核心思想,解决什么问题* 增强原有对象的功能**/ //第一…

我的学习生涯(Delphi篇) - 21

我们平常要和图片打交道,那么我们如何把图片存在数据库中呢? -------------------------------------------------------------------------------------------------美丽分割线--------------------------- 年代:2007 文件:My091…

Map 的Properties集合存储IO流对象

package IODemo;import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties;/*** author Alina* date 2021年12月07日 11:29 下午* 集合IO一起使用* Map接口实现Hashtable 子类 Properties* 特点:线程安全,泛型Str…