package java6; import com.sun.org.apache.bcel.internal.generic.NEW; import org.junit.Test; import java.io.*; /** * 一、流的分类: * 1.操作数据单位:字节流,字符流 * 2.数据的流向:输入流,输出流 * 3.流的角色:节点流,处理流 * 二、流的体系结构 * 抽象基类 节点流(或者文件流) 缓冲流(处理流的一种) * InputStream --->输入流 ---> FileInputStream(read(byte[] BT) ---> BufferedInputStream(read(byte[] BT) * OutputStream --->输出流 ---> FileOutputStream(write(BT,0,len) ---> BufferedOutputStream(write(BT,0,len) * Reader --->输入流 ---> FileReader(read(char[] cbuf) ---> BufferedReader(read(char[] cbuf)/readLine() * Writer --->输出流 ---> FileWriter (write(cbuf,0,len) ---> BufferedWriter(write(cbuf,0,len) * 不能用字符流来处理图片等字节数据 * @author 冀帅 * @date 2020/8/18-21:20 */ public class FileReaderWriterTest { public static void main(String[] args) { File file = new File("hello.txt");//相较于当前工程 System.out.println(file.getAbsolutePath()); File file1 = new File("DayTest0806\\hello.txt"); System.out.println(file1.getAbsolutePath()); } /* *将DayTest0806下的hello.txt文件内容读入程序中,并输出到控制台。 * 说明点: * 1.read()的理解:返回读入的一个字符,如果达到文件末尾,就返回-1 * 2.异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try -catch-finally处理 * 3.读入的文件一定要存在,否则就会报异常:FileNotFoundException, * */ @Test public void testFileReader() { // 1.实例化File类对象,指明要操作文件。 FileReader fr = null; try { File file = new File("hello.txt");//相较于当前module // 2.提供具体的流 fr = new FileReader(file); // 3.数据的读入过程 // read返回读入的一个字符,如果达到文件末尾,就返回-1 // 方式一: // int data = fr.read();//文件以数字存在data,读入了一个 // while (data!=-1){ // System.out.print((char) data);//强转 // data = fr.read();//继续读,直到为-1 // } // 方式二: 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(); } } } //read()的操作升级,使用read的重载方法 @Test public void testFileRead1() { // 1.File类的实例化 FileReader fr = null; try { File file = new File("hello.txt"); // 2.FileReader流的实例化 fr = new FileReader(file); // 3.读入的具体的操作细节 // 用read的别的构造方法,放到数组里,一次性读完,快。 read(char[] cubf);返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1 char[] cbuf = new char[5]; ;//一次读5个 int len; 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);//从头开始取,每次只取len个。 System.out.print(str); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.资源的关闭 try { if (fr!=null) fr.close(); } catch (IOException e) { e.printStackTrace(); } } } /* * 从内存中写出数据到硬盘文件里 * 说明: * 1.输出操作:对应的File是可以不存在的。并不会报异常 * 2. * File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。 * File对应的硬盘中的文件如果存在: * 如果流使用的构造器是:FileWriter(file,false)/FileWriter(file):对原有的文件覆盖 * 如果流使用的构造器是:FileWriter(file,true):不会对原有文件进行覆盖,而是在原有文件的基础上追加内容。, * * * */ @Test public void testFileWriter() { // 1.提供File类的对象,指明写出到的文件 FileWriter fw = null;//参数可不加,默认为false,就是不追加,直接覆盖,要是true的话,会追加,不会覆盖 try { 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.流资源的关闭 try { if (fw!=null) fw.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void teseFileRreaderFileWriter() { // 1.创建Flile类的对象,指明读入和写出的文件 FileReader fr = null; FileWriter fw = null; try { File srcFile = new File("hello.txt"); File destFile = new File("hello2.txt"); // 2.创建输入流和输出流的对象 fr = new FileReader(srcFile); fw = new FileWriter(destFile); // 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 { // fr.close(); // } catch (IOException e) { // e.printStackTrace(); // } // // } // 方式二 try { if (fw!=null) fw.close(); } catch (IOException e) { e.printStackTrace(); }try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } // } }