1. 文件
- 什么是文件?
文件,对我们并不陌生,文件是保存数据的地方,它可以保存任意形式的数据。 - 文件流
文件在程序中是以流的形式来操作的 - 流:数据在数据源和程序之间的路径
- 输入流:数据从数据库(文件)到程序(内存)的路径
- 输出流:数据从程序(内存)到数据库(文件)的路径
2. IO流
- I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理数据的传输。如读/写文件,网络通信等。
- Java出现中,对于数据的输入/输出操作以"流(stream)"的方式进行。
- java.io包下提供了各种"流"类和接口,泳衣获取不同种类的数据,并通过方法输入或输出数据。
- IO流原理
输入input:读取外部数据到程序中。
输出output:将程序数据输出到外部。 - 流的分类
- 按操作数据单位不同分为:字节流(8bit),字符流(按字符)。字符流效率更高,字节流操作二进制文件是无损的。
- 按数据流的流向不同分为:输入流,输出流。
- 按流的角色的不同分为:节点流,处理流(包装流)。
字节流:InputStream(字节输入流)、OutputStream(字节输出流)
字符流:Reader(字符输入流)、Whiter(字符输出流)
它们都是抽象类,不能直接创建对象,只能通过创建它们的实现子类来使用。
- 节点流和处理流
- 节点流可以从一个特点的数据源读写数据,如FileReader、FileWriter
访问文件:FileInputStream、FileOutputStream、FileReader、FileWriter
访问数组:ByteArrayInputStream、ByteArrayOutputStream、ByteArrayReader、ByteArrayWriter
访问管道:PipedInputStream、PipedOutputStream、PipedReader、PipedWriter
访问字符串:StringReader、StringWriter - 处理流(包装流)
"连接"已存在的流(节点流或处理流),为程序提供更为强大的读写功能,如BufferedReader、BufferedWriter
缓冲流:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter
转换流(字符流):InputStreamReader、OutputStreamWrite
对象流(字节流):ObjectInputStream、ObjectOutputStream
抽象基类:FilterInputStream、FilterOutputStream、FilterReader、FilterWriter
打印流:PrintStream、PrintReader
推回输入流:PushbackInputStream、PushbackReader
特殊流(字节流):DataInputStream、DataOutputStream
2.1 FileInputStream
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) {
}
@Test
public void readFile01(){
int readData = 0;
String filePath = "e:\\news1.txt";
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1){ //read()方法读取完毕会返回-1
System.out.print((char) readData);//读取一个字节的数据
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void readFile02(){
byte[] buf = new byte[8];
int lebgth = 0;
String filePath = "e:\\news1.txt";
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
//read()方法读取完毕会返回-1
//读取buf.length()数量字节的数据
//read(buf)返回实际读取的字节数
while ((lebgth = fileInputStream.read(buf)) != -1){
System.out.print(new String(buf,0,lebgth));
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
2.2 FileOutputStream
import org.junit.jupiter.api.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class Demo01 {
public static void main(String[] args) {
}
/*
使用FileOutPutStream将数据写到文件中
如果文件不存在,则创建该文件
*/
@Test
public void writeFile(){
String filePath = "e:/a.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write('Z');
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void writeFile02(){
String filePath = "e:/a.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath);
String str = "Hello,Java!";
fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
//str.getBytes()将字符串转成字节数组
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/*
new FileOutputStream(filePath) 流的创建方式是覆盖,当写入时会覆盖原来的内容
new FileOutputStream(filePath,true) 流的创建方式是添加,当写入时会追加在原来的内容后面
*/
@Test
public void writeFile03(){
String filePath = "e:/a.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath,true);
String str = "Hello,World!";
fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
//str.getBytes()将字符串转成字节数组
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}