提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
File流以及输入输出流(IO)的使用
一、将字符串内容写到本地(针对.txt文件)
1.FileWriter写到本地
public class FileWrite {
public static void main(String[] args) {
String str = "hello world!"; //要写的内容
FileWriter writer;
for (int i = 0; i < 3; i++) {
try {
String filename = "D:\\" + i + "abc.txt"; //写到本地的文件位置
writer = new FileWriter(filename);
writer.write(str); //开始写
writer.flush(); //刷新
writer.close(); //关流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.FileWriter写到本地,在原有文件上追加写
public class FileWrite2 {
public static void main(String[] args) {
String str = "hello world!";
FileWriter writer;
for (int i = 0; i < 3; i++) {
try {
writer = new FileWriter("D:\\token.txt", true); //true代表在原文件上追加写
writer.write(str);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.FileOutputStream写文件到本地
public class FileOutputStream01 {
public static void main(String[] args) throws IOException {
String str = "nihao";
File txt = new File("D:/log1.txt");
if (!txt.exists()) {
txt.createNewFile();
}
byte bytes[] = new byte[512]; //缓冲池
bytes = str.getBytes();
int b = bytes.length; //是字节的长度,不是字符串的长度
FileOutputStream fos = new FileOutputStream(txt);
fos.write(bytes, 0, b);
fos.close();
}
}
4.FileReader读取本地文件内容并打印
public class FileWrite4 {
public static void main(String[] args) throws IOException {
File file = new File("D:\\log1.txt");
FileReader fileReader = new FileReader(file);
// 创建一个BufferedReader对象,用于按行读取文件内容
BufferedReader reader = new BufferedReader(fileReader);
String line;
// 逐行读取文件内容并打印
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}
5.FileInputStream读取本地文件内容并打印
public class fileInputStream02 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\log1.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
fileInputStream.close();
}
}
6.自动创建文件的上层文件夹
String content = ”hello“;
File file = new File("D:\\temp\\test\\hello.txt");
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs(); //创建文件的父级文件夹
}
file.createNewFile();
byte bytes[] = new byte[512]; //缓冲池
bytes = content .getBytes();
int b = bytes.length; //是字节的长度,不是字符串的长度
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes, 0, b);
fos.close(); //关流
文章借鉴链接:添加链接描述
二、使用输入输出流(fileInputStream /fileOutputStream )读取和写入文件(可实现文字,图片等)
public class IOFile {
public static void main(String[] args) throws IOException {
//读取文件
FileInputStream fileInputStream = new FileInputStream("D:\\abc.png");
//创建缓冲流
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//写入文件
FileOutputStream fileOutputStream = new FileOutputStream("D:\\abc2.png");
//创建缓冲流
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//创建缓存区,装输入流文件
byte[] buff = new byte[512];
int len = 0;
while ((len = bufferedInputStream.read(buff)) != -1) {
fileOutputStream.write(buff, 0, len);
}
//关流
bufferedOutputStream.close();
fileOutputStream.close();
bufferedInputStream.close();
fileInputStream.close();
}
}
三、使用FileUtils工具实现文件读写(目前仅针对.txt文件)
@org.junit.Test
public void readAndWriteFile() throws IOException {
// 写,append:true表示在文件尾部追加
FileUtils.writeStringToFile(new File("C:\\Users\\lxb\\Desktop\\test.txt"),"utf-8","utf-8",true);
//读
String file = FileUtils.readFileToString(new File("C:\\Users\\lxb\\Desktop\\test.txt"), "utf-8");
System.out.println(file);
}