BloveSaga

在希腊帕尔纳斯山南坡上,有一个驰名世界的戴尔波伊神托所,在它的入口处的巨石上赫然锈刻着这样几个大字: 认识你自己!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  34 随笔 :: 12 文章 :: 122 评论 :: 0 Trackbacks

               File类

 一个File类的对象,表示了磁盘上的文件或目录。
 File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作。

import java.io.*;
class FileTest
{
 public static void main(String[] args) throws Exception
 {
  //File f = new File("1.txt");
  //f.createNewFile();   创建文件
  //f.mkdir(); 创建文件夹
  //File f = new File("F:\\Java Develop\\1.txt");//使用绝对路径
  //f.createNewFile();
  /*
  *WINDOWS平台下有盘符,LINUX下是没有的
  *考虑到JAVA语言的平台性,所有用分隔符seperator/seperatorChar
  */
  /*
  File fDir = new File(File.separator);//创建了当前的根目录
  String strFile = "Java Develop"+File.separator+"1.txt";
  File f = new File(fDir,strFile);
  f.createNewFile();
  //f.delete();
  f.deleteOnExit();
  Thread.sleep(3000);
  */
  /*
  for(int i=0;i<5;i++)
  {
   File.createTempFile("linshi",".tmp");
   f.deleteOnExit();
  }
  Thread.sleep(3000);
  */
  
  File fDir = new File(File.separator);
  String strFile ="Java Develop"+File.separator;
  File f = new File(fDir,strFile);
  //文件过滤器
  String[] names = f.list(new FilenameFilter()
  {
   public boolean accept(File dir,String name)
   {
    return name.indexOf(".java")!=-1;
   }
  });
  for(int i=0;i<names.length;i++)
  {
   System.out.println(names[i]);
  }
 }
}

            流式I/0

 流(Stream)是字节的源或目的。
 两种基本的流是: 输入流(Input Stream)和输出流(Output Stream)。从从中读出一系列字节的
 对象称为输入流。而能向其中写入一系列字节的对象称为输出流。


           流的分类
 
 节点流: 从特定的地方读写的流类,例如:磁盘或一块内存区域。
 过滤流: 使用节点作为输入或输出。过滤流使用的是一个已经存在的输入流或输出流连接创建的。
 (如下图)


         InputStream(一个抽象的基类)
 .三个基本的读写方法
  abstract int read(): 读取一个字节数据,并返回到数据,如果返回-1,表示读到了输入流的
                       末尾。
  int read(byte[] b):  将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,
                       表示读到了输入流的末尾。
  int read(byte[] b,int off,int len): 将数据读入一个字节数组,同时返回是实际读取的字
                       节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放
                       数据的起始偏移位置;len指定读取的最大字节数。
 其他的方法
  long-skip(long n): 在输入流中跳过n个字节,并返回实际跳过的字节数。
  int available():   返回在不发生阻塞的情况下,可读取的字节数。
  void close():      关闭输入流,释放和这个流相关的系统资源。
  void mark(int reqdlimit): 在输入流的当前位置放置一个标记,如果读取的字节数多余
                     readlimit设置的值,则流忽略这个标记。
  void reset():      返回到上一个标记。
  boolean markSupported(): 测试当前是否支持mark和reset方法。如果支持返回true,反之false。

         java.io包中的InputStream的类层次 (下图)

        OutputStream

 三个基本的写方法
 abstract void write(int b): 往输出流中写入一个字节
 void write(byte[] b):       往输出流中写入数组b中的所有字节
 void writte(byte[] b,int off,int len): 往输出流中写入数组b中从偏移量off开始的len个
                             字节的数据
 其它方法
 void flush(): 刷新输出流,强制缓冲区中的输出字节被写出
 void close(): 关闭输出流,释放和这个流相关的系统资源

        java.io包中OutputStream的类层次(如下图)



        基本的流类

 FileInputStream和FileOutputStream
 节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经
 存在,则覆盖这个文件。
 
 BufferedInputStream和BufferedOutputStream
 过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。

 DataInputStream和DataOutputStream
 过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的基本数据类型的功能。

 PipedInputStream和PipedOutputStream
 管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream
 对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。

code:
import java.io.*;
class StreamTest
{
 public static void main(String[] args)throws Exception
 {
  /*
  int data;
  while((data=System.in.read())!=-1)  //从标准设备读取数据
  {
   System.out.write(data);//从标准设备输出数据
  }
  */
  //输出流写数据,只需要关闭尾端的流就可以了,因为fos连接到了bos
  FileOutputStream fos = new FileOutputStream("1.txt");
  //fos.write("http://www.google.cn".getBytes());
  //fos.close();
  BufferedOutputStream bos = new BufferedOutputStream(fos);
  //bos.write("http//www.baidu.com".getBytes());
  //bos.flush();
  //bos.close();
  DataOutputStream dos=new DataOutputStream(bos); //连接到了bos和fis
  byte b=3;
  int i=78;
  char ch='a';
  float f=4.5f;
  dos.writeByte(b);
  dos.writeInt(i);
  dos.writeChar(ch);
  dos.writeFloat(f);
  dos.close(); //必须调用flush()或者close()不然不会写入硬盘
  
  //输入流读数据
  FileInputStream fis=new FileInputStream("1.txt");
  BufferedInputStream bis = new BufferedInputStream(fis);
  //byte[] buf=new byte[100];
  //int len=fis.read(buf);
  //int len=bis.read(buf);
  //System.out.println(new String(buf,0,len));
  //fis.close();
  //bis.close();
  //注意读取的顺序要和写的顺序一样
  DataInputStream dis = new DataInputStream(bis);
  System.out.println(dis.readByte());
  System.out.println(dis.readInt());
  System.out.println(dis.readChar());
  System.out.println(dis.readFloat());
  dis.close();  
  
 }
}


管道输入/输出流 code:
import java.io.*;
class PipedStreamTest
{
 public static void main(String[] args)
 {
  PipedOutputStream pos=new PipedOutputStream();
  PipedInputStream pis=new PipedInputStream();
  //连接
  try
  {
   pos.connect(pis);
   new Producer(pos).start();
   new Consumer(pis).start();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

class Producer extends Thread
{
 private PipedOutputStream pos;
 public Producer(PipedOutputStream pos)
 {
  this.pos=pos;
 }
 public void run()
 {
  try
  {
   pos.write("hello,welcome!".getBytes());
   pos.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

class Consumer extends Thread
{
 private PipedInputStream pis;
 Consumer(PipedInputStream pis)
 {
  this.pis=pis;
 }
 public void run()
 {
  try
  {
   byte[] buf=new byte[100];
   int len=pis.read(buf);
   System.out.println(new String(buf,0,len));
   pis.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

=================================================================================
              Java I/O库的设计原则

 Java的I/O库提供了一个称做链接的机制,可以将一个流与另一个流首尾相接,形成一个流管道的链接。
 这种机制实际上是一种被称做为Decorator(装饰)的设计模式的应用。
 
 通过流的链接,可以动态的增加流的功能,而这些功能的增加是通过组合一些流的基本功能而动
 态获取的。

 我们要获取一个I/O对象,往往需要产生多个I/O对象,这也是Java I/O库不大容易掌握的原因,
 但在I/O库中的Decorator模式的运用,给我们提供了实现上的灵活性。

 I/O流的链接图(如下)



               Reader和Writer

 Java程序语言使用Unicode来表示字符串和字符。
 Reader和Writer这两个抽象类主要用来读写字符流。

 java.io包中Reader的类层次(如下图)

 java.io包中Writer的类层次(如下图)


 code:
import java.io.*;
class StreamTest
{
 public static void main(String[] args)throws Exception
 {
  /*
  FileOutputStream fos = new FileOutputStream("1.txt");
  OutputStreamWriter osw = new OutputStreamWriter(fos); 
  BufferedWriter bw = new BufferedWriter(osw);
  
  bw.write("http://www.yahoo.com.cn");
  bw.close(); 
  
  FileInputStream fis = new FileInputStream("1.txt");
  InputStreamReader isr = new InputStreamReader(fis);
  BufferedReader br = new BufferedReader(isr);
  System.out.println(br.readLine());
  br.close();
  */
  //InputStreamReader/OutputStreamWriter是一个中间过度类,连接字符和字符串
  InputStreamReader isr = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(isr);
  String strLine;
  while((strLine=br.readLine())!=null)
  {
   System.out.println(strLine);
  }
  br.close();
 }
}

            字符集的编码

 ASCII(American Standard Code for Information Interchange,美国信息互换标准代码),是基
 于常用的英文字符的一套电脑编码系统。我们知道英文中经常使用的字符,数字符号被计算机
 处理时都是以二进制编码的形式出现(bit)二进制数对应。其最高位是0,相应的十进制数是0-127
 如,数字1,有一些制表符和其他符号组成。ASCII是现金最通用的单字节编码系统。

 GB2312: GB2312码是中华人民共和国国家汉字信息交换用编码,全称《信息交换用汉字编码字
 符集-基本集》。主要用于给每一个中文字符指定相应的数字,也就是进行编码。一个中文字符
 用两个字节的数字来表示,为了和ASCII码有所区别,将中文字符每一个字节的最高位置都用1
 来表示。

 GBK:为了对更多的字符进行编码,国家又发布了新的编码系统GBK(GBK的K是“扩展”的汉语
 拼音的第一个字母)。在新的编码系统里,除了完全兼容GB2312外,还对繁体中文,一些不常用
 的汉字和许多符号进行了编码。

 ISO-8859-1:是西方国家所使用的字符编码集,是一种单字节的字符集,而英文实际上只用了其
 中数字小于128的部分。

 Unicode: 这是一种通用的字符集,对所有语言的文字进行统一编码,对每一个字符都采用2个字节
 来表示,对于英文字符采取前面加“0”字节的策略实现等长兼容。如"a"的ASCII码为0x61,
 UNICODE就为0x00,0x61。

 UTF-8: Elight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,
 UCS是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节,如果
 字符是0x0000,或在0x0080与0x007f之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff
 之间,对应的UTF码是三个字节。

posted on 2006-06-11 14:18 蓝色Saga 阅读(694) 评论(1)  编辑  收藏 所属分类: Basic Study for JAVA

评论

# re: 基础学习之---Java I/O学习(一) 2007-07-23 16:10 99
Java I/O学习
http://iask.sina.com.cn/info/ztc-1047.shtml  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: