一. IO流相关
- File :文件和目录路径名的抽象表示
- 创建文件的方法
- File(String pathname)
- boolean createNewFiles()
public class File2 {
public static void main(String[] args) throws IOException {
// 创建文件对象指向一个虚拟文件夹路径
File file = new File("Day11-Tomcat\\itcast"); // 相对路径
// 在当前项目模块下创建文件夹
boolean mkdir = file.mkdir();
System.out.println(mkdir);
// 创建文件对象指向一个虚拟文件路径
file = new File("Day11-Tomcat\\itcast\\a.txt");
boolean newFile = file.createNewFile();
System.out.println(newFile);
}
}
public class File1 {
public static void main(String[] args) throws IOException {
// 创建文件对象指向一个虚拟路径
File file = new File("Day11-Tomcat\\itcast\\a.txt");
// 删除文件对象
boolean delete = file.delete();
System.out.println(delete);
// 创建文件对象指向一个虚拟文件夹路径
file = new File("Day11-Tomcat\\itcast"); // 相对路径
boolean delete1 = file.delete();
}
}
二. 文件字节流
- IO: 输入输出流 ,用来处理设备间的数据传输问题,比如文件复制,上传和下载
- 分类
- 按照数据的流向:输入输出流
- 按照数据类型:字节流和字符流
- 字节流
- OutputStream :把内存中的数据输出到硬盘,即电脑上的相关文件
- InputStream:把硬盘上的数据加载进入内存
- FileOutputStream 文件字节输出流,将内存中的数据直接输出到一个文件中
- 常用方法
- void write(byte[ ] b)
- void write (byte[ ] b , int off , int len ) : 从指定的字节数组,从索引off开始读,一次读取 len 长度的数据
- 字节流输出数据实现换行:\ n
public class OutputStream1 {
public static void main(String[] args) {
// 创建字节输出流对象
/*
运行字节输出流,做了三件事情,
1. 调用系统功能创建文件
2. 创建了字节输出流对象
3. 让字节输出流对象指向创建好的文件
*/
// 实现数据的追加写入 , 设置第二个参数为true
FileOutputStream fs = null;
try {
fs = new FileOutputStream("Day11-Tomcat\\leah.txt", true);
fs.write("斯人若彩虹".getBytes());
// 创建一个字节数组
byte[] bytes = "abced".getBytes();
//用文件输出流输出字节数组中的数据,从索引0开始,一次输出一个字节数组长度的数据
fs.write(bytes, 0, bytes.length);
// 字节流输出数据实现换行:\n
for (int i = 0; i < 10; i++) {
fs.write("hello".getBytes());
fs.write("\n".getBytes());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流并释放资源
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- FileInputStream:文件字节输入流,把文件中的数据加载进入内存
public class InputStream1 {
public static void main(String[] args) {
FileInputStream fo = null;
try {
// 创建字节输入流对象
fo = new FileInputStream("Day11-Tomcat\\leah.txt");
// 创建一个字节数组,作为存储数据的容器
byte[] bytes = new byte[1024];
// 创建一个变量,用来存储实际读取数据长度的值
int len;
// 调用读取数据的方法,读取数据
while ((len = fo.read(bytes)) != -1) {
// 把数组中的数组转换成字符串输出
System.out.println(new String(bytes, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 使用字节流复制图片
public class File3 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 创建文件输入流和输出流
fis = new FileInputStream("D:\\itcast\\02.jpg");
fos = new FileOutputStream("Day11-Tomcat\\leah");
// 创建一个字节数组,用来装读取的数据,创建变量,装读取数据的总大小
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null && fos != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
三. 缓冲字节流
- BufferedOutputstream:缓冲字节输出流
- BufferedOutputstream:缓冲字节输入流
- 作用:提高读取数据的效率
- 构造方法的参数:普通字节输入输出流
public class BufferedStream {
public static void main(String[] args) throws FileNotFoundException {
// 创建缓冲字节输入输出流,是一种包装流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Day11-Tomcat\\leah.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\itcast\\02.jpg"))
}
}
四. 字符流
-
Reader 抽象类
- InputStreamReader:字符输入流
-
Writer 抽象类
- OutputStreamWriter:字符输出流,可以直接写出一个字符串
-
文件字符流
- FileWriter
- FileReader
public class BufferedStream {
public static void main(String[] args) throws IOException {
// 创建字符输出流输出数据
OutputStreamWriter ow=new OutputStreamWriter(new FileOutputStream("Day11-Tomcat\\leah.txt"));
ow.write("遇上方知有");
ow.flush(); //刷新流
ow.close();
}
}
public class BufferedStream {
public static void main(String[] args) throws IOException {
// 创建文件字符输出流
// 文件字符输出流继承自字符输出流,所以功能基本一样,但是更加简洁
// 如果涉及到字符编码的问题,还是需要用字符流 InputStreamReader/OutputStreamWriter
FileWriter fw=new FileWriter("Day11-Tomcat\\leah.txt") ;
fw.write("斯人若彩虹");
fw.flush();
fw.close();
}
}
五. 字符缓冲流
- BufferedReader (Reader in) : 缓冲字符输入流
- 一行行读取数据:String readLine()
- BudderedWriter (Writer out) :缓冲字符输出流
- 特殊方法换行 :void newLine()
public class File5 {
public static void main(String[] args) throws IOException {
// 创建缓冲输入流
BufferedReader br = new BufferedReader(new FileReader("Day11-Tomcat\\a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("Day11-Tomcat\\leah.txt"));
// 从文件中一行一行读取数据
String line;
while ((line = br.readLine()) != null) {
// 写出数据并换行
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
}