Java世界

          學習筆記

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          集合與流

          Collection(集合) API
          Set:一個無序無重復;List:一個有序可重復。

          Iterators迭代器
          如:
          List list = new ArrayList();
          Iterators elements = list.iterator();
          while(elements.hasNext()) {
          ??????System.out.println(elements.next());
          }
          Vector向量
          轉換成枚舉類型
          Enumeration enum1 = p.elements();
          while(enum1.hasMoreElements()) {
          ??????name = (String)enum1.nextElement();
          ??????System.out.println(name);
          }
          Hashtable 放入“鍵-值”關系對象。
          put(參數1,參數2)
          Enumeration enum1 = hash.keys();
          Object obj;
          while(enum1.hasMoreElements()) {
          ??????obj = enum1.nextElement();
          ??????System.out.println(obj+":"+hash.get(obj));
          }


          Java流
          輸入/輸出源抽象為“流”,16位字符流。
          字節流:InputStream和OutputStream。
          字符流:Reader和Writer。
          數據類型分節點流(低級流)和處理流(高級流),高級流和低級流聯系起來使用,低級流和輸入/輸出設備關聯。
          System.exit(0)是正常退出,System.exit(1)是非正常退出。


          可以通過FileInputStream和FileOutputStream來實現文件的拷貝。
          如:
          package com.sinojava.one;
          import java.io.*;
          /*
          ?? 文件拷貝,將一個文件中的內容拷貝到另一個文件中
          */
          public class CopyFile
          {
          ???????public boolean copyFile(String src,String des)
          ???????{?
          ???????????File srcFile,desFile;
          ???????????srcFile = new File(src);
          ???????????desFile = new File(des);
          ???????????FileInputStream fis = null;
          ???????????FileOutputStream fos = null;
          ?
          ???????????try{
          ????????????????desFile.createNewFile();
          ????????????????fis = new FileInputStream(srcFile);
          ????????????????fos = new FileOutputStream(desFile);
          ????????????????int bytesRead;
          ????????????????byte[] buf = new byte[4 * 1024];? // 4K buffer
          ????????????????while((bytesRead=fis.read(buf))!=-1){
          ?????????????????????fos.write(buf,0,bytesRead);? //0到bytesRead字節
          ????????????????}
          ????????????????fos.flush();
          ????????????????fos.close();
          ????????????????fis.close();
          ??????????? }catch(IOException e){
          ???????????????????System.out.println(e);
          ???????????????????return false;
          ??????????????}
          ?????????????return true;?
          ??????????}?
          ??????????public static void main(String args[])
          ??????????{
          ?????????????????CopyFile cf = new CopyFile();
          ?????????????????String src = args[0];
          ?????????????????String des = args[1];
          ?????????????????if(cf.copyFile(src,des))
          ?????????????????{
          ????????????????????????System.out.println("拷貝成功!");
          ?????????????????}
          ?????????????????else
          ?????????????????{
          ????????????????????????System.out.println("拷貝失?。?);
          ?????????????????}??
          ?????????????}
          ??????}


          緩沖流:對節點流封裝,起緩沖作用,提高讀寫效率。
          流的幾種經典流向:
          A-file---(bytes)FileInputStream---(bytes)DataInputStream---(String) .
          A-file---FileInputStream---BufferedInputStream---DataInputStream .
          A-file---FileReader---BufferedReader .


          重定向標準輸入/輸出,使用System.setIn()和System.setOut()來改變設備。


          RandomAccessFile 特殊文件流
          它的接口是Data Input 和 Data Output。
          有兩個構造方法:
          RandomAccessFile(File file,String mode)
          RandomAccessFile(String file,String mode)


          編碼的轉換(中文出現亂碼現象)
          如:
          package com.sinojava.one;
          import java.io.*;
          public class AppendFile
          {
          ??? public static void main(String[] args) {
          ??????????? String toAppend = args[0];?? //從命令行隨意輸入字符
          ??????????? try{
          ????????????int i = 0;
          ????????????String record = new String();
          ????????????String toCn = null;
          ????????????//處理中文問題
          ????????????toCn = new String(toAppend.getBytes("GBK"),"ISO8859_1"); //將其轉換為ISO8859_1格式
          ??????
          ????????????RandomAccessFile rf1?
          ????????????= new RandomAccessFile("c:/toAppend.txt","rw");?? //rw是固定寫法,表示讀寫操作
          ????????????rf1.seek(rf1.length());? //返回的是文件的長度
          ????????????rf1.writeBytes(toCn+"\n");?? //給文件以字節的形式寫入字符串
          ????????????rf1.close();
          ????
          ????????????RandomAccessFile rf2 = new RandomAccessFile("c:/toAppend.txt","r");? //設置從toAppend.txt中讀內容
          ????????????String outCn = null;
          ????????????while ((record = rf2.readLine()) != null)?
          ????????????{
          ????????????????????i ++;
          ????????????????????//處理中文問題
          ????????????????????outCn = new String(record.getBytes("ISO8859_1"),"GBK");??????????????????????????????????????????????????????????????????????//把文件中的ISO8859_1編碼格式轉換為GBK格式
          ????????
          ????????????????????System.out.println("Line "+i+":"+outCn);? //最后將讀出來的內容打印到命令控制臺上
          ????????????}
          ????????????rf2.close();
          ????????????}catch(Exception e)
          ????????????{
          ???????????????????e.printStackTrace();?
          ????????????}??
          ??????}
          }

          posted on 2007-10-25 17:04 Rabbit 閱讀(250) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 兴安盟| 威远县| 沾化县| 东明县| 土默特右旗| 台安县| 黑山县| 集贤县| 剑川县| 大渡口区| 阳谷县| 安阳县| 红河县| 枞阳县| 安化县| 东阿县| 贵溪市| 清丰县| 大冶市| 德安县| 兴隆县| 台北县| 都兰县| 甘泉县| 新郑市| 行唐县| 化州市| 桂阳县| 游戏| 利川市| 大名县| 南川市| 柳林县| 噶尔县| 宝山区| 余干县| 泸西县| 中卫市| 全椒县| 汝州市| 大渡口区|