这是我参与更文挑战的第10天,活动详情查看: 更文挑战
文件流
- 字节流:FileIntputStream、FileOutputStream
- 字符流:FileReader、FileWriter
- 对于文本文件(.txt , .java, .c),使用字符流处理
- 对于非文本文件(.jpg , .mp3, .mp4, .avi, .doc, .ppt),使用字节流处理
文件字节流
输入txt文件
package FileInputOutputTest;
import java.io.*;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis =null;
try {
//1.造文件
File file = new File("hello.txt");
//2.造流
fis = new FileInputStream(file);
//3.读数据
byte[] buffer = new byte[5];
int len;//记录每次读取的字节的个数
while ((len = fis.read(buffer)) != -1) {
String str = new String(buffer, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭资源
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
输出png文件
package FileInputOutputTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputStreamTest2 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos =null;
try {
File srcfile = new File("怪物万岁.png");
File destfile = new File("怪物万岁1.png");
fis = new FileInputStream(srcfile);
fos = new FileOutputStream(destfile);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
System.out.println("复制成功!");
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
文件字符流
- read()的理解:返回读入的一个字符。如果达到问价你的末尾,返回-1
- 异常的处理:为了保证资源一定可以执行关闭操作。需要使用try-catch-finally处理
- 读入的文件一定要存在,否则就会报FileNotFoundException。
输入流
1.方式一
public class FileReaderTest1 {
public static void main(String[] args) {
FileReader fr =null;
try {
/*
将包下的hello.txt文件内容读入程序,并输出到控制台
*/
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt");
//2.提供具体的流
fr = new FileReader(file);
//3.数据读入的过程
//read():返回读入的一个字符。如果达到文件末尾,返回-1
// int data=fr.read();
// while (data != -1) {
// System.out.print((char) data);
// data=fr.read();
// }
//方式二:
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.流的关闭操作
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制代码
2.方式二
package FileReaderWriterTest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest2 {
public static void main(String[] args) {
//对read()操作的升级:使用read的重载方法
FileReader fr=null;
try {
//1.File类的实例化
File file = new File("hello.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次读入的cbuf数组中字符的个数。如果达到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
// while ((len = fr.read(cbuf)) != -1) {
// for (int i = 0; i <len; i++) {
// System.out.print(cbuf[i]);
// }
// }
// System.out.println();
//方式二
while ((len = fr.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.资源的关闭
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
输出流
-
输出操作,对应的File可以不存在。并不会报异常
-
如果不存在,在输出的过程中,会自动创建文件。
如果存在,
如果流使用的构造器是FileWriter(file,false)/FileWriter(file):对原有的文件覆盖
如果流使用的构造器是FileWriter(file,true):对原有文件的基础上追加内容
package FileReaderWriterTest;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReaderWriterTest {
public static void main(String[] args) {
FileWriter fw = null;
FileReader fr = null;
try {
//1.创建File类的对象,指明读入和写出的文件
File srcFile = new File("hello.txt");
File destFile = new File("hello1.txt");
//不能用字符流来处理图片等字节数据
// File srcFile = new File("怪物万岁.png");
// File destFile = new File("怪物万岁1.png");
//2.创建输入流和输出流的对象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
//3.数据的读入和写出
char[] cbuf = new char[5];
int len;//记录每次读入到cbuf数组中的字符个数
while ((len = fr.read(cbuf)) != -1) {
fw.write(cbuf, 0, len);//每次写出len个字符
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭资源
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
复制代码
从内存写出数据到硬盘文件内
package FileReaderWriterTest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) throws IOException {
/*
从内存写出数据到硬盘文件内
*/
//1.提供File文件
File file = new File("hello.txt");
//2.提供FileWriter的对象,用于数据的写出
FileWriter fw = new FileWriter(file);
//3.写出的操作
fw.write("I have a dream!\n");
fw.write("you need have a dream ,too.");
//4.流资源的关闭
fw.close();
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END