当前位置:网站首页>10.file/io stream -bite
10.file/io stream -bite
2022-06-22 08:30:00 【Aeolian u】
File object
Directories are also called folders , A folder is also a file , Specific directories and files can be used File Object to represent .\ Indicates the folder where the layer is located 

Byte stream object and character stream object
Byte stream object :
read :InputStream( abstract class )->FileInputStream( Implementation subclass )
Write :OutputStream( abstract class )->FileOutputStream( Implementation subclass )
FileInputStream:
read Three versions of overloads are provided .
1) Parameterless version : Read one byte at a time . The return value is the byte read : The return value is the corresponding byte ASCII Code value , After reading, go back -1
2) A parameter version : Read several bytes at a time , Put the read result into the array specified in the parameter , The return value is the number of bytes read .
Returns the length of the filled byte array , Read and return -1
3) Three parameter versions : Read several bytes at a time , Put the read result into the array specified in the parameter , The return value is the number of bytes read . Not from the beginning of the array , But from the middle (off The position of this subscript )len Indicates how many elements can be placed at most ( byte )
// In the construction method, you need to specify the path to open the file .
// The path here can be an absolute path , It could be a relative path , It can also be File object
InputStream inputStream = null;
try {
// 1. Create objects , At the same time, it is also opening the file .
inputStream = new FileInputStream("d:/test.txt");
// 2. Try reading one byte at a time , Finish reading the whole document .
while (true) {
int b = inputStream.read();
if (b == -1) {
// Read to the end of the file
break;
}
System.out.println(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. Remember to close the file after reading , Release resources ~
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Another way of writing : from try Automatically call close
try (InputStream inputStream = new FileInputStream("d:/test.txt")) {
while (true) {
int b = inputStream.read();
if (b == -1) {
break;
}
System.out.println(b);
}
} catch (IOException e) {
e.printStackTrace();
}
Read several bytes at a time and put them in the array :
try (InputStream inputStream = new FileInputStream("d:/test.txt")) {
// Read several bytes at a time .
while (true) {
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
if (len == -1) {
// If you return -1 It means that the reading is finished
break;
}
for (int i = 0; i < len; i++) {
System.out.println(buffer[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream: Each time a file is written, the contents of the original file will be cleared , You can add a parameter to the constructor :true, Make it not empty , Then write... After the original file
// Use byte stream , The case of writing documents .
public class Demo9 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("d:/test.txt")) {
// outputStream.write(97);
// outputStream.write(98);
// outputStream.write(99);
byte[] buffer = new byte[]{
97, 98, 99};
outputStream.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Character stream object :
read :Reader->FileReader
// Read and write according to characters
public class Demo10 {
public static void main(String[] args) {
try (Reader reader = new FileReader("d:/test.txt")) {
// Read by character .
while (true) {
char[] buffer = new char[1024];
int len = reader.read(buffer);
if (len == -1) {
break;
}
// for (int i = 0; i < len; i++) {
// System.out.println(buffer[i]);
// }
// If it comes in here Array yes byte Array , You can also manually specify utf8 Character sets avoid garbled code .
String s = new String(buffer, 0, len);
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Write :Writer->FileWriter
// Write in characters
public class Demo11 {
public static void main(String[] args) {
try (Writer writer = new FileWriter("d:/test.txt")) {
writer.write("xyz");
} catch (IOException e) {
e.printStackTrace();
}
}
}
边栏推荐
猜你喜欢
随机推荐
Installation and use of Jupiter notebook
18 中介者模式
Summary of sub database and sub table 1
Find elements that are not common to two arrays
Summary of basic knowledge of Oracle database SQL statement III: data operation language (DQL)
JVM memory overflow
Interpreting the technology group in maker Education
Sqlserver paging
Bee framework, an ORM framework
10.File/IO流-bite
Summary of basic knowledge of Oracle database SQL statement II: data operation language (DML)
Thread.start()方法源码分析
Flask博客实战 - 实现全站导航菜单及首页数据展示
复杂科学在创客教学研究中的应用
Remove the restriction of video memory occupied by tensorflow GPU
Implementation and landing of any to any real-time voice change RTC dev Meetup
How to write high performance SQL statements?
Do not use primitive types in new code during the use of generic types
golang中使用swagger遇到的一些问题
依图在实时音视频中语音处理的挑战丨RTC Dev Meetup









