一、Java IO 编程

Java IO 编程 
1、基本概念 
   Java中对文件的操作是以流的方式进行的,流是Java内存中一组有序数据序列。Java将数据从源(文件、内存、键盘、网络)读入到内存中,形成了流,然后还可以将这些流写到另外的目的地(文件、内存、控制台、网络)之所以叫做流,是因为这个数据序列在不同时刻所操作的是源的不同部分。 
2、流的分类 
流的分类方式一般有以下三种: 
(1) 输入的方向分:输入流和输出流,输入和输出的参照对象是Java程序。 
(2) 处理数据的单位分:字节流和字符流,字节流读取的最小单位是一个字节。 
(3) 功能的不同分:节点流和处理流,一个是直接一个是包装的。 
3、流分类的关系 
流分类的根源来自四个基本的类,这四个类的关系如下: 
   字节流            字符流 
输入流 InputStream Reader 
输出流 OutputStream Writer 
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、代码模拟实战 

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. public class FileDemoTest {  
  5.     /** 
  6.      * @description 基础File类操作 
  7.      * @param args 
  8.      * @throws IOException IO异常处理 
  9.      */  
  10.     public static void main(String[] args) throws IOException {  
  11.         // Windows系统下的文件目录格式,这个和Unix系统等不同  
  12.         // String pathSeparator = File.pathSeparator; // ;  
  13.         // String separator = File.separator; // \  
  14.   
  15.         // 1、创建和删除文件  
  16.         File file = new File("d:\\helloworld.txt");  
  17.         // File file = new File("d:"+File.separator+"helloworld.txt");  
  18.         file.createNewFile(); // 创建文件  
  19.         if (file.exists()) { // 如果文件存在,则删除文件  
  20.             file.delete();  
  21.         }  
  22.   
  23.         // 2、创建文件夹操作  
  24.         File file2 = new File("d:\\helloworld");  
  25.         // File file2 = new File("d:"+File.separator+"helloworld");  
  26.         file2.mkdir(); // 建立文件夹  
  27.         if (file2.isDirectory()) {  
  28.             System.out.println("hello-directory"); // 判断是否是目录  
  29.         }  
  30.   
  31.         // 3、遍历文件或者文件夹操作  
  32.         File file3 = new File("d:\\");  
  33.         // File file3 = new File("d:"+File.separator);  
  34.         File files[] = file3.listFiles(); // 列出全部内容  
  35.         for (int i = 0; i < files.length; i++) {  
  36.             System.out.println(files[i]);  
  37.         }  
  38.     }  
  39. }  

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.FileInputStream;  
  5.   
  6. public class InputStreamDemo {  
  7.     /** 
  8.      * @description 字节流:输入流InputStream 
  9.      * @param args 
  10.      * @throws IOException 
  11.      */  
  12.     public static void main(String[] args) throws IOException {  
  13.         // 字节流的输入流和输出流过程步骤比较固定  
  14.         // 第1步、使用File类找到一个文件  
  15.         File f = new File("d:" + File.separator + "helloworld.txt");  
  16.         // 第2步、通过子类实例化父类对象(InputStream为抽象类,本身不能直接实例化)  
  17.         InputStream input = new FileInputStream(f);  
  18.         // 第3步、进行读操作  
  19.         byte b[] = new byte[(int)f.length()]; // 数组大小由文件大小来确定  
  20.         for (int i = 0; i < b.length; i++) {  
  21.             b[i] = (byte) input.read(); // 读取内容  
  22.         }  
  23.         // 第4步、关闭输出流  
  24.         input.close();   
  25.     }  
  26. }  

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.io.OutputStream;  
  4. import java.io.FileOutputStream;  
  5.   
  6. public class OutputStreamDemo {  
  7.     /** 
  8.      *@description 字节流:输出流OutputStream类 
  9.      *@param args 
  10.      *@throws IOException  
  11.      */  
  12.     public static void main(String[] args) throws IOException {  
  13.         // 输入和输出流参考的是Java程序,输出流操作步骤也比较固定  
  14.         // 第1步、使用File类找到一个文件  
  15.         File f = new File("d:" + File.separator + "helloworld.txt");  
  16.         // 第2步、通过子类实例化父类对象  
  17.         //OutputStream out = new FileOutputStream(f);  
  18.         OutputStream out = new FileOutputStream(f,true);  
  19.         // 第3步、进行写操作  
  20.         String str = "say hello world!!!";  
  21.         byte b[] = str.getBytes();  
  22.         for(int i=0;i<b.length;i++){   
  23.             out.write(b[i]); // 单个写入  
  24.         }  
  25.         // out.write(b);  
  26.         // 重载方法write  
  27.         // public abstract void write(int b) throws IOException;  
  28.         // public void write(byte b[]) throws IOException {}  
  29.         // 第4步、关闭输出流  
  30.         out.close(); // 关闭输出流  
  31.     }  
  32. }  

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.io.Reader;  
  4. import java.io.FileReader;  
  5.   
  6. public class ReaderDemo {  
  7.     /** 
  8.      *@description 字节流和字符流按照处理数据的单位划分 
  9.      *@param args 
  10.      *@throws IOException IO异常处理 
  11.      */  
  12.     public static void main(String[] args) throws IOException {  
  13.         // 第1步、使用File类找到一个文件  
  14.         File f = new File("d:" + File.separator + "helloworld.txt");  
  15.         // 第2步、通过子类实例化父类对象  
  16.         Reader input = new FileReader(f);  
  17.         // 第3步、进行读操作  
  18.         char c[] = new char[1024];  
  19.         int temp = 0;  
  20.         int len = 0;  
  21.         while ((temp = input.read()) != -1) {  
  22.             // 如果不是-1就表示还有内容,可以继续读取  
  23.             c[len] = (char) temp;  
  24.             len++;  
  25.         }  
  26.         // 第4步、关闭输出流  
  27.         input.close();  
  28.     }  
  29. }  

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.io.Writer;  
  4. import java.io.FileWriter;  
  5.   
  6. public class WriterDemo {  
  7.     /** 
  8.      *@description 通过代码学习发现,基本上步骤一致的 
  9.      *@param args 
  10.      *@throws IOException 
  11.      */  
  12.     public static void main(String[] args) throws IOException {  
  13.         // 第1步、使用File类找到一个文件  
  14.         File f = new File("d:" + File.separator + "helloworld.txt");  
  15.         // 第2步、通过子类实例化父类对象  
  16.         Writer out = new FileWriter(f); // 通过对象多态性,进行实例化  
  17.         // 第3步、进行写操作  
  18.         String str = "\nsay hello\tworld";   
  19.         out.write(str);   
  20.         // 第4步、关闭输出流  
  21.         // out.flush(); // 清空缓冲  
  22.         out.close(); // 关闭输出流  
  23.           
  24.         // 另外有缓冲功能的类为:BufferedReader  
  25.     }  
  26. }  

Java代码   收藏代码
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.io.OutputStreamWriter;  
  9. import java.io.Reader;  
  10. import java.io.Writer;  
  11.   
  12. public class ByteAndCharStreamTest {  
  13.     /** 
  14.      *@description  字节流:InputStream,OutputStream 
  15.      *              字符流:Reader,Writer   
  16.      *              // 字节流和字符流相互转换测试 
  17.      *@param args 
  18.      * @throws IOException 文件操作异常处理 
  19.      */  
  20.     public static void main(String[] args) throws IOException {  
  21.         // 1、输入流-字节流转换成字符流  
  22.         // 通过InputStreamReader类来进行转换  
  23.         InputStream is = new FileInputStream("d:\\helloworld.txt");  
  24.         Reader reader = new InputStreamReader(is); // 将字节流转换成字符流  
  25.         char c[] = new char[1024];  
  26.         int len = reader.read(c); // 读取操作,保存在字符数组中  
  27.         reader.close(); // 关闭操作  
  28.         System.out.println(new String(c,0,len)); // 字符数组转换成String类实例  
  29.           
  30.         // 2、输出流-字节流转换成字符流  
  31.         // 通过OutputStreamWriter类来进行转换  
  32.         File f = new File("d:" + File.separator + "helloworld.txt");  
  33.         Writer out = new OutputStreamWriter(new FileOutputStream(f)); // 字节流变为字符流  
  34.         out.write("hello world!!!"); // 使用字符流输出  
  35.         out.close();  
  36.           
  37.         // 3、从字符流到字节流转换可以采用String类提供的操作  
  38.         // 从字符流中获取char[],转换成String实例然后再调用String API的getBytes()方法   
  39.         // 接1中的代码如下,最后通过ByteArrayInputStream即可完成操作  
  40.         String str = new String(c,0,len);  
  41.         byte b[] = str.getBytes();  
  42.         InputStream is2 = new ByteArrayInputStream(b);  
  43.         is2.close();  
  44.           
  45.         // 4、其他常见的流  
  46.         // 内存操作流ByteArrayInputStream,ByteArrayOutputStream  
  47.         // 管道流PipedOutputStream,PipedInputStream  
  48.         // 打印流PrintStream  
  49.         // 缓存流BufferedReader  
  50.         // ...等等  
  51.     }  
  52. }  

Java代码   收藏代码
  1. import java.io.FileOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.OutputStream;  
  4.   
  5. public class CharSetDemo {  
  6.     /** 
  7.      * @description 字符编码学习测试方法 
  8.      * @param args 
  9.      * @throws IOException 文件IO异常处理 
  10.      */  
  11.     public static void main(String[] args) throws IOException {  
  12.         // 1、字符编码,通过system类来获取  
  13.         String fe = System.getProperty("file.encoding");  
  14.         System.out.println(fe); // GBK  
  15.           
  16.         // 2、进行转码操作  
  17.         OutputStream out = new FileOutputStream("d:\\helloworld.txt");  
  18.         byte b[] = "Java,你好!!!".getBytes("ISO8859-1"); // 转码操作  
  19.         out.write(b); // 保存  
  20.         out.close(); // 关闭  
  21.     }  
  22. }  

Java代码   收藏代码
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.InputStream;  
  4. import java.io.ObjectInputStream;  
  5. import java.io.OutputStream;  
  6. import java.io.ObjectOutputStream;  
  7. import java.io.Serializable;  
  8.   
  9. class Demo implements Serializable{ // 实现序列化接口  
  10.     // 序列化方式之一,Java自身提供的序列化支持  
  11.     // 其他序列化方式,待以后有需要进一步学习  
  12.     private static final long serialVersionUID = 1L;  
  13.     private String info; // 定义私有属性  
  14.       
  15.     // 如果某个属性不想被序列化,采用transient来标识  
  16.     //private transient String noser;   
  17.       
  18.     public Demo(String info){  
  19.         this.info = info;  
  20.     }  
  21.     public String getInfo() {  
  22.         return info;  
  23.     }  
  24.     public void setInfo(String info) {  
  25.         this.info = info;  
  26.     }  
  27.     public String toString() {  
  28.         return "info = " + this.info;  
  29.     }  
  30. }  
  31. public class SerializedDemo {  
  32.     /** 
  33.      *@description 对象序列化、反序列化操作 
  34.      *@param args 
  35.      *@throws Exception 
  36.      */  
  37.     public static void main(String[] args) throws Exception {  
  38.         // 1、对象序列化是将内存中的对象转换成二进制形式的数据流  
  39.         // 2、对象反序列化刚好和对象序列化方向相反,将二进制数据流转换成对象  
  40.           
  41.         // a、对象序列化采用ObjectOutputStream类  
  42.         ObjectOutputStream oos = null// 声明对象输出流  
  43.         OutputStream out = new FileOutputStream("d:\\helloworld.txt");   
  44.         oos = new ObjectOutputStream(out);  
  45.         oos.writeObject(new Demo("helloworld"));  
  46.         oos.close();  
  47.           
  48.         // b、对象反序列化采用ObjectInputStream类  
  49.         ObjectInputStream ois = null// 声明对象输入流  
  50.         InputStream input = new FileInputStream("d:\\helloworld.txt");  
  51.         ois = new ObjectInputStream(input); // 实例化对象输入流  
  52.         Object obj = ois.readObject(); // 读取对象  
  53.         ois.close();  
  54.         System.out.println(obj);  
  55.     }  
  56. }  

二、Java类集合框架 
在百度搜到的一个比较详细的java类集合笔记,链接如下: 
http://wenku.baidu.com/view/52cf133f5727a5e9856a6186.html  
最后,记录一个思考题目:采用Java类集合如何实现一个stack,使得增加、删除、获取最大值、最小值以及中值的时间操作效率相当。 
Java代码   收藏代码
  1. /** 
  2.  * @author Administrator 
  3.  *  
  4.  * @description 如何从java类集框架中选取适当的数据结构呢??? 
  5.  * @history 
  6.  */  
  7. public interface Stack<T> {  
  8.     public void add(T t); // 添加元素  
  9.     public void delete(T t); // 删除元素  
  10.     public T getMax(); // 获取最大值  
  11.     public T getMin(); // 获取最小值  
  12.     public T getMiddle(); // 获取第中间大值  
  13. }  
<!--EndFragment-->
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值