JAVA核心技术I---JAVA基础知识(文本文件读写)
一:java IO包概述
(一)Java读写文件,只能以(数据)流的形式进行读写
(二)java IO 包
–节点类:直接对文件进行读写
–包装类 • 转化类:字节/字符/数据类型的转化类 • 装饰类:装饰节点类
(三)节点类
–InputStream, OutputStream(字节)
• FileInputStream,FileOutputStream
–Reader,Writer(字符)
• FileReader, FileWriter
(四)转换类:字符到字节之间的转化
–InputStreamReader:文件读取时字节,转化为Java能理解的字符
–OutputStreamWriter:Java将字符转化为字节输入到文件中
(五)装饰类:装饰节点类
–DataInputStream,DataOutputStream: 封装数据流
–BufferedInputStream,BufferOutputStream:缓存字节流
–BufferedReade, BufferedWriter:缓存字符流
二:文本文件读写
–输出文本字符到文件中
–从文件中读取文本字符串
(一)写文件
–先创建文件,写入数据,关闭文件 –FileOutputStream, OutputStreamWriter, BufferedWriter –BufferWriter • write • newLine –try-resource 语句,自动关闭资源 –关闭最外层的数据流,将会把其上所有的数据流关闭
public static void writeFile1() { FileOutputStream fos = null; OutputStreamWriter osw = null; BufferedWriter bw = null; try { fos = new FileOutputStream("c:/temp/abc.txt"); // 节点类 osw = new OutputStreamWriter(fos, "UTF-8"); // 转化类 //osw = new OutputStreamWriter(fos); // 转化类 bw = new BufferedWriter(osw); // 装饰类 // br = new BufferedWriter(new OutputStreamWriter(new // FileOutputStream("c:/temp/abc.txt"))) bw.write("我们是"); bw.newLine(); bw.write("Ecnuers.^^"); bw.newLine(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { bw.close(); // 关闭最后一个类,会将所有的底层流都关闭 } catch (Exception ex) { ex.printStackTrace(); } } }
public static void writeFile2() { //try-resource 语句,自动关闭资源 try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:/temp/abc.txt")))) { bw.write("我们是"); bw.newLine(); bw.write("Ecnuers.^^"); bw.newLine(); } catch (Exception ex) { ex.printStackTrace(); } }
(二)读文件
–先打开文件,逐行读入数据,关闭文件 –FileInputStream, InputStreamWriter, BufferedReade –BufferReader • readLine –try-resource 语句,自动关闭资源 –关闭最外层的数据流,将会把其上所有的数据流关闭
public static void readFile1() { FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; try { fis = new FileInputStream("c:/temp/abc.txt"); // 节点类 isr = new InputStreamReader(fis, "UTF-8"); // 转化类 //isr = new InputStreamReader(fis); br = new BufferedReader(isr); // 装饰类 // br = new BufferedReader(new InputStreamReader(new // FileInputStream("c:/temp/abc.txt"))) String line; while ((line = br.readLine()) != null) // 每次读取一行 { System.out.println(line); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { br.close(); // 关闭最后一个类,会将所有的底层流都关闭 } catch (Exception ex) { ex.printStackTrace(); } } }
public static void readFile2() { String line; //try-resource 语句,自动关闭资源 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("c:/temp/abc.txt")))) { while ((line = in.readLine()) != null) { System.out.println(line); } } catch(Exception ex) { ex.printStackTrace(); } }
(三)混用写入和读取
public class FileW { public static void main(String[] args) { //写文件 FileOutputStream fos = null; //节点类 OutputStreamWriter osw = null; //转换类 BufferedWriter bw=null; //装饰类 try { fos = new FileOutputStream("D:\\MyJava\\Code\\JH16\\test\\aa\\bb\\E.txt"); osw = new OutputStreamWriter(fos); bw = new BufferedWriter(osw); //开始写入 bw.write("我们是:"); bw.newLine(); bw.write("fwawa!"); bw.newLine(); }catch(Exception e) { System.err.println(e.getMessage()); }finally{ try { bw.close(); }catch(Exception e) { System.err.println(e.getMessage()); } } //读取文件 String line; try(BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream("D:\\MyJava\\Code\\JH16\\test\\aa\\bb\\E.txt")))){ while((line=in.readLine())!=null) { System.out.println(line); } }catch(Exception e) { System.err.println(e.getMessage()); } } }
我们是:
fwawa!