当前位置:网站首页>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("执行完毕");
}
}
边栏推荐
- Django实现对数据库数据增删改查(一)
- 关于一次性通过CISSP考试的一点经验分享
- 软件测试面试题目:请你列举几个物品的测试方法怎么说?
- Golang Protobuf 处理
- 8.递归遍历和删除案例
- 本体开发日记05-努力理解SWRL(上)
- 米斗APP逆向分析
- Do you know the principles of test cases and how to write defect reports?
- GBase数据库产生迁移工具假死的原因是什么?
- 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
猜你喜欢
Ontology Development Diary 03 - When debugging is in progress
软件测试个人求职简历该怎么写,模板在这里
有返回值的函数
makefile学习-解决目标文件输出路径问题
性能测试包括哪些方面?分类及测试方法有哪些?
记录一次被入侵5900端口经历
The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented
软件测试面试中,面试官问你一些比较“刁难”的问题你会怎么回答
【机器学习】数据科学基础——机器学习基础实践(二)
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
随机推荐
Do you know the principles of test cases and how to write defect reports?
软件测试面试思路技巧和方法分享,学到就是赚到
软件测试个人求职简历该怎么写,模板在这里
性能测试的基本概念是什么?做好性能测试需要掌握哪些知识?
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
Ovie map computer terminal and mobile terminal can not be used, is there any alternative map tool
软件测试面试中,面试官问你一些比较“刁难”的问题你会怎么回答
[Personal study summary] CRC verification principle and implementation
本体开发日记04-努力理解protege的某个方面
Django实现对数据库数据增删改查(一)
1.线程简介
6.Map interface and implementation class
自动化测试简历编写应该注意哪方面?有哪些技巧?
功能自动化测试实施的原则以及方法有哪些?
米斗APP逆向分析
2048小游戏成品源码
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(7.4 Annotation Properties-注释属性)
年薪40W测试工程师成长之路,你在哪个阶段?
测试计划包括哪些内容?目的和意义是什么?
接口开发规范及测试工具的使用