新博客地址:Temijava
第一讲:IO流(概述)linux
一,IO流的概述:git
二,IO流经常使用基类:github
=========注:由这四个基类派生出的子类名称都是以父类名称做为后缀的,例如FileInputStream 是 InputStream 类的子类 FileWriter 是Writer 类的子类。前缀名是流对象的功能=========web
第二讲:IO流(FileWriter)api
一,Writer类的基本了解(查阅API):数组
二,写入字符流的步骤:app
===知识点扩展:Java语言自己是不能往硬盘里面写数据的(不一样系统写入数据方式不一样)。Java的数据写入会调用系统的写入资源。系统资源使用后必须关闭===函数
三,代码练习:编码
1 //导入须要用到的包。 2 import java.io.*; 3 4 public class FileWriterDemo{ 5 public static void main(String args[]) throws IOException{ 6 7 8 //建立一个 FileWriter 对象 9 FileWriter fw=new FileWriter("Demo.txt"); 10 11 //调用Writer 方法写入数据 12 fw.writer("HelloWorld"); 13 14 //刷新流 15 fw.flush(); 16 17 //关闭流,一样会触发刷新 18 fw.close(); 19 } 20 }
第三讲:IO流(IO异常处理方式)
一,对于 IO 异常处理的一些说明:
二,代码练习:
1 import java.io.*; 2 3 public class FileWriterDemo2{ 4 public static void main(String args[]){ 5 6 //在 try 语句外面创建对象引用 7 FileWriter fw=null; 8 9 10 //try 语句捕获异常 11 try{ 12 13 14 //try 内部进行对象初始化 15 fw=new FileWriter("Demo.txt"); 16 17 //写入数据 18 fw.write("HelloHeiMa"); 19 }catch (IOException e){ 20 System.out.println(e.toString()); 21 }finally{ 22 23 24 //关闭动做一样进行 try 处理 25 try{ 26 //判断 fw 是否为空 27 if(fw!=null) 28 fw.close(); 29 }catch (IOException e){ 30 System.out.println(e.toSring()); 31 } 32 } 33 }
第四讲:IO流(文件的续写)
一,进行数据续写思路的分析:
二,知识点扩充:
三,代码练习:
1 import java.io.*; 2 3 4 public class FileWriterDemo3 { 5 public static void main(String args[]){ 6 7 //声明FileWriter对象 8 FileWriter fw=null; 9 try{ 10 11 //以追加方式实例化FileWriter对象, 12 fw=new FileWriter("E:/demo.txt",true); 13 14 //向文件中追加数据 15 fw.write("abcd\r\nHeiMa"); 16 }catch(IOException e){ 17 System.out.println("内容写入失败"); 18 }finally{ 19 20 21 //关闭系统资源 22 try{ 23 if(fw!=null) 24 fw.close(); 25 }catch(IOException e){ 26 System.out.println("关闭流操做失败"); 27 } 28 } 29 } 30 }
第五讲:IO流(文本文件读取方式一)
一,FileReader类的了解:
(File file) throws FileNotFoundException ===当指定的文件不存在时抛出FileNotFoundException====
public FileReader(File file) throws FileNotFoundException
二,文本文件读取过程:
三,代码练习:
1 import java.io.*; 2 3 4 public class FileWriterDemo4 { 5 public static void main(String args[]){ 6 7 8 //声明文件读写对象 9 FileReader fr=null; 10 11 12 //实例化对象,进行文件读写 13 try{ 14 15 //若文件不存在抛出FileNotFoundException异常 16 fr=new FileReader("E:\\demo.txt"); 17 18 //定义int 变量ch 接受读取结果 19 int ch; 20 21 //若读取结果为-1则表示到达文件末尾,读取结束 22 while((ch=fr.read())!=-1) 23 System.out.println("ch"+(char)ch); 24 System.out.println("读取完毕"); 25 26 27 //文件不存在的异常处理 28 }catch(FileNotFoundException e){ 29 System.out.println("指定的文件不存在"); 30 }catch(IOException e){ 31 System.out.println("读取文件发生错误"); 32 }finally{ 33 34 //关闭系统资源 35 try{ 36 if(fr!=null) 37 fr.close(); 38 }catch(IOException e){ 39 System.out.println("关闭文件发生错误"); 40 } 41 } 42 } 43 }
第六讲:IO流(文本文件读取方式二)
一,用到的方法:
二,注意事项:
三,代码练习:
1 import java.io.*; 2 3 4 public class FileWriterDemo4 { 5 public static void main(String args[]){ 6 7 8 //声明文件读写对象 9 FileReader fr=null; 10 11 12 //实例化对象,进行文件读写 13 try{ 14 15 //若文件不存在抛出FileNotFoundException异常 16 fr=new FileReader("E:\\demo.txt"); 17 18 //定义整形变量表示读取到的字符数量 19 int ch; 20 21 22 //定义字符数组向里读取字符 23 char[] cbuf=new char[1024]; 24 25 //若读取结果为-1则表示到达文件末尾,读取结束 26 while((ch=fr.read(cbuf))!=-1) 27 28 //截取有效的字符数 29 System.out.println(new String(cbuf,0,ch)); 30 System.out.println("读取完毕"); 31 32 33 //文件不存在的异常处理 34 }catch(FileNotFoundException e){ 35 System.out.println("指定的文件不存在"); 36 }catch(IOException e){ 37 System.out.println("读取文件发生错误"); 38 }finally{ 39 40 //关闭系统资源 41 try{ 42 if(fr!=null) 43 fr.close(); 44 }catch(IOException e){ 45 System.out.println("关闭文件发生错误"); 46 } 47 } 48 } 49 }
第七讲:IO流(文本文件读取练习)
一,练习要求:
二,实现代码:
1,打印文件:
package com.examp.ch18; import java.io.*; public class FileWriterDemo4 { public static void main(String args[]){ //声明文件读写对象 FileReader fr=null; String filename="E:\\test.java"; //判断文件名是否为.java 结尾。 if(!filename.endsWith(".java")){ System.out.println("目标文件后坠名不是.java"); return; } //实例化对象,进行文件读写 try{ //若文件不存在抛出FileNotFoundException异常 fr=new FileReader(filename); //定义整形变量表示读取到的字符数量 int ch; //定义字符数组向里读取字符 char[] cbuf=new char[1024]; //若读取结果为-1则表示到达文件末尾,读取结束 while((ch=fr.read(cbuf))!=-1) //截取有效的字符数 System.out.println(new String(cbuf,0,ch)); System.out.println("读取完毕"); //文件不存在的异常处理 }catch(FileNotFoundException e){ System.out.println("指定的文件不存在"); }catch(IOException e){ System.out.println("读取文件发生错误"); }finally{ //关闭系统资源 try{ if(fr!=null) fr.close(); }catch(IOException e){ System.out.println("关闭文件发生错误"); } } } }
2,复制文件:
1 import java.io.*; 2 3 4 public class FileWriterDemo4 { 5 public static void main(String args[]){ 6 if(copy("E:\\test.java","F:\\demo.txt")) 7 8 9 //复制成功给出提示 10 System.out.println("复制成功"); 11 } 12 13 14 public static boolean copy(String srcfile,String dest){ 15 16 //声明读文件对象以及写文件对象的引用。 17 FileReader fr=null; 18 FileWriter fw=null; 19 20 21 //定义int变量表示读取的字符数 22 int ch; 23 24 //定义字符数组,用来读取 25 char[] cbuf=new char[1024]; 26 try{ 27 28 //分别实例化源文件的读对象,以及目的文件的写对象 29 fr=new FileReader(srcfile); 30 fw=new FileWriter(dest); 31 32 33 //边读边写,实现文件复制 34 while((ch=fr.read(cbuf))!=-1){ 35 fw.write(cbuf, 0, ch); 36 } 37 38 39 //复制成功返回true 40 return true; 41 }catch(FileNotFoundException e){ 42 43 //产生异常,给出提示,返回false 44 System.out.println(e.toString()); 45 return false; 46 }catch(IOException e){ 47 48 49 //产生异常,给出提示,返回false 50 System.out.println("复制文件产生错误"); 51 return false; 52 }finally{ 53 54 //释放系统资源 55 try{ 56 if(fr!=null) 57 fr.close(); 58 if(fw!=null) 59 fw.close(); 60 }catch(IOException e){ 61 62 63 //产生异常,给出提示。 System.out.println("关闭文件产生错误!"); 64 } 65 } 66 } 67 }