首页 > 编程语言 >Java--I/O流

Java--I/O流

时间:2024-07-03 16:31:34浏览次数:3  
标签:Java 字节 -- write read int file new

I/O流

InputStream、OutputStream、FileInputStream、FileOutputStream(字节流)

字节输入流InputStream主要方法:

read() :从此输入流中读取一个数据字节。

read(byte[] b) :从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。

read(byte[] b, int off, int len) :从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。

close():关闭此输入流并释放与该流关联的所有系统资源。

字节输出流OutputStream主要方法:

write(byte[] b) :将 b.length 个字节从指定 byte 数组写入此文件输出流中。

write(byte[] b, int off, int len) :将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。

write(int b) :将指定字节写入此文件输出流。

close() :关闭此输入流并释放与该流关联的所有系统资源。

字节流的方式效率较低,不建议使用

案例

public class IOTest {

public static void main(String[] args) throws IOException {

File file = new File("D:/test.txt");

write(file);

System.out.println(read(file));

}

public static void write(File file) throws IOException {

OutputStream os = new FileOutputStream(file, true);

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

// 写入文件

os.write(string.getBytes());

// 关闭流

os.close();

}

public static String read(File file) throws IOException {

InputStream in = new FileInputStream(file);

// 一次性取多少个字节

byte[] bytes = new byte[1024];

// 用来接收读取的字节数组

StringBuilder sb = new StringBuilder();

// 读取到的字节数组长度,为-1时表示没有数据

int length = 0;

// 循环取数据

while ((length = in.read(bytes)) != -1) {

// 将读取的内容转换成字符串

sb.append(new String(bytes, 0, length));

}

// 关闭流

in.close();

return sb.toString();

}

}

BufferedInputStream、BufferedOutputStream(缓冲字节流)

缓冲字节流是为高效率而设计的,真正的读写操作还是靠FileOutputStream和FileInputStream,所以其构造方法入参是这两个类的对象也就不奇怪了。

案例

public class IOTest {

public static void write(File file) throws IOException {

// 缓冲字节流,提高了效率

BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(file, true));

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

// 写入文件

bis.write(string.getBytes());

// 关闭流

bis.close();

}

public static String read(File file) throws IOException {

BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));

// 一次性取多少个字节

byte[] bytes = new byte[1024];

// 用来接收读取的字节数组

StringBuilder sb = new StringBuilder();

// 读取到的字节数组长度,为-1时表示没有数据

int length = 0;

// 循环取数据

while ((length = fis.read(bytes)) != -1) {

// 将读取的内容转换成字符串

sb.append(new String(bytes, 0, length));

}

// 关闭流

fis.close();

return sb.toString();

}

}

InputStreamReader、OutputStreamWriter(字符流)

字符输入流Reader主要方法:

read():读取单个字符。

read(char[] cbuf) :将字符读入数组。

read(char[] cbuf, int off, int len) : 将字符读入数组的某一部分。

read(CharBuffer target) :试图将字符读入指定的字符缓冲区。

flush() :刷新该流的缓冲。

close() :关闭此流,但要先刷新它。

字符输出流Writer主要方法:

write(char[] cbuf) :写入字符数组。

write(char[] cbuf, int off, int len) :写入字符数组的某一部分。

write(int c) :写入单个字符。

write(String str) :写入字符串。

write(String str, int off, int len) :写入字符串的某一部分。

flush() :刷新该流的缓冲。

close() :关闭此流,但要先刷新它。

另外,字符缓冲流还有两个独特的方法:

BufferedWriter类newLine() :写入一个行分隔符。这个方法会自动适配所在系统的行分隔符。

BufferedReader类readLine() :读取一个文本行。

字符流适用于文本文件的读写,OutputStreamWriter类其实也是借助FileOutputStream类实现的,故其构造方法是FileOutputStream的对象

案例

public class IOTest {

public static void write(File file) throws IOException {

// OutputStreamWriter可以显示指定字符集,否则使用默认字符集

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

osw.write(string);

osw.close();

}

public static String read(File file) throws IOException {

InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");

// 字符数组:一次读取多少个字符

char[] chars = new char[1024];

// 每次读取的字符数组先append到StringBuilder中

StringBuilder sb = new StringBuilder();

// 读取到的字符数组长度,为-1时表示没有数据

int length;

// 循环取数据

while ((length = isr.read(chars)) != -1) {

// 将读取的内容转换成字符串

sb.append(chars, 0, length);

}

// 关闭流

isr.close();

return sb.toString()

}

}

