当前位置:网站首页>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);
边栏推荐
- The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
- In 2022, 6G development has indeed warmed up
- pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
- 2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination
- How to restore the factory settings of HP computer
- Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
- Summary of fluent systemchrome
- Druids connect to mysql8.0.11
- 2022 electrician (elementary) examination questions and electrician (elementary) registration examination
- [SRS] build a specified version of SRS
猜你喜欢
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
Code in keil5 -- use the code formatting tool astyle (plug-in)
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
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
How to solve win10 black screen with only mouse arrow
This time, thoroughly understand bidirectional data binding 01
1 Introduction to spark Foundation
随机推荐
Electronic tube: Literature Research on basic characteristics of 6j1
2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination
4 environment construction -standalone ha
The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
Code in keil5 -- use the code formatting tool astyle (plug-in)
On my first day at work, this API timeout optimization put me down!
6.2 normalization 6.2.5 third normal form (3NF)
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
QGIS grid processing DEM data reclassification
How to connect a laptop to a projector
Es6~es12 knowledge sorting and summary
webAssembly
Leetcode: a single element in an ordered array
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
Development trend and market demand analysis report of China's energy storage battery industry Ⓩ 2022 ~ 2028
Pat grade A - 1164 good in C (20 points)
Buuctf, web:[geek challenge 2019] buyflag
Oil monkey plug-in