FileInputStream及FileOutputStream
FileInputStream
类的继承关系
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/f378ec96317d4df0b896fe36d030ff22.png)
方法摘要
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/21753b67c7b8498ca20e34ab3de2c3a9.png)
演示FileIputStream的使用(字节输入流 文件–>程序)
package com.cyr.IOStream.inputStream;
import org.junit.Test;
import java.io.IOException;
import java.io.FileInputStream;
public class FileInputStream_ {
@Test
public void readFile01(){
String filePath = "e:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1){
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02(){
String filePath = "e:\\hello.txt";
int readData = 0;
byte[] buf = new byte[8];
int readLen = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readLen = fileInputStream.read(buf)) != -1){
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream
类的继承关系
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e890da9cf4e1452096d1b8c9c881c54b.png)
方法摘要
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/8d7bb2c9ff97476c867acdae99b8c6c1.png)
演示FileOutputStream将数据写到文件中
package com.cyr.IOStream.outputStream;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStream01 {
@Test
public void writeFile(){
String filePath = "e:\\a.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath);
String str = "hello,world";
fileOutputStream.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}