当前位置:网站首页>IO stream code
IO stream code
2022-06-27 21:39:00 【continueLR】
Catalog
FileInputStrea Byte input stream
FileOutputStream Byte output stream
FileReader Character input stream
FileWriter Character output stream
Copy : Use FileInputStream + FileOutputStream Complete the copy of the file .
Use FileReader FileWriter If you copy , You can only copy “ Plain text ” file .
FileInputStrea Byte input stream
package com.bjpowernode.java.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
The final version , Need to master .
*/
public class FileInputStreamTest04 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("chapter23/src/tempfile3");
// Prepare one byte Array
byte[] bytes = new byte[4];
/*while(true){
int readCount = fis.read(bytes);
if(readCount == -1){
break;
}
// hold byte Array to string , How many conversions do you read .
System.out.print(new String(bytes, 0, readCount));
}*/
int readCount = 0;
while((readCount = fis.read(bytes)) != -1) {
System.out.print(new String(bytes, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}FileOutputStream Byte output stream
package com.bjpowernode.java.io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* File byte output stream , Responsible for writing .
* From memory to hard disk .
*/
public class FileOutputStreamTest01 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
// myfile When the file does not exist, it will be created automatically !
// It's a way to use it with caution , In this way, the original file will be emptied first , And then rewrite it .
//fos = new FileOutputStream("myfile");
//fos = new FileOutputStream("chapter23/src/tempfile3");
// Write... At the end of the file by appending . The contents of the original file will not be emptied .
fos = new FileOutputStream("chapter23/src/tempfile3", true);
// Start writing .
byte[] bytes = {97, 98, 99, 100};
// take byte The array is all written out !
fos.write(bytes); // abcd
// take byte Part of the array writes !
fos.write(bytes, 0, 2); // Write again ab
// character string
String s = " I am a Chinese , I'm proud of !!!";
// Converts a string to byte Array .
byte[] bs = s.getBytes();
// Write
fos.write(bs);
// After you've written , Finally, we must refresh
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader Character input stream
package com.bjpowernode.java.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/*
FileReader:
File character input stream , Only plain text can be read .
When reading text content , It's more convenient , quick .
*/
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader = null;
try {
// Create a file character input stream
reader = new FileReader("tempfile");
// Prepare one char Array
char[] chars = new char[4];
// Go to char Read in array
reader.read(chars); // Read in character : for the first time e, The second time f, third time wind ....
for(char c : chars) {
System.out.println(c);
}
/*// Start reading
char[] chars = new char[4]; // Read once 4 Characters
int readCount = 0;
while((readCount = reader.read(chars)) != -1) {
System.out.print(new String(chars,0,readCount));
}*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}FileWriter Character output stream
package com.bjpowernode.java.io;
import java.io.FileWriter;
import java.io.IOException;
/*
FileWriter:
File character output stream . Write .
Only plain text can be output .
*/
public class FileWriterTest {
public static void main(String[] args) {
FileWriter out = null;
try {
// Create a file character output stream object
//out = new FileWriter("file");
out = new FileWriter("file", true);
// Start writing .
char[] chars = {' I ',' yes ',' in ',' countries ',' people '};
out.write(chars);
out.write(chars, 2, 3);
out.write(" I am a java Software engineer !");
// Write a newline character .
out.write("\n");
out.write("hello world!");
// Refresh
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Copy : Use FileInputStream + FileOutputStream Complete the copy of the file .
package com.bjpowernode.java.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
Use FileInputStream + FileOutputStream Complete the copy of the file .
The process of copying should be reading , While writing .
When using the above byte stream to copy files , The file type is arbitrary , Omnipotent . Any kind of file can be copied .
*/
public class Copy01 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// Create an input stream object
fis = new FileInputStream("D:\\course\\02-JavaSE\\video\\chapter01\\ Power nodes -JavaSE- Du Jubin -001- Display of file extension .avi");
// Create an output stream object
fos = new FileOutputStream("C:\\ Power nodes -JavaSE- Du Jubin -001- Display of file extension .avi");
// At the core of : While reading , While writing
byte[] bytes = new byte[1024 * 1024]; // 1MB( Most copies at a time 1MB.)
int readCount = 0;
while((readCount = fis.read(bytes)) != -1) {
fos.write(bytes, 0, readCount);
}
// Refresh , The output stream is finally refreshed
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// Separate try, Not together try.
// Together try When , One of them has an exception , It may affect the closing of another flow .
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}Use FileReader FileWriter If you copy , You can only copy “ Plain text ” file .
package com.bjpowernode.java.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
Use FileReader FileWriter If you copy , You can only copy “ Plain text ” file .
*/
public class Copy02 {
public static void main(String[] args) {
FileReader in = null;
FileWriter out = null;
try {
// read
in = new FileReader("chapter23/src/com/bjpowernode/java/io/Copy02.java");
// Write
out = new FileWriter("Copy02.java");
// Read and write :
char[] chars = new char[1024 * 512]; // 1MB
int readCount = 0;
while((readCount = in.read(chars)) != -1){
out.write(chars, 0, readCount);
}
// Refresh
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
边栏推荐
- After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
- Go从入门到实战——Panic和recover(笔记)
- 行业案例|从零售之王看银行数字化转型的运营之道
- 银河麒麟系统局域网文件共享教程
- 猜拳游戏专题训练
- 划重点!国产电脑上安装字体小技巧
- Go從入門到實戰——接口(筆記)
- Is it safe to open an account and buy stocks? Who knows
- ABC-Teleporter Setting-(思维+最短路)
- GFS distributed file system
猜你喜欢

100 important knowledge points that SQL must master: using functions to process data

mysql使用笔记一

快递e栈——数组篇小型项目

Full record of 2022 open source moment at Huawei partners and Developers Conference

CEPH distributed storage

Go从入门到实战——协程机制(笔记)

Modify large online games through CE modifier

Null pointer exception

白嫖红队goby&POC,叫你如何白嫖?

Here are 12 commonly used function formulas for you. All used ones are good
随机推荐
Prospects for enterprise digitalization (38/100)
OpenSSL 编程 一:基本概念
Very comprehensive dolphin scheduler installation and use documents
有时间看看ognl表达式
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
快速excel导出
Go from introduction to actual combat - all tasks completed (notes)
Modify large online games through CE modifier
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
CORBA 架构体系指南(通用对象请求代理体系架构)
Codeforces Round #721 (Div. 2)
Let Ma Huateng down! Web3.0, hopeless
squid代理服务器
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
Go从入门到实战——协程机制(笔记)
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
Go from introduction to actual combat - context and task cancellation (notes)
创建对象时JVM内存结构
GFS分布式文件系统
GFS distributed file system