一、字节流
字节流可以读取一切文件
1.javaIO流之创建文本、文件夹、多级文件夹
注意:java的IO流很容易报错故障。
解决:捕获:Ctrl + Alt +try/catch
package JavaIo;
import java.io.File;
import java.io.IOException;
public class FileDemon01 {
public static void main(String[] args) {
try {
File file1 = new File("E:\\测试文本.txt");//位置
boolean a = file1.createNewFile();//布尔类型的
System.out.println(a?"创建成功":"创建失败");
File file2 = new File("E:\\测试文件");
boolean b = file2.mkdir();
System.out.println(b?"创建文件成功":"创建文件失败");
File file3 = new File("E:\\测试多级文件夹\\一级\\二级");
boolean c = file3.mkdirs();
System.out.println(c?"创建多级文件夹成功":"创建多级文件夹失败");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.javaIO流之删除文本
package JavaIo;
import java.io.File;
public class FileDemon02 {
public static void main(String[] args) {
File file = new File("E:\\测试文本.txt");
boolean a = file.delete();
System.out.println(a?"删除成功":"删除失败");
}
}
3.文本是否存在
try {
File file = new File("E:/测试文本");
boolean a = file.exists();
if(a)
{
System.out.println("文件已存在!");
}
else
{
System.out.println("文件创建成功");
file.mkdir();
}
} catch (Exception e) {
e.printStackTrace();
}
4.查看文件大小、获取文件名、文件路径
try { //查看文件大小
File file1 = new File("E:/2022.txt");
file1.createNewFile();
Long size = file1.length();
System.out.println("size="+size);
//获取文件名
String filename = file1.getName();
System.out.println("filenamme="+filename);
//获取文件路径
String path = file1.getPath();
System.out.println("path="+path);
} catch (Exception e) {
e.printStackTrace();
}
注意:length()的用法:(一般在类里面都是方法)
String字符串: length()返回字符串的个数 数组:数组名.length 是属性,获取数组中元素的个数
集合:集合对象名.size() 返回集合中元素个数
5.递归文件夹(自己调用自己)
方法:isDirectory():判断文件是否是文件夹 listFiles():查询某个文件夹下的所有文件
package JavaIo;
import java.io.File;
public class JavaDg {
public static void showFile(String pathname)
{
File f1 = new File(pathname);
boolean a = f1.isDirectory();//判断是否为文件夹
if(a)
{//是文件夹
File[] files = f1.listFiles();
for (File tempFile : files)
{
boolean b = tempFile.isDirectory();
if(b)
{
showFile(tempFile.getPath());//递归
}
else
{
String filePath = f1.getPath();
System.out.println("普通文件夹--------"+filePath);
}
}
}
else
{//不是文件夹
String filePath = f1.getPath();
System.out.println("普通文件夹--------"+filePath);
}
}
public static void main(String[] args) {
showFile("E:/汇编语言实验软件");
}
6.IO 流(*):输入输出流:
1)字节流(两者对比)
1.字节输入流
(1)在文件和程序之间铺设管道
try {
//(1)FileInputStream f1 = new FileInputStream("D:\\2022.txt");
(2) File f1 = new File("D:\\2022.txt");
if (f1.exists() && f1.length()>0) {
FileInputStream fis = new FileInputStream(f1);
}//第二种更灵活
} catch (FileNotFoundException e) {
System.out.println("找不到该文件");
}
(2)开、关水龙头(read方法只读一个字节)
FileInputStream f1 = new FileInputStream("D:\\2022.txt");
//开水龙头
int a = f1.read();
int b = f1.read();
int c = f1.read();
System.out.println("a="+(char)a);
System.out.println("b="+(char)b);
System.out.println("c="+(char)c);
//关水龙
f1.close();
//1.在文件之间铺设管道
FileInputStream f1 = new FileInputStream("E:\\2022.txt");
//开水龙头(此方法可以全部输出)
int ch = 0;
while ((ch = f1.read())!= -1)//遍历输出
{
System.out.print((char)ch);
}
//关水龙
f1.close();
2.字节输出流
try {
String data = "everyday";//创建水厂
//铺设管道
FileOutputStream f1 = new FileOutputStream("E:\\2023.txt",true);//加上true如果文本之前便有数据,便会接在后面,不会清除之前的数据
byte ch[] = data.getBytes();//开水龙头,如果文件不存在会自动帮我们生成
f1.write(ch);//如果文件不存在,则会自动帮我们生成
f1.close();//关水龙头
} catch (Exception e) {
e.printStackTrace();
}
IO流复制:
package JavaIo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Filecopy {
public static void main(String[] args) {
try {
Long start = System.currentTimeMillis();//开始时间
FileInputStream fin = new FileInputStream("E:\\2023.txt");//E盘复制到D盘
FileOutputStream fon = new FileOutputStream("D:\\2023.txt");
int in = 0;
while ((in=fin.read()) != -1)//读数据输入
{
fon.write(in);//数据输出
}
fin.close();
fon.close();
Long endt = System.currentTimeMillis();
System.out.println("复制成功");
System.out.println("共花费时间:"+(start-endt));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Buffered管道(更大,更快):
BufferedInputStream 流(*):字节缓冲输入流
注意:但是不能直接写文件地址(要先建细管道再建大管道)
BufferedInputStream bin = new BufferedInputStream("E:\\2022.txt");//错误的
例子:
package JavaIo;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class FileBuffere {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("E:\\2022.txt");
BufferedInputStream bin = new BufferedInputStream(fin);
byte[] car = new byte[1024];
int len = 0;
while ((len = bin.read(car))!=-1)
{
System.out.println(len);
}
bin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedOutputstream:字节缓冲输出流
package JavaIo;
import java.io.*;
public class BuffereText {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("E:\\英语四级\\01.晓艳四级救命写作+翻译.mp4");
BufferedInputStream bin = new BufferedInputStream(fin);
FileOutputStream fon = new FileOutputStream("D:\\英语四级1.mp4");
BufferedOutputStream bou = new BufferedOutputStream(fon);
byte[] car = new byte[1024];//可以改为1024*1024更快
int len = 0;
while ((len=bin.read())!=-1)
{
bou.write(car,0,len);//0是数组下标,从0开始。len是存多少数据
}
bou.close();
bin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、字符流读取纯文本文件比较方便(txt等)帮助我们处理了乱码问题
字符输入流:
字符输出流:
package JavaIo;
import java.io.FileWriter;
import java.io.IOException;
public class Filrrdead {
public static void main(String[] args) {
String str = "java真的很简单";
try {
FileWriter fw = new FileWriter("E:\\javatext.txt");
fw.write(str);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}