当前位置:网站首页>IO flow review
IO flow review
2022-07-03 22:29:00 【Fairy wants carry】
Catalog
BufferedReader And BufferWriter:
Example : Array by array reading :
Copy file ( Similar to file download )
SpringMvc Packaged transferto() Method :
BufferedReader And BufferWriter:
Introduce :
It is conducive to improving the reading and writing efficiency of character stream , Buffer mechanism is introduced ;
BufferedReader And BufferedWriter Each has 8192 A character buffer :—> When BufferedReader When reading external resources , The buffer will be filled with resources first , Then if you call read(), It is also read from the buffer first ;
and BufferedWriter, When we use it to introduce resources or write files , The written data will not be delivered to the destination first , But now in the buffer ;
Example : Array by array reading :
package Demo;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author diao 2022/2/14
*/
public class demo7 {
public static void main(String[] args) throws IOException {
String filePath = null;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
// Judge
if(!reader.ready()){
System.out.println(" The file is temporarily unreadable ");
return;
}
int size=0;
char[] chars = new char[20];
// call Reader Of read Method to read ( Array , The offset , length )
while((size=reader.read(chars,0,chars.length))!=-1){
// An array of reading resources
System.out.println(new String(chars,0,size));
}
reader.close();
}
}
Line by line reading :
package Demo;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author diao 2022/2/14
*/
public class demo7 {
public static void main(String[] args) throws IOException {
String filePath=null;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
if(!reader.ready()){
System.out.println(" The file stream is temporarily unreadable ");
return;
}
int size=0;
String line;
// Read line by line
while((line=reader.readLine())!=null){
System.out.println(line+"\n");
}
reader.close();
}
}
When reading line breaks , You need to add newline symbols yourself ;
BufferedReader Than FileReader Advanced place :BufferedReader In addition to one character reading and one array array reading , It can also be read line by line , And it also has a faster buffer speed ;
The code problem :
Handle : Used packaging InputStreamReader Of BufferedReader To read the file ;
package Demo;
import java.io.*;
/**
* @author diao 2022/2/14
*/
public class demo8 {
public static void main(String[] args) throws IOException {
String file=null;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
char[] chars = new char[20];
String line;// That's ok
int size;
while((size=reader.read(chars,0,chars.length))!=-1){
System.out.println(new String(chars,0,chars.length));
}
}
}
Copy file ( Similar to file download )
Two steps : First, read external resources ( It can be understood as on an external hard disk ), Then put the external resources into memory at this time , Finally, transfer the resource to the local hard disk and save it ;

some demo:
Read one by one , Reading while writing —> Copy file
static void copyByChar(String srcFile, String destFile) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(srcFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));
int ch=0;
// Reading while writing
// Read a character
while ((ch = reader.read()) != -1)
{
// Write a character
writer.write(ch);
}
reader.close();
writer.close();
}Array form —> Read as much as you write
static void copyByCharArray(String srcFile, String destFile) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(srcFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));
int size=0;
char[] cbuf=new char[20];
// Read an array of characters
while ((size = reader.read(cbuf)) != -1)
{
// How much to read and how much to write
writer.write(cbuf,0,size);
}
reader.close();
writer.close();
}Copy... By line :
static void copyByLine(String srcFile,String destFile) throws IOException
{
BufferedReader reader=new BufferedReader(new FileReader(srcFile));
BufferedWriter writer=new BufferedWriter(new FileWriter(destFile));
String line;
//BufferedReader When reading a line, the returned string does not include line breaks
// If there is a line of characters, the string of that line is returned , Return on no null
while((line=reader.readLine())!=null)
{
writer.write(line);
writer.newLine();// Write line breaks
}
reader.close();
writer.close();
}test :
public static void main(String[] args) throws IOException
{
String from = "gbk.txt";
String to = "gbk_copy.txt";
String to1 = "gbk_copy1.txt";
String to2 = "gbk_copy2.txt";
copyByChar(from, to);
copyByCharArray(from, to1);
copyByLine(from, to2);
}Line break problem :
Add a first line to judge : Write in the first line if you have resources , Write the line separator without ;
static void copyByLine(String srcFile,String destFile) throws IOException
{
BufferedReader reader=new BufferedReader(new FileReader(srcFile));
BufferedWriter writer=new BufferedWriter(new FileWriter(destFile));
String line;
//BufferedReader When reading a line, the returned string does not include line breaks
// If there is a line of characters, the string of that line is returned , Return on no null
boolean flag=false;
while((line=reader.readLine())!=null)
{
if(!flag)
{
flag=true;
writer.write(line);
}
else
{
writer.newLine();// Write line breaks
writer.write(line);
}
}
reader.close();
writer.close();
}SpringMvc Packaged transferto() Method :
One time direct reading
// To upload pictures
//1 Image storage path
String pic_path="";
//2 old name
String originalFilename = items_pic.getOriginalFilename();
//3 New name (uuid Random number plus suffix )
String newfileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
// New pictures
File newfile=new File(pic_path+newfileName);
// Write memory pictures to disk
items_pic.transferTo(newfile);
// Write the new picture into the object , Easy to update in the database
itemsCustom.setPic(newfileName);
// call service Update product information , The page needs to pass the product information to this method
itemsService.updateItems(id, itemsCustom);边栏推荐
- How to solve win10 black screen with only mouse arrow
- Why should enterprises do more application activities?
- 2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
- [automation operation and maintenance novice village] flask-2 certification
- What are the common computer problems and solutions
- JS closure knowledge points essence
- [actual combat record] record the whole process of the server being attacked (redis vulnerability)
- Blue Bridge Cup Guoxin Changtian single chip microcomputer -- led lamp module (V)
- Pointer concept & character pointer & pointer array yyds dry inventory
- Shell script three swordsman awk
猜你喜欢

Overview of Yunxi database executor
![[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list](/img/db/b992d2b461ca17652518a1511b4947.gif)
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list

Buuctf, misc: sniffed traffic

string

Learning notes of raspberry pie 4B - IO communication (SPI)

Pooling idea: string constant pool, thread pool, database connection pool

Quick one click batch adding video text watermark and modifying video size simple tutorial

How can enterprises and developers take advantage of the explosion of cloud native landing?

Go Technology Daily (2022-02-13) - Summary of experience in database storage selection

320. Energy Necklace (ring, interval DP)
随机推荐
Redis single thread and multi thread
How about agricultural futures?
Development mode and Prospect of China's IT training industry strategic planning trend report Ⓣ 2022 ~ 2028
This time, thoroughly understand bidirectional data binding 01
Flutter internationalized Intl
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
Programming language (2)
C deep anatomy - the concept of keywords and variables # dry inventory #
The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention
The 2022 global software R & D technology conference was released, and world-class masters such as Turing prize winners attended
Covariance
How to restore the factory settings of HP computer
BUUCTF,Misc:LSB
How the computer flushes the local DNS cache
Electronic tube: Literature Research on basic characteristics of 6j1
IPhone development swift foundation 08 encryption and security
Kali2021.4a build PWN environment
4 environment construction -standalone ha
pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
Sow of PMP