//缓冲字符流

public static void write(File file) throws IOException {

// BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new

// FileOutputStream(file, true), "UTF-8"));

// FileWriter可以大幅度简化代码

BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

bw.write(string);

bw.close();

}

public static String read(File file) throws IOException {

BufferedReader br = new BufferedReader(new FileReader(file));

// 用来接收读取的字节数组

StringBuilder sb = new StringBuilder();

// 按行读数据

String line;

// 循环取数据 readLine读取一行

while ((line = br.readLine()) != null) {

// 将读取的内容转换成字符串

sb.append(line);

}

// 关闭流

br.close();

return sb.toString();

}

标签:Java,字节,--,write,read,int,file,new
From: https://blog.csdn.net/weixin_68489989/article/details/140154631

相关文章

  • Mybatis使用foreach执行in语句、批量增删改查
    参考:https://www.cnblogs.com/leeego-123/p/10725210.html一、xml文件中foreach的主要属性foreach元素的属性主要有collection,item,index,separator,open,close。collection:表示集合,数据源item:表示集合中的每一个元素index:用于表示在迭代过程中,每次迭代到的位置separator:表示在......
  • SHAP 工作原理
    目录IntroductionIntroductionSHAP(ShapleyAdditiveExplanations)是基于博弈论的shapley值,来衡量各个特征对模型预测值的贡献度。那么什么是shapley值,SHAP又是如何使用它来解释模型的呢?ShapleyValuesShapleyvalues是LloydShapley提出的,主要是为了解决以下问题:If......
  • 【每日一练】python列表
    1、输入一个整数列表,将列表中的元素按照逆序输出。list1=[5,4,5,6]list1.reverse()print(list1)[6,5,4,5]2、输入一个字符串列表,输出其中长度大于等于5的字符串,并且将它们转换为大写形式。list1=['hello','lol','ak47','aliang']foriinlist1:iflen(i)......
  • Asp.Net Core -Authorizationz授权
    授权的内部实现参考动态授权参考 动态授权基于权限的授权1.定义权限-PermissionspublicclassPermissions{publicconststringAdmin="Admin";publicconststringUsers="Users";publicconststringUserCreate=Users+".Create";......
  • 2024.07.03【读书笔记】|医疗科技创新流程(第二章 创新创造 商业模式基础)
    目录一级目录二级目录三级目录4.4商业模式基础引言商业模式定义商业模式的重要性商业模型的筛选影响客户与创新互动的主要因素商业模式的选择商业模式的变革创新者的角色总结一级目录二级目录三级目录4.4商业模式基础引言在医疗设备领域,商业模式通常是指如......
  • Maven安装与配置
    1.安装maven前提条件:已安装好JDK下载地址:https://maven.apache.org/download.cgi配置环境变量:新建系统变量MAVEN_HOME变量值:C:\Java\apache-maven-3.9.8编辑系统变量Path添加变量值:%MAVEN_HOME%\bin#执行有输出结果mvn-v2.配置镜像源编辑C:\Java\apach......
  • Java实现生成二维码及二维码解析
    1、导入所需的工具类jar包<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.google.zxing</gro......
  • 听专家的,不如听国家的,网络安全究竟值不值得报?
    考学选专业,或者跳槽选行业的,看这篇!如果你什么都不懂,家里也没有矿,那就紧跟国家大事和地方政策。关于网络安全专业究竟是否值得报考?要知道“二十大”、“十四五”等大会一直在提一个词叫做“数字中国建设”,数字化的进程也必然会带来很多风口。5G带来物联网和人工智能的风口,......
  • spring-security安全框架(超精细版附带流程讲解图)
    目录一、回顾一下二、security使用2.1覆盖掉默认配置「自定义配置」2.2如何自定义认证2.3纯纯自定义2.4jwt2.5官网认证流程2.6 RBAC模型4.1.创建表结构2.7如何实现权限流程一、回顾一下security干啥的?认证和授权使用方式引入依赖,基于springboo......
  • python编写使用xmlrpc上传wordpress网站文章的程序
    1、安装库        pipinstallpython-wordpress-xmlrpc,tkinter,xmlrpc,json2、发布文章url="http://域名/xmlrpc.php"username=用户名password=密码title=标题content=内容tags=标签categories=分类client=C......