读文件和写文件有俩种风格的操作(俩组不同的类)
- 字节流 InputStream(负责读)/ OutputStream (负责写)~~针对二进制文件进行读写,操作基本单位是字节。
- 字符流 Reader(负责读)/ Writer(负责写) ~~针对文本文件进行读写,操作进本单位是字符。
InputStream 概述
方法
说明
InputStream 只是一个抽象类,要使用还需要具体的实现类。关于 InputStream 的实现类有很多,基本可以认为不同的输入设备都可以对应一个 InputStream 类,我们现在只关心从文件中读取,所以使用 FileInputStream
FileInputStream 概述
构造方法
代码示例
示例1
将文件完全读完的两种方式。相比较而言,后一种的 IO 次数更少,性能更好。
第一种:
package file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Test8 {
public static void main(String[] args) {
//try with resources 把要释放的资源,放到try()然后就会自动调用到关闭 无论是否异常
//要求try()里面的对象能够实现Closeable接口
try (InputStream inputStream = new FileInputStream("test/test.txt")) {
int b;
while (true) {
b = inputStream.read();
if (b == -1) {
break;
}
System.out.printf("%c", b);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
第二种
package file;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Test9 {
public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("test/test.txt")) {
int len;
byte[] buf = new byte[1024];
while (true) {
len = inputStream.read(buf);
if (len == -1) {
break;
}
for (int i = 0; i < len; i++) {
System.out.printf("%c", buf[i]);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
示例2
package file;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Test10 {
public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("test/test.txt")) {
int len;
byte[] buffer = new byte[1024];
while (true) {
len = inputStream.read(buffer);
if (len == -1) {
break;
}
String s = new String(buffer, 0, len, "utf-8");
System.out.println(s);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
利用 Scanner 进行字符读取
上述例子中,我们看到了对字符类型直接使用 InputStream 进行读取是非常麻烦且困难的,所以,我们使用一种我们之前比较熟悉的类来完成该工作,就是 Scanner 类。使用scanner相当于使用字符来读。
package file1;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("test/test.txt")) {
try (Scanner scanner = new Scanner(inputStream, "utf-8")) {
while (scanner.hasNext()) {
String s = scanner.next();
System.out.println(s);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
OutputStream 概述
方法
说明
OutputStream 同样只是一个抽象类,要使用还需要具体的实现类。我们现在还是只关心写入文件中,所以使用 FileOutputStream
利用 OutputStreamWriter 进行字符写入
示例1:
package file1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test3 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("test/test.txt")) {
outputStream.write('H');
outputStream.write('e');
outputStream.write('l');
outputStream.write('l');
outputStream.write('o');
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例2:
package file1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test4 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("test/test.txt")) {
byte[] bytes = new byte[]{(byte) 'H', (byte) 'a', (byte) 'H', (byte) 'a'};
outputStream.write(bytes);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例3:
package file1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test5 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("test/test.txt")) {
byte[] bytes = new byte[]{(byte) 'C', (byte) 'H', (byte) 'I', (byte) 'N', (byte) 'A'};
outputStream.write(bytes, 0, 4);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例4:
package file1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test6 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("test/test.txt")) {
String s = "nothing";
byte[] b = s.getBytes();
outputStream.write(b);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例5:
package file1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test7 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("test/test.txt")) {
String s = "百骑劫魏营,功震天下英!";
byte[] b = s.getBytes("utf-8");
outputStream.write(b);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
利用 PrintWriter 找到我们熟悉的方法
上述,我们其实已经完成输出工作,但总是有所不方便,我们接来下将 OutputStream 处理下,使用PrintWriter 类来完成输出,因为PrintWriter 类中提供了我们熟悉的 print/println/printf 方法
示例
package file1;
import java.io.*;
public class Test8 {
public static void main(String[] args) {
try (OutputStream outputStream = new FileOutputStream("test/test.txt")) {
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "utf-8")) {
try (PrintWriter writer = new PrintWriter(outputStreamWriter)) {
writer.println("我是第一行!");
writer.print("我是第二行!\n");
writer.printf("我是第%d行!\n", 3);
writer.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}