1、FileReader读入数据的基本操作
1.1、读取文件【四个步骤】
建立一个流对象,将已存在的一个文件加载进流。
FileReaderfr= new FileReader(new File(“Test.txt”));
创建一个临时存放数据的数组。
char[] ch= new char[1024];
调用流对象的读取方法将流中的数据读入到数组中
fr.read(ch);
关闭资源
fr.close();
import org.junit.Test;import java.io.File;import java.io.FileReader;import java.io.IOException;/*** 一、流的分类:* 1.操作数据单位:字节流、字符流* 2.数据的流向:输入流、输出流* 3.流的角色:节点流、处理流** 二、流的体系结构* 抽象基类 节点流(或文件流) 缓冲流(处理流的一种)* InputStream FileInputStream (read(byte[] buffer)) BufferedInputStream (read(byte[] buffer))* OutputStream FileOutputStream (write(byte[] buffer,0,len) BufferedOutputStream (write(byte[] buffer,0,len) / flush()* Reader FileReader (read(char[] cbuf)) BufferedReader (read(char[] cbuf) / readLine())* Writer FileWriter (write(char[] cbuf,0,len) BufferedWriter (write(char[] cbuf,0,len) / flush()*/public class FileReaderWriterTest {public static void main(String[] args) {File file = new File("hello.txt");//相较于当前工程System.out.println(file.getAbsolutePath());File file1 = new File("day09\\hello.txt");System.out.println(file1.getAbsolutePath());}/*** 将day09下的hello.txt文件内容读入程序中,并输出到控制台** 说明点:* 1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1* 2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理* 3. 读入的文件一定要存在,否则就会报FileNotFoundException。**/@Testpublic void test(){FileReader fr = null;try {//实例化File对象,指明要操作的文件File file = new File("hello.txt");//相较于当前的Module//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();// }//或if(fr != null){try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}}
2、FileReader中使用read(char[] cbuf)读入数据
import org.junit.Test;import java.io.File;import java.io.FileReader;import java.io.IOException;public class FileReaderWriterTest {//对read()操作升级:使用read的重载方法@Testpublic void test2(){FileReader fr = null;try {//1.File类的实例化File file = new File("hello.txt");//2.FileReader流的实例化fr = new FileReader(file);//3.读入的操作//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1char[] cbuf = new char[5];int len;fr.read(cbuf);while((len = fr.read(cbuf)) != -1){//方式一://错误的写法// for(int i = 0;i < cbuf.length;i++){// System.out.print(cbuf[i]);// }//正确的写法// for(int i = 0;i < len;i++){// System.out.print(cbuf[i]);// }//方式二://错误的写法,对应着方式一的错误的写法// String str = new String(cbuf);// System.out.print(str);//正确的写法String str = new String(cbuf,0,len);System.out.print(str);}} catch (IOException e) {e.printStackTrace();}finally {if(fr != null){//4.资源的关闭try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}}
3、FileWriter写出数据的操作
1、写入文件
创建流对象,建立数据存放文件
FileWriterfw= new FileWriter(new File(“Test.txt”));
调用流对象的写入方法,将数据写入流
fw.write(“atguigu-songhongkang”);
关闭流资源,并将流中的数据清空到文件中
fw.close();
import org.junit.Test;import java.io.*;public class FileReaderWriterTest {/*** 从内存中写出数据到硬盘的文件里。** 说明:* 1.输出操作,对应的File可以不存在的。并不会报异常* 2.* File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。* File对应的硬盘中的文件如果存在:* 如果流使用的构造器是:FileWriter(file,false) / FileWriter(file):对原有文件的覆盖* 如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原有文件基础上追加内容*/@Testpublic void test3(){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 dream!\n");fw.write("you need to have a dream!");} catch (IOException e) {e.printStackTrace();} finally {//4.流资源的关闭if(fw != null){try {fw.close();} catch (IOException e) {e.printStackTrace();}}}}}
4、使用FileReader和FileWriter实现文本文件的复制
import org.junit.Test;import java.io.*;public class FileReaderWriterTest {@Testpublic void test4() {FileReader fr = null;FileWriter fw = null;try {//1.创建File类的对象,指明读入和写出的文件File srcFile = new File("hello1.txt");File srcFile2 = new File("hello2..txt");//不能使用字符流来处理图片等字节数据// File srcFile = new File("爱情与友情.jpg");// File srcFile2 = new File("爱情与友情1.jpg");//2.创建输入流和输出流的对象fr = new FileReader(srcFile);fw = new FileWriter(srcFile2);//3.数据的读入和写出操作char[] cbuf = new char[5];int len;//记录每次读入到cbuf数组中的字符的个数while((len = fr.read(cbuf)) != -1){//每次写出len个字符fw.write(cbuf,0,len);}} catch (IOException e) {e.printStackTrace();} finally {//4.关闭流资源//方式一:// try {// if(fw != null)// fw.close();// } catch (IOException e) {// e.printStackTrace();// }finally{// try {// if(fr != null)// fr.close();// } catch (IOException e) {// e.printStackTrace();// }// }//方式二:try {if(fw != null)fw.close();} catch (IOException e) {e.printStackTrace();}try {if(fr != null)fr.close();} catch (IOException e) {e.printStackTrace();}}}}
5、使用FileInputStream不能读取文本文件的测试
import org.junit.Test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;/*** 测试FileInputStream和FileOutputStream的使用** 结论:* 1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理* 2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理*/public class FileIOPutTest {//使用字节流FileInputStream处理文本文件,可能出现乱码。@Testpublic void testFileInputStream(){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 {if(fis != null) {//4.关闭资源try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}}
6、使用FileInputStream和FileOutputStream读写非文本文件
import org.junit.Test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileIOPutTest {/*** 实现对图片的复制操作*/@Testpublic void testFileInputOutputStream() {FileInputStream fis = null;FileOutputStream fos = null;try {//1.造文件File srcFile = new File("爱情与友情.jpg");File destFile = new File("爱情与友情2.jpg");//2.造流fis = new FileInputStream(srcFile);fos = new FileOutputStream(destFile);//3.复制的过程byte[] buffer = new byte[5];int len;//4.读数据while((len = fis.read(buffer)) != -1){fos.write(buffer,0,len);}System.out.println("复制成功");} catch (IOException e) {e.printStackTrace();} finally {if(fos != null){//5.关闭资源try {fos.close();} catch (IOException e) {e.printStackTrace();}}if(fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}}
7、使用FileInputStream和FileOutputStream复制文件的方法测试
import org.junit.Test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileIOPutTest {//指定路径下文件的复制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[1024];int len;while((len = fis.read(buffer)) != -1){fos.write(buffer,0,len);}} 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();}}}}@Testpublic void testCopyFile(){long start = System.currentTimeMillis();// String srcPath = "C:\\Users\\29433\\Desktop\\164.jpg";// String destPath = "C:\\Users\\29433\\Desktop\\164.jpg";String srcPath = "hello.txt";String destPath = "hello3.txt";copyFile(srcPath,destPath);long end = System.currentTimeMillis();System.out.println("复制操作花费的时间为:" + (end - start));//1}}
