聊天室项目框架:
IO流学习:
IO流原理及流的分类:
Java IO 原理 :
流的分类:
IO流体系:
IO流简单的操作:
(类似于C语言的文件操作)
1、实体化File类的对象,指明要操作的文件
2、提供具体的流
3、数据的读入
package FileTest; 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){ FileReader fr = null; try { //1、实体化File类的对象,指明要操作的文件 File file = new File("C:\\Users\\Test\\src\\FileTest\\hello.txt");//相当与当前工程 System.out.println(file.getAbsolutePath()); //2、提供具体的流 fr = new FileReader(file); //3、数据的读入 //read();返回读入的一个字符,如果达到文件的末尾,返回-1 /* 方式一: int read = fr.read(); while ( read != -1 ) { System.out.print((char)read); read = fr.read(); } */ //方式二 char[] cbuf = new char[5]; int len; while ((len = fr.read(cbuf)) != -1) { for (int i = 0 ; i < len ; i ++) { System.out.print(cbuf[i]); } } } catch (IOException e) { e.printStackTrace(); } finally { //4、流的关闭 try { if (fr != null) { fr.close(); } } catch (IOException e) { e.printStackTrace(); } //写出数据的操作 //1、提供File类的对象,指明写出的文件 //2、提供FileWriter的对象,用于数据的写出 //3、写出的操作 //4、流的关闭 } } }