当前位置:网站首页>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 about agricultural futures?
- Firefox set up proxy server
- Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
- How to solve the problem of computer networking but showing no Internet connection
- Code in keil5 -- use the code formatting tool astyle (plug-in)
- Conditional statements of shell programming
- QGIS grid processing DEM data reclassification
- 6.0 kernel driver character driver
- Quick one click batch adding video text watermark and modifying video size simple tutorial
- Farmersworld farmers world, no faith, how to talk about success?
猜你喜欢
6.0 kernel driver character driver
1068. Consolidation of ring stones (ring, interval DP)
Learning notes of raspberry pie 4B - IO communication (SPI)
pivot ROP Emporium
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Awk getting started to proficient series - awk quick start
Kali2021.4a build PWN environment
Electronic tube: Literature Research on basic characteristics of 6j1
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
随机推荐
[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)
540. Single element in ordered array
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
The reason why the computer runs slowly and how to solve it
Mindmanager2022 serial number key decompression installer tutorial
The 2022 global software R & D technology conference was released, and world-class masters such as Turing prize winners attended
Report on the development strategy of China's engineering bidding agency and suggestions for the 14th five year plan Ⓙ 2022 ~ 2028
股票炒股开户注册安全靠谱吗?有没有风险的?
How to solve win10 black screen with only mouse arrow
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
Covariance
Learning notes of raspberry pie 4B - IO communication (SPI)
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Some 5000+ likes, the development notes of a director of cosmic factory, leaked
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)
Quick one click batch adding video text watermark and modifying video size simple tutorial
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers