当前位置:网站首页>4. Character stream
4. Character stream
2022-08-09 09:32:00 【come here my bear】
字符流
- 字符流的父类(抽象类):
- Reader::字符输入流 int read()
- Writer:字符输出流 void write()
- 文件字符流 (FileReader/FileWriter)
- int read(char[] c) 从流中读取多个字符,将读到内容存入c数组,返回实际读到的字符数,如果达到文件的尾部,则返回-1
- void write(String str) 一次写多个字符,将b数组中所有字符,写入输出流
- 字符缓存流 (BufferedReader/BufferedWriter)
- 高效读写
- 支持输入换行符 newLine()
- 可一次写一行,读一行
- 打印流 (PrintWriter/PrintStream)
- 封装了print() / println() 方法,支持写入后换行
- 支持数据原样打印
文件字符流
FileReader
package com.io.zifu;
import java.io.FileReader;
import java.io.IOException;
/**
* 字符流的使用
* FileReader
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
// 创建FileReader对象
FileReader fr = new FileReader("E:\\桌面\\aaa.txt");
// 读取文件
// 第一种读取
// int data=0;
// while ((data=fr.read())!=-1){
// System.out.print((char)data);
// }
// 第二种读取
char[] buf = new char[1024];
int count = 0;
while ((count=fr.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
// 关闭
fr.close();
}
}
FileWriter
package com.io.zifu;
import java.io.FileWriter;
import java.io.IOException;
/**
* 字符流的使用
* FileWriter
*/
public class Demo2 {
public static void main(String[] args) throws IOException {
// 创建FileWriter对象
FileWriter fw = new FileWriter("E:\\桌面\\aaa.txt",true); //true 为追加模式
// 写入
fw.write("Java是世界上最好的语言");
// 关闭
fw.close();
System.out.println("执行完毕");
}
}
FileReader与FileWriter实现复制文件,Images and binary files cannot be copied4
使用字节流可以复制任意文件
package com.io.zifu;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 使用FileReader和FileWriter复制文件,Images and binary files cannot be copied
*/
public class Demo3 {
public static void main(String[] args) throws IOException {
// 创建对象
FileReader fr = new FileReader("E:\\桌面\\aaa.txt");
FileWriter fw = new FileWriter("E:\\桌面\\bbb.txt");
int data=0;
while ((data=fr.read())!=-1){
fw.write((char)data);
}
// 关闭
fr.close();
fw.close();
System.out.println("执行完毕");
}
}
字符缓冲流
BufferedReader
package com.io.zifu;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Use a character buffer stream to read the file
* BufferedReader
*/
public class Demo4 {
public static void main(String[] args) throws IOException {
// 创建BufferedReader对象
FileReader fr = new FileReader("E:\\桌面\\aaa.txt");
BufferedReader br = new BufferedReader(fr);
//读取
// 第一种方式读取
// char[] buf = new char[1024];
// int count = 0;
// while ((count=br.read(buf))!=-1){
// System.out.println(new String(buf,0,count));
// }
// 第二种方式读取,一行一行的读取 readLine()
String line = null;
while ((line=br.readLine())!=null){
System.out.println(line);
}
// 关闭
br.close();
}
}
BufferedWriter
package com.io.zifu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* 字符缓冲流
* BufferedWriter
*/
public class Demo5 {
public static void main(String[] args) throws IOException {
// 创建BufferedWriter对象
FileWriter fw = new FileWriter("E:\\桌面\\aaa.txt",true);
BufferedWriter bw = new BufferedWriter(fw);
// 写入
bw.newLine(); // 换行
bw.write("come on tomorrow");
// 关闭
bw.close();
System.out.println("执行完毕");
}
}
打印流
PrintWriter
package com.io.zifu;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 打印流
* PrintWriter
*/
public class Demo6 {
public static void main(String[] args) throws IOException {
// 创建对象
PrintWriter pw = new PrintWriter("E:\\桌面\\aaa.txt");
// 打印
pw.println("come on tomorrow");
pw.println(3.14);
pw.println('h');
// 关闭
pw.close();
System.out.println("执行完毕");
}
}
边栏推荐
- 进入大厂的面试经验(P7)
- 单元测试是什么?怎么写?主要测试什么?
- 问卷问题和答案的合并
- Do you know the principles of test cases and how to write defect reports?
- static_assert报错为什么?
- 功能自动化测试实施的原则以及方法有哪些?
- QT sets the icon of the exe executable
- 1.线程简介
- A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
- unix环境编程学习-多线程
猜你喜欢
随机推荐
单元测试是什么?怎么写?主要测试什么?
Ontology development diary 04 - to try to understand some aspects of protege
MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression
软件测试的流程规范有哪些?具体要怎么做?
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
Cisco common basic configuration of common commands
4.泛型和工具类
GBase数据库中,源为 oracle 报出“ORA-01000:超出打开游标最大数”
TestNG使用教程详解
字典
初窥门径代码起手,Go lang1.18入门精炼教程,由白丁入鸿儒,首次运行golang程序EP01
Read file by byte and character_load configuration file
MySQL event_single event_timed loop event
Golang Protobuf 处理
1.流的概念
自动化测试框架有哪几种?搭建的思路是什么?一篇文章让你彻底了解自动化
秒拍app分析
本体开发日记05-努力理解SWRL(Built-Ins)
Jfinal loading configuration file principle
常用功能测试的检查点与用例设计思路