Java学习笔记之四
Java学习笔记之四
主要内容为:Java IO和Java类集
一、Java IO 编程
1、基本概念
Java中对文件的操作是以流的方式进行的,流是Java内存中一组有序数据序列。Java将数据从源(文件、内存、键盘、网络)读入到内存中,形成了流,然后还可以将这些流写到另外的目的地(文件、内存、控制台、网络)之所以叫做流,是因为这个数据序列在不同时刻所操作的是源的不同部分。
2、流的分类
流的分类方式一般有以下三种:
(1)输入的方向分:输入流和输出流,输入和输出的参照对象是Java程序。
(2)处理数据的单位分:字节流和字符流,字节流读取的最小单位是一个字节。
(3)功能的不同分:节点流和处理流,一个是直接一个是包装的。
3、流分类的关系
流分类的根源来自四个基本的类,这四个类的关系如下:
字节流 字符流
输入流InputStreamReader
输出流OutputStreamWriter
4、其他知识补充
(1)什么是IO
IO(Input/Output)是计算机输入/输出的接口,Java的核心库java.io提供了全面的IO接口,包括:文件读写、标准设备输出等等。Java中的IO是以流为基础进行输入输出的,所有数据被串行化写入输出流,或者从输入流读入。
(2)流IO和块IO
此外,Java也对块传输提供支持,在核心库java.nio中采用的便是块IO,流IO和块IO对比而言,流IO的好处是简单易用,缺点是效率不如块IO;相反块IO是效率比较高但是编程比较复杂。Java的IO模型设计非常优秀,它使用了Decorator模式,按照功能进行划分stream,编程过程中可以动态地装配这些stream,以便获取所需要的功能。
备注:以上资料提取自百度文库,链接地址如下:
http://wenku.baidu.com/view/9aa0ec35eefdc8d376ee3280.html
5、代码模拟实战
import java.io.File;import java.io.IOException;public class FileDemoTest {/** * @description 基础File类操作 * @param args * @throws IOException IO异常处理 */public static void main(String[] args) throws IOException {// Windows系统下的文件目录格式,这个和Unix系统等不同// String pathSeparator = File.pathSeparator; // ;// String separator = File.separator; // \// 1、创建和删除文件File file = new File("d:\\helloworld.txt");// File file = new File("d:"+File.separator+"helloworld.txt");file.createNewFile(); // 创建文件if (file.exists()) { // 如果文件存在,则删除文件file.delete();}// 2、创建文件夹操作File file2 = new File("d:\\helloworld");// File file2 = new File("d:"+File.separator+"helloworld");file2.mkdir(); // 建立文件夹if (file2.isDirectory()) {System.out.println("hello-directory"); // 判断是否是目录}// 3、遍历文件或者文件夹操作File file3 = new File("d:\");// File file3 = new File("d:"+File.separator);File files[] = file3.listFiles(); // 列出全部内容for (int i = 0; i < files.length; i++) {System.out.println(files[i]);}}}import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.FileInputStream;public class InputStreamDemo {/** * @description 字节流:输入流InputStream * @param args * @throws IOException */public static void main(String[] args) throws IOException {// 字节流的输入流和输出流过程步骤比较固定// 第1步、使用File类找到一个文件File f = new File("d:" + File.separator + "helloworld.txt");// 第2步、通过子类实例化父类对象(InputStream为抽象类,本身不能直接实例化)InputStream input = new FileInputStream(f);// 第3步、进行读操作byte b[] = new byte[(int)f.length()]; // 数组大小由文件大小来确定for (int i = 0; i < b.length; i++) {b[i] = (byte) input.read(); // 读取内容}// 第4步、关闭输出流input.close(); }}import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.io.FileOutputStream;public class OutputStreamDemo {/** *@description 字节流:输出流OutputStream类 *@param args *@throws IOException */public static void main(String[] args) throws IOException {// 输入和输出流参考的是Java程序,输出流操作步骤也比较固定// 第1步、使用File类找到一个文件File f = new File("d:" + File.separator + "helloworld.txt");// 第2步、通过子类实例化父类对象//OutputStream out = new FileOutputStream(f);OutputStream out = new FileOutputStream(f,true);// 第3步、进行写操作String str = "say hello world!!!";byte b[] = str.getBytes();for(int i=0;i<b.length;i++){out.write(b[i]); // 单个写入}// out.write(b);// 重载方法write// public abstract void write(int b) throws IOException;// public void write(byte b[]) throws IOException {}// 第4步、关闭输出流out.close(); // 关闭输出流}}import java.io.File;import java.io.IOException;import java.io.Reader;import java.io.FileReader;public class ReaderDemo {/** *@description 字节流和字符流按照处理数据的单位划分 *@param args *@throws IOException IO异常处理 */public static void main(String[] args) throws IOException {// 第1步、使用File类找到一个文件File f = new File("d:" + File.separator + "helloworld.txt");// 第2步、通过子类实例化父类对象Reader input = new FileReader(f);// 第3步、进行读操作char c[] = new char[1024];int temp = 0;int len = 0;while ((temp = input.read()) != -1) {// 如果不是-1就表示还有内容,可以继续读取c[len] = (char) temp;len++;}// 第4步、关闭输出流input.close();}}import java.io.File;import java.io.IOException;import java.io.Writer;import java.io.FileWriter;public class WriterDemo {/** *@description 通过代码学习发现,基本上步骤一致的 *@param args *@throws IOException */public static void main(String[] args) throws IOException {// 第1步、使用File类找到一个文件File f = new File("d:" + File.separator + "helloworld.txt");// 第2步、通过子类实例化父类对象Writer out = new FileWriter(f); // 通过对象多态性,进行实例化// 第3步、进行写操作String str = "\nsay hello\tworld"; out.write(str); // 第4步、关闭输出流// out.flush(); // 清空缓冲out.close(); // 关闭输出流// 另外有缓冲功能的类为:BufferedReader}}import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.Reader;import java.io.Writer;public class ByteAndCharStreamTest {/** *@description 字节流:InputStream,OutputStream *字符流:Reader,Writer * // 字节流和字符流相互转换测试 *@param args * @throws IOException 文件操作异常处理 */public static void main(String[] args) throws IOException {// 1、输入流-字节流转换成字符流// 通过InputStreamReader类来进行转换InputStream is = new FileInputStream("d:\\helloworld.txt");Reader reader = new InputStreamReader(is); // 将字节流转换成字符流char c[] = new char[1024];int len = reader.read(c); // 读取操作,保存在字符数组中reader.close(); // 关闭操作System.out.println(new String(c,0,len)); // 字符数组转换成String类实例// 2、输出流-字节流转换成字符流// 通过OutputStreamWriter类来进行转换File f = new File("d:" + File.separator + "helloworld.txt");Writer out = new OutputStreamWriter(new FileOutputStream(f)); // 字节流变为字符流out.write("hello world!!!"); // 使用字符流输出out.close();// 3、从字符流到字节流转换可以采用String类提供的操作// 从字符流中获取char[],转换成String实例然后再调用String API的getBytes()方法 // 接1中的代码如下,最后通过ByteArrayInputStream即可完成操作String str = new String(c,0,len);byte b[] = str.getBytes();InputStream is2 = new ByteArrayInputStream(b);is2.close();// 4、其他常见的流// 内存操作流ByteArrayInputStream,ByteArrayOutputStream// 管道流PipedOutputStream,PipedInputStream// 打印流PrintStream// 缓存流BufferedReader// ...等等}}import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class CharSetDemo {/** * @description 字符编码学习测试方法 * @param args * @throws IOException 文件IO异常处理 */public static void main(String[] args) throws IOException {// 1、字符编码,通过system类来获取String fe = System.getProperty("file.encoding");System.out.println(fe); // GBK// 2、进行转码操作OutputStream out = new FileOutputStream("d:\\helloworld.txt");byte b[] = "Java,你好!!!".getBytes("ISO8859-1"); // 转码操作out.write(b); // 保存out.close(); // 关闭}}import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.OutputStream;import java.io.ObjectOutputStream;import java.io.Serializable;class Demo implements Serializable{ // 实现序列化接口// 序列化方式之一,Java自身提供的序列化支持// 其他序列化方式,待以后有需要进一步学习private static final long serialVersionUID = 1L;private String info; // 定义私有属性// 如果某个属性不想被序列化,采用transient来标识//private transient String noser; public Demo(String info){this.info = info;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}public String toString() {return "info = " + this.info;}}public class SerializedDemo {/** *@description 对象序列化、反序列化操作 *@param args *@throws Exception */public static void main(String[] args) throws Exception {// 1、对象序列化是将内存中的对象转换成二进制形式的数据流// 2、对象反序列化刚好和对象序列化方向相反,将二进制数据流转换成对象// a、对象序列化采用ObjectOutputStream类ObjectOutputStream oos = null; // 声明对象输出流OutputStream out = new FileOutputStream("d:\\helloworld.txt"); oos = new ObjectOutputStream(out);oos.writeObject(new Demo("helloworld"));oos.close();// b、对象反序列化采用ObjectInputStream类ObjectInputStream ois = null; // 声明对象输入流InputStream input = new FileInputStream("d:\\helloworld.txt");ois = new ObjectInputStream(input); // 实例化对象输入流Object obj = ois.readObject(); // 读取对象ois.close();System.out.println(obj);}}/** * @author Administrator * * @description 如何从java类集框架中选取适当的数据结构呢??? * @history */public interface Stack<T> {public void add(T t); // 添加元素public void delete(T t); // 删除元素public T getMax(); // 获取最大值public T getMin(); // 获取最小值public T getMiddle(); // 获取第中间大值}