当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
- Here are 12 commonly used function formulas for you. All used ones are good
- 豆沙绿保护你的双眼
- mysql使用笔记一
- 互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
- 数组作业题
- GFS distributed file system
- Go从入门到实战——所有任务完成(笔记)
- Go from introduction to actual combat - context and task cancellation (notes)
- OpenSSL 编程 二:搭建 CA
- 系统自带的karsonzhang/fastadmin-addons报错
猜你喜欢

win11桌面出现“了解此图片”如何删除

Kirin V10 installation font

Icml2022 | scalable depth Gaussian Markov random field

非常全面的DolphinScheduler(海豚调度)安装使用文档

Let Ma Huateng down! Web3.0, hopeless

Go从入门到实战——Panic和recover(笔记)

Very comprehensive dolphin scheduler installation and use documents

Go从入门到实战——错误机制(笔记)

Go从入门到实战——共享内存并发机制(笔记)

Go from introduction to actual combat -- channel closing and broadcasting (notes)
随机推荐
Go from introduction to practice -- coordination mechanism (note)
Flask----应用案例
Go from entry to practice -- CSP concurrency mechanism (note)
让马化腾失望了!Web3.0,毫无希望
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
GFS distributed file system
TreeSet详解
Acwing周赛57-数字操作-(思维+分解质因数)
OpenSSL 编程 一:基本概念
squid代理服務器
MySQL client tools are recommended. I can't imagine that it is best to use Juran
uniapp拦截请求
跟我一起AQS SOS AQS
100 important knowledge points that SQL must master: retrieving data
SQL Server for循环用法
Icml2022 | scalable depth Gaussian Markov random field
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Acwing周赛57-最长连续子序列-(二分or树状数组)
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler