当前位置:网站首页>I/o basic knowledge sorting
I/o basic knowledge sorting
2022-06-10 06:54:00 【Brother Xiaohao learns programming】
I/O The following function types are involved in the stream
The readout is divided into the following 5 Kind of :
FileReader Read file contents ( Convert the file into a character stream to read , It reads char Array or String )
BufferReader packing FileReader Generate buffer More efficient
InputStream It's an interface ( You can't new InputStream);
FileInputStream Read file contents ( Byte stream read , binary data , It reads byte Array .)
InputStreamReader Stream bytes into character streams
Writing is divided into corresponding 5 Kind of :
FileWriter
BufferedWriter
OutputStream
FileOutputStream
OutputStreamWriter
Be sure to distinguish which is written , Which one is read
read out :
FileInputStream
FileReader
write in :
FileOutputStream
FileWriter
Don't talk much , Go straight to the example ( Copy code directly try catch that will do )
1. Read file contents
FileReader Read
File file = new File(" File pathname ");
// Read file related functions
FileReader fileReader = null;
fileReader = new FileReader("file");
char c = 0;
do {
c=(char)fileReader.read();// Read a character
System.out.println(c);
}while (c != '-1');
fileReader.close();
BufferReader and FileReader Combined reading
File file = new File(" File path ");
BufferedReader bufferedReader=null;
bufferedReader = new BufferedReader(new FileReader(file));
String s;
while((s=bufferedReader.readLine()) != null){
System.out.println(s);
}
bufferedReader.close();
//bufferedReader stay close Will automatically put FileReader to close fall
InputStreamReader new come out FileInputStream Read
Mode one
File file = new File(" File path ");
InputStreamReader inputStreamReader = null;
inputStreamReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
char[] ch=new char[2014];
int len=inputStreamReader.read(ch);
System.out.println(new String(ch,0,len));
inputStreamReader.close();
The parameter in the method needs to be passed a byte Type array .
Mode two
File file = new File(" File path ");
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(new FileInputStream(file));
StringBuffer stringBuffer = new StringBuffer();
while(inputStreamReader.ready()) {
stringBuffer.append((char)inputStreamReader.read());
}
System.out.println(stringBuffer.toString());
inputStreamReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Read what you type from the keyboard
BufferReader and InputStreamReader A mixture of
According to the line read
BufferedReader bufferedReader = null;
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" When the input “ sign out ” Exit input when :");
String s = "";
do {
s = bufferedReader.readline();
System.out.println(s);
}while (c!=" sign out ");
bufferedReader.close();
Read by character
BufferedReader bufferedReader = null;
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" When the input ‘q’ Exit input when :");
char c;
do {
c = (char) bufferedReader.read();
System.out.println(c);
}while (c!='q');
bufferedReader.close();
2. Write file contents
FileWriter write in
File file = new File(" File path ");
FileWriter fileWriter = null;
fileWriter = new FileWriter(file);
fileWriter.append(" Insert content ");
fileWriter.close();
BufferedWriter and FileWriter Combined write
File file = new File(" File path ");
BufferedWriter bufferedWriter = null;
bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(" Insert content ");
bufferedWriter.close();
OutputStreamWriter new come out FileOutputStream write in
If FileOutputStream If the object file does not exist, the file will be created automatically
File file = new File(" File path ");
FileOutputStream fileOutputStream = new FileOutputStream("file ");
OutputStreamWriter outputStreamWriter = null;
outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
outputStreamWriter.append(" Insert content ");
outputStreamWriter.append("\r\n");// Line break
outputStreamWriter.close();
fileOutputStream.close();
Using interfaces OutputStream new come out FileOutputStream Write with other files
( Other documents here refer to Excel Form file )
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream("workbook.xlsx");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("firstSheet");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue(1);
workbook.write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use WorkBook Of write() The parameters in the method must be OutputStream
边栏推荐
- 最长公共子序列
- 2022-2027 (New Edition) forecast report on application status and development potential of China's superhard material industry
- 判断进程是否有管理员权限
- Nignx configuring websocket
- SQL practice: SQL optimization problems
- [today in history] March 2: Yahoo was officially established; PC design pioneer was born; [email protected] Bankruptcy
- 微信团队分享:微信后台在海量并发请求下是如何做到不崩溃的
- TeleyeControlor V8.47版本发布 更改Socket框架
- 在 Kubernetes 中基于 StatefulSet 部署 MySQL(下)
- Jerry's aicmd_ SET_ BT_ Addr command format [chapter]
猜你喜欢

Qt--- create dialog 2: quick dialog design

Multiple solutions to one problem × 5 (array + circular linked list)

Nignx configuring websocket

小程序:通过getCurrentPages获取当前页面路由信息

leetcode.38 ---外观数列

深度解析智能運維下告警關聯頻繁項集挖掘算法原理

Typecho模板 vCards/简约个性便的个人博客主题模板

Read in one second: the practical operation of increasing capital and shares of enterprises!

Wechat team sharing: how the wechat background does not crash under massive concurrent requests

POC_Jenkins
随机推荐
JS数据交互之本地存储
YoseZang 原创 特征码定位器 SignatureTest V6.36 Rls 发布
Data migration of OLAP tool Doris or starrocks
Qt--- create dialog box 3: implementation of shape variable dialog box
Matlab: polynomial representation and its basic operations
bson,json
QT---创建对话框2:快速对话框设计
Tensorflow experiment 10 - image flipping and zooming
Cannot open 'appears when Proteus simulates p * * * \lisa5476 Error in SDF '
Liuyongzhi: one code communication defect analysis and architecture design scheme - sound network developer entrepreneurship lecture VOL.02
Teleyecontroller v8.69 reconfiguration of keyboard recording function release by:yose
一举刷新 54 个中文 NLP 任务基准,ERNIE3.0加持下的EasyDL可能是市面上最好用的NLP开发平台...
CMD of Jerry's AI protocol_ SET_ BLE_ Visibility [chapter]
判断进程是否有管理员权限
8-1不安全的文件下载原理和案例演示
Solving the problem of interval updating + interval maximum value by block sequence
Successfully solved: importerror: cannot import name 'import' from 'sklearn preprocessing
小程序:通过getCurrentPages获取当前页面路由信息
解析:稳定币不是“稳定的币”,其本质是一种产品
推荐几本软件工程方向的书