P587 IO流-FileReader读入数据的基本操作
package com.io;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderWriterTest {
public static void main(String[] args){
File file = new File("hello.txt"); //相较于当前工程
System.out.println(file.getAbsolutePath());
}
@Test
public void testFileReader(){
FileReader fr = null;
try {
// 1. 实例化File类对象
File file = new File("hello.txt"); //相较于当前Module
// 2. 提供具体的流
fr = new FileReader(file);
// 3.数据的读入
// int data = fr.read();
// while (data != -1){
// System.out.print((char) data);
// data = fr.read();
// }
int data;
while((data = fr.read()) != -1){
System.out.println((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.流的关闭操作
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
P588 IO流-FileReader中使用Read的重载方法读入数据
// 对read操作升级,使用read的重载方法
@Test
public void testFileReader1() throws IOException {
FileReader fr = null;
try {
// 类的实例化
File file = new File("hello.txt");
// 流的实例化
fr = new FileReader(file);
// 读入操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
// for (int i = 0; i < len; i++) {
// System.out.println(cbuf[i]);
// }
// String str = new String(cbuf);
// System.out.println(str);
String str = new String(cbuf, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null){
// 资源的关闭
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
P589 IO流-FileWriter写出数据的操作
@Test
public void testFileWriter() throws IOException {
// 1. File类对象
File file = new File("hello1.txt");
// 2. 提供FileWriter对象,用户数据写出
FileWriter fw = new FileWriter(file, false);
// 3. 写出操作
fw.write("I have a dreamn");
fw.write("U have a dream");
// 4. 流的关闭
fw.close();
}
P590 IO流-使用FileReader和FileWriter实现文本文件的复制
@Test
public void testFileWriter() throws IOException {
FileWriter fw = null;
try {
// 1. File类对象
File file = new File("hello1.txt");
// 2. 提供FileWriter对象,用户数据写出
fw = new FileWriter(file, false);
// 3. 写出操作
fw.write("I have a dreamn");
fw.write("U have a dream");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null){
try {
// 4. 流的关闭
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileReaderFileWriter(){
FileReader fr = null;
FileWriter fw = null;
try {
// 创建File类对象 指明读入和写出文件
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
// 创建输入流和输出流的对象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
// 数据的读入和写出操作
char[] cbuf = new char[5];
int len;// 记录每次读入到数组的字符的个数
while ((len = fr.read(cbuf)) != -1){
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流资源
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
P591 IO流-字符流不能处理图片文件的测试
P592 IO流-使用FileInputStream不能读取文本文件的测试
@Test
public void testFileInoutStream() throws IOException {
FileInputStream fis = null;
try {
// 文件
File file = new File("hello.txt");
// 造流
fis = new FileInputStream(file);
// 读数据
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 {
if (fis != null){
fis.close();
}
}
}
P593 IO流-使用FileInputStream和FileOutputStream读写非文本文件
@Test
public void testFileInputOutputStream(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File("XX.jpg");
File destFile = new File("XX2.jpg");
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);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
P594 IO流-使用FileInputStream和FileOutputStream复制文件的方法测试
// 指定路径下文件的复制
public void copyFile(String srcPath, String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
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);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String srcPath = "C:\Users\hu\Desktop\计划.docx";
String destPath = "C:\Users\hu\Desktop\plan.docx";
copyFile(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("时间为:" + (end - start));
}
P595 IO流-缓冲流实现非文本文件的复制
// 缓冲流是提高文件的读写效率的
public class BufferedTest {
@Test
public void BufferedStreamTest(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcFile = new File("XX.jpg");
File destFile = new File("XXX.jpg");
// 2.1 造流
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
// 2.2 造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 3 读取和写入
byte[] buffer = new byte[5];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4 资源关闭
// 要求:先关闭外层流、再关闭内层流
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭外层流的同时,内层流会自动关闭
// fos.close();
// fis.close();
}
P596 IO流-缓冲流与节点流读写速度对比
// 实现文件复制的方法
public void copyFileWithBuffered(String srcPath, String destPath){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
// 2.1 造流
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
// 2.2 造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 3 读取和写入
byte[] buffer = new byte[5];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4 资源关闭
// 要求:先关闭外层流、再关闭内层流
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testCopyFileWithBuffered(){
long start = System.currentTimeMillis();
String srcPath = "C:\Users\hu\Desktop\计划.docx";
String destPath = "C:\Users\hu\Desktop\plan.docx";
copyFileWithBuffered(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("时间为:" + (end - start));
}
P597 IO流-缓冲流实现文本文件的复制
@Test
public void testBufferedReaderBufferedWriter(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("XX.txt")));
bw = new BufferedWriter(new FileWriter(new File("XXX.txt")));
// 读写操作
// char[] cbuf = new char[1024];
// int len;
// while((len = br.read(cbuf)) != -1){
// bw.write(cbuf, 0, len);
// }
// 方式二
String data;
while ((data = br.readLine()) != null){
bw.write(data + "n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}