1.文件创建三种方式
package com.li.springboot.text;
import java.io.File;
import java.io.IOException;
public class Text {
public static void main(String[] args) throws IOException {
//方式一
String filePath1 = "e:\\oop.txt";
File file1 = new File(filePath1);
file1.createNewFile();
//方式二
String filePath2 = "e:\\a.txt";
File file2 = new File("e:", "a.txt");
file2.createNewFile();
//方式三
String filePath3 = "e:";
File ParentFile = new File(filePath3);
File file = new File(ParentFile, "b.txt");
file.createNewFile();
}
}
2.文件目录创建
package com.li.springboot.text;
import java.io.File;
import java.io.IOException;
public class Text {
public static void main(String[] args) throws IOException {
//创建文件目录
String filePath1 = "e:\\oop\\01";
String filePath2 = "e:\\01";
File file1 = new File(filePath1);
File file2 = new File(filePath2);
file1.mkdirs();//多级目录
file2.mkdir();//一级目录
}
}
3.字节流
FileInputStream 和 FileOutputStream
package com.li.springboot.text;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Text {
public static void main(String[] args) throws IOException {
String fileInPath = "e:\\oop.txt";
String fileOutPath ="e:\\ioc.txt";
FileInputStream fileInputStream = new FileInputStream(fileInPath);
FileOutputStream fileOutputStream = new FileOutputStream(fileOutPath);
byte[] bytes = new byte[1024];
int read;
while ((read = fileInputStream.read(bytes)) != -1){
fileOutputStream.write(bytes,0,read);
}
if(fileInputStream != null){
fileInputStream.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
}
}
4.字符流
FileReader 和 FileWriter
package com.li.springboot.text;
import java.io.*;
public class Text {
public static void main(String[] args) throws IOException {
String fileInPath = "e:\\oop.txt";
String fileOutPath ="e:\\ioc.txt";
FileReader fileReader = new FileReader(fileInPath);
FileWriter fileWriter = new FileWriter(fileOutPath);
char[] chars = new char[10];
int read;
while((read = fileReader.read(chars)) != -1){
fileWriter.write(chars,0,read);
}
if(fileReader != null){
fileReader.close();
}
if(fileWriter != null){
fileWriter.close();
}
}
}
5.包装流
字符包装流
package com.li.springboot.text;
import java.io.*;
public class Text {
public static void main(String[] args) throws IOException {
String fileInPath = "e:\\oop.txt";
String fileOutPath ="e:\\ioc.txt";
FileReader fileReader = new FileReader(fileInPath);
FileWriter fileWriter = new FileWriter(fileOutPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String readLine;
while ((readLine = bufferedReader.readLine()) != null){
bufferedWriter.write(readLine);
bufferedWriter.newLine();
}
if(bufferedReader != null){
bufferedReader.close();
}
if(bufferedWriter != null){
bufferedWriter.close();
}
}
}
字节包装流
package com.li.springboot.text;
import java.io.*;
public class Text {
public static void main(String[] args) throws IOException {
String fileInPath = "e:\\oop.txt";
String fileOutPath ="e:\\ioc.txt";
FileInputStream fileInputStream = new FileInputStream(fileInPath);
FileOutputStream fileOutputStream = new FileOutputStream(fileOutPath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] bytes = new byte[1024];
int read;
while ((read = bufferedInputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes,0,read);
}
if(bufferedInputStream != null){
bufferedInputStream.close();
}
if(bufferedOutputStream != null){
bufferedOutputStream.close();
}
}
}
6.对象处理流