- 按照操作数据单位分类:字节流(二进制文件)、字符流(文本文件)
- 按照数据流的流向不同:输入流、输出流
- 按照流的角色不同:节点流、处理流/包装流
抽象基类 |
字节流 |
字符流 |
输入流 |
InputStream |
Reader |
输出流 |
OutputStream |
Writer |
- 输入流:数据从数据源(文件)到程序(内存)之间的路径
- 输出流:数据从程序(内存)到数据源(文件)之间的路径
public class CreateFile {
public static void main(String[] args) {
String filePath = "C:\\Users\\16671\\Desktop\\new01.md";
String parentPath = "C:\\Users\\16671\\Desktop\\";
String child1 = "new02.md";
String child2 = "new03.md";
CreateFile createFile = new CreateFile();
createFile.create01(filePath);
File parentFile = new File(parentPath);
createFile.create02(parentFile, child1);
createFile.create03(parentPath, child2);
}
public void create01(String filePath){
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("Success 01");
} catch (IOException e) {
e.printStackTrace();
}
}
public void create02(File parent, String child){
File file = new File(parent, child);
try {
file.createNewFile();
System.out.println("Success 02");
} catch (IOException e) {
e.printStackTrace();
}
}
public void create03(String parentPath, String childPath){
File file = new File(parentPath, childPath);
try {
file.createNewFile();
System.out.println("Success 03");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class FileInfo {
public static void main(String[] args) {
String filePath = "C:\\Users\\16671\\Desktop\\new03.md";
File file = new File(filePath);
System.out.println("getAbsolutePath : " + file.getAbsolutePath());
System.out.println("exists : " + file.exists());
System.out.println("getName : " + file.getName());
System.out.println("getParent : " + file.getParent());
System.out.println("getPath : " + file.getPath());
System.out.println("isFile : " + file.isFile());
System.out.println("isDictionary : " + file.isDirectory());
System.out.println("fileLength : " + file.length());
}
}
public class DictionaryControl {
public static void main(String[] args) {
String filePath = "C:\\Users\\16671\\Desktop\\new01.md";
String path1 = "C:\\Users\\16671\\Desktop\\dir";
String path2 = "C:\\Users\\16671\\Desktop\\dir2\\demo\\a";
DictionaryControl dictionaryControl = new DictionaryControl();
dictionaryControl.dir01(filePath);
dictionaryControl.dir02(path1);
dictionaryControl.dir03(path2);
}
public void dir01(String filePath){
File file = new File(filePath);
if (file.exists()){
if (file.delete())
System.out.println("删除成功");
else System.out.println("删除失败");
}else {
System.out.println("文件不存在");
}
}
public void dir02(String path){
File file = new File(path);
if (file.exists()){
if (file.delete())
System.out.println("删除成功");
else System.out.println("删除失败");
}else {
System.out.println("目录不存在");
}
}
public void dir03(String path){
File file = new File(path);
if (file.exists()){
System.out.println("目录已经存在");
}else{
if (file.mkdirs())
System.out.println("目录创建成功");
else System.out.println("目录创建失败");
}
}
}

public class InStreamInfo {
@Test
public void readFile01(){
String filePath = "e:\\TestFile\\hello.txt";
int readData = 0;
try {
FileInputStream fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1){
System.out.print((char)readData);
}
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class InStreamInfo {
@Test
public void readFile02(){
String filePath = "e:\\TestFile\\hello.txt";
int readLen = 0;
byte[] buf = new byte[8];
try {
FileInputStream fileInputStream = new FileInputStream(filePath);
while ((readLen = fileInputStream.read(buf)) != -1){
System.out.print(new String(buf, 0, readLen));
}
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public class OutStreamInfo {
@Test
public void writeFile(){
String filePath = "e:\\TestFile\\hello.txt";
try {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
String originStr = "Hello, Java!";
fileOutputStream.write('H');
fileOutputStream.write(originStr.getBytes());
fileOutputStream.write(originStr.getBytes(), 0, 5);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!