参考:https://blog.csdn.net/mu_wind/article/details/108674284
文本io有:字节io和字符io
详情参考:io流
https://blog.csdn.net/qq_43470725/article/details/121283862
下面直接代码演示:
1、字节流读写操作文件:
package com.example.io.fileIo.byteStreamFile;
import java.io.*;
public class ByteStreamFileTest01 {
public static void main(String[] args) {
ByteStreamFileTest01 byteStreamFileTest01 = new ByteStreamFileTest01();
String path = byteStreamFileTest01.getPath();
path = getPath01(path);
System.out.println(path);
// 创建文件夹和文件并返回File:
File file = createFile(path);
// 向文件中写入值
try {
write(file);
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读值
try {
System.out.println(read01(file));
} catch (IOException e) {
e.printStackTrace();
}
}
// 得到该类的路径
public String getPath(){
String path = this.getClass().getResource("").getPath();
return path;
}
public static String getPath01(String pathIn){
pathIn = pathIn.substring(1,pathIn.length());
pathIn = pathIn.replace("/", "\\");
System.out.println("pathIn==="+pathIn);
return pathIn;
}
public static File createFile(String path){
// 创建文件夹
path = path + "test";
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
// 创建文件
path = path + "\\test.txt";
System.out.println("path---last===="+path);
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public static void write(File file) throws IOException {
// 创建文件输出流以写入由指定的File对象表示的文件。 如果第二个参数是true ,
// 则字节将被写入文件的末尾而不是开头。 创建一个新的FileDescriptor对象来表示此文件连接。
OutputStream os = new FileOutputStream(file,true);
// 要写入的字符串
String str = "云想衣裳花想容,春风拂槛露华浓。\n" +
"若非群玉山头见,会向瑶台月下逢。520";
// 写入文件
os.write(str.getBytes());
// 关闭流
os.close();
}
public static String read01(File file) throws IOException {
InputStream in = new FileInputStream(file);
// 一次性取多少个字节
byte[] bytes = new byte[1024];
// 用来接收读取的字节数组
StringBuffer sb = new StringBuffer();
// 读取到的数组的长度,为-1时表示没有数据
int length = 0;
// 循环读取数据
while ((length = in.read(bytes))!= -1){
// 将读取的内容转换成字符
sb.append(new String(bytes,0,length));
}
in.close();
return sb.toString();
}
// 不适合读中文:
public static String read02(File file) throws IOException {
InputStream in = new FileInputStream(file);
StringBuffer sb = new StringBuffer();
int temp = 0;
// 循环读取数据
while ((temp = in.read())!= -1){
// 将读取的内容转换成字符
char c = (char) temp;
System.out.println(c);
sb.append(c);
}
in.close();
return sb.toString();
}
}
2、带缓冲流的字节流读写处理文件:
package com.example.io.fileIo.byteStreamFile;
import java.io.*;
public class BufferedStreamFileTest02 {
public static void main(String[] args) {
BufferedStreamFileTest02 bufferedStreamFileTest02 = new BufferedStreamFileTest02();
String path = bufferedStreamFileTest02.getPath();
path = getPath01(path);
System.out.println(path);
// 创建文件夹和文件并返回File:
File file = createFile(path);
// 向文件中写入值
try {
write(file);
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读值
try {
System.out.println(read01(file));
} catch (IOException e) {
e.printStackTrace();
}
}
// 得到该类的路径
public String getPath(){
String path = this.getClass().getResource("").getPath();
return path;
}
public static String getPath01(String pathIn){
pathIn = pathIn.substring(1,pathIn.length());
pathIn = pathIn.replace("/", "\\");
System.out.println("pathIn==="+pathIn);
return pathIn;
}
public static File createFile(String path){
// 创建文件夹
path = path + "test";
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
// 创建文件
path = path + "\\test.txt";
System.out.println("path---last===="+path);
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public static void write(File file) throws IOException {
// 缓冲字节流,提高效率
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file,true));
// 要写入的字符串
String str = "云想衣裳花想容,春风拂槛露华浓。\n" +
"若非群玉山头见,会向瑶台月下逢。520";
// 写入文件
bos.write(str.getBytes());
// 关闭流
bos.close();
}
public static String read01(File file) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
// 一次性取多少个字节
byte[] bytes = new byte[1024];
// s用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 读取到的字节数组长度,为-1时表示没有数据
int length = 0;
// 循环读取数字
while ((length = bis.read(bytes)) != -1){
// 将读取的内容转化为字符串
sb.append(new String(bytes,0,length));
}
// 关闭流
bis.close();
return sb.toString();
}
}
3、字符流处理文件读写
package com.example.io.fileIo.charStreamFile;
import java.io.*;
public class CharStreamFileTest01 {
public static void main(String[] args) {
CharStreamFileTest01 charStreamFileTest01 = new CharStreamFileTest01();
String path = charStreamFileTest01.getPath();
path = getPath01(path);
System.out.println(path);
// 创建文件夹和文件并返回File:
File file = createFile(path);
// 向文件中写入值
try {
write(file);
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读值
try {
System.out.println(read01(file));
} catch (IOException e) {
e.printStackTrace();
}
}
// 得到该类的路径
public String getPath(){
String path = this.getClass().getResource("").getPath();
return path;
}
public static String getPath01(String pathIn){
pathIn = pathIn.substring(1,pathIn.length());
pathIn = pathIn.replace("/", "\\");
System.out.println("pathIn==="+pathIn);
return pathIn;
}
public static File createFile(String path){
// 创建文件夹
path = path + "test";
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
// 创建文件
path = path + "\\test.txt";
System.out.println("path---last===="+path);
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public static void write(File file) throws IOException {
// OutputStreamWriter可以显示指定字符集,否则使用默认字符集
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file,true),"UTF-8");
// 要写入的字符串
String str = "云想衣裳花想容,春风拂槛露华浓。\n" +
"若非群玉山头见,会向瑶台月下逢。520";
osw.write(str);
osw.close();
}
public static String read01(File file) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"UTF-8");
// 字符数组:一次取多少个字符
char[] chars = new char[1024];
// 每次读取的字符数组先append到StringBuilder中
StringBuilder sb = new StringBuilder();
// 读取到字符数组长度,为-1时表示没有数据
int length;
// 循环取数据
while ((length = isr.read(chars)) != -1){
// 将读取的内容转换成字符串
sb.append(chars,0,length);
}
// 关闭流
isr.close();
return sb.toString();
}
}
4、字符流便捷操作文件读写:
package com.example.io.fileIo.charStreamFile;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharFastStreamFileTest02 {
public static void main(String[] args) {
CharFastStreamFileTest02 charFastStreamFileTest02 = new CharFastStreamFileTest02();
String path = charFastStreamFileTest02.getPath();
path = getPath01(path);
System.out.println(path);
// 创建文件夹和文件并返回File:
File file = createFile(path);
// 向文件中写入值
try {
write(file);
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读值
try {
System.out.println(read01(file));
} catch (IOException e) {
e.printStackTrace();
}
}
// 得到该类的路径
public String getPath(){
String path = this.getClass().getResource("").getPath();
return path;
}
public static String getPath01(String pathIn){
pathIn = pathIn.substring(1,pathIn.length());
pathIn = pathIn.replace("/", "\\");
System.out.println("pathIn==="+pathIn);
return pathIn;
}
public static File createFile(String path){
// 创建文件夹
path = path + "test";
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
// 创建文件
path = path + "\\test.txt";
System.out.println("path---last===="+path);
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public static void write(File file) throws IOException {
FileWriter fw = new FileWriter(file,true);
// 要写入的字符串
String str = "云想衣裳花想容,春风拂槛露华浓。\n" +
"若非群玉山头见,会向瑶台月下逢。520";
fw.write(str);
fw.close();
}
public static String read01(File file) throws IOException {
FileReader fr = new FileReader(file);
// 一次性取多少个字符
char[] chars = new char[1024];
// 用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 读取到的字节数组长度,为-1时表示没有数据
int length;
// 循环取数据
while ((length = fr.read(chars)) != -1){
// 将读取的内容转换成字符串
sb.append(chars,0,length);
}
// 关闭流
fr.close();
return sb.toString();
}
}
5、字符缓冲流操作文件读写
package com.example.io.fileIo.charStreamFile;
import java.io.*;
public class BufferCharStreamFileTest03 {
public static void main(String[] args) {
BufferCharStreamFileTest03 bufferCharStreamFileTest03 = new BufferCharStreamFileTest03();
String path = bufferCharStreamFileTest03.getPath();
path = getPath01(path);
System.out.println(path);
// 创建文件夹和文件并返回File:
File file = createFile(path);
// 向文件中写入值
try {
write(file);
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读值
try {
System.out.println(read01(file));
} catch (IOException e) {
e.printStackTrace();
}
}
// 得到该类的路径
public String getPath(){
String path = this.getClass().getResource("").getPath();
return path;
}
public static String getPath01(String pathIn){
pathIn = pathIn.substring(1,pathIn.length());
pathIn = pathIn.replace("/", "\\");
System.out.println("pathIn==="+pathIn);
return pathIn;
}
public static File createFile(String path){
// 创建文件夹
path = path + "test";
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
// 创建文件
path = path + "\\test.txt";
System.out.println("path---last===="+path);
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public static void write(File file) throws IOException {
// BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(file, true), "UTF-8"));
// FileWriter可以大幅度简化代码
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
// 要写入的字符串
String str = "云想衣裳花想容,春风拂槛露华浓。\n" +
"若非群玉山头见,会向瑶台月下逢。520";
bw.write(str);
bw.close();
}
public static String read01(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
// 用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 按行读取数据
String line;
// 循环读取数据
while ((line = br.readLine()) != null){
// 将读取的内容转换成字符串
sb.append(line);
}
// 关闭流
br.lines();
return sb.toString();
}
}