当前位置:网站首页>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("执行完毕");
}
}
边栏推荐
- 【机器学习】数据科学基础——机器学习基础实践(二)
- A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)
- Teach you how to get a 0.1-meter high-precision satellite map for free
- "The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
- 2.线程创建
- HD Satellite Map Browser
- JS-常用方法整理
- 记录一次被入侵5900端口经历
- 问卷问题和答案的合并
- Environment build onnxruntime 】
猜你喜欢

JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...

div模拟textarea文本框,输入文字高度自适应,且实现字数统计和限制

常用功能测试的检查点与用例设计思路

The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented

本体开发日记02-sparql简单查询

lateral view explode的另一种实现方式

接口性能测试方案设计方法有哪些?要怎么去写?

QT sets the icon of the exe executable

2048小游戏成品源码

接口设计
随机推荐
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
web测试之功能测试常用的方法有哪几种?有什么要点要注意?
Ontology development diary 02 - simple sparql query
本体开发日记02-sparql简单查询
Jfinal loading configuration file principle
What does the test plan include?What is the purpose and meaning?
4.泛型和工具类
你一定要看的安装及卸载测试用例的步骤及方法总结
2. Byte stream
1. Introduction to threads
本体开发日记05-努力理解SWRL(中)
有返回值的函数
MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression
[Personal study summary] CRC verification principle and implementation
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
本体开发日记05-努力理解SWRL(Usage Suggestions)
常用命令之思科常用基础配置
Do you know the basic process and use case design method of interface testing?
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
Do you know the principles of test cases and how to write defect reports?