Java世界

          學習筆記

          常用鏈接

          統(tǒng)計

          積分與排名

          天籟村

          新華網(wǎng)

          雅虎

          最新評論

          集合與流

          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(參數(shù)1,參數(shù)2)
          Enumeration enum1 = hash.keys();
          Object obj;
          while(enum1.hasMoreElements()) {
          ??????obj = enum1.nextElement();
          ??????System.out.println(obj+":"+hash.get(obj));
          }


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


          可以通過FileInputStream和FileOutputStream來實現(xiàn)文件的拷貝。
          如:
          package com.sinojava.one;
          import java.io.*;
          /*
          ?? 文件拷貝,將一個文件中的內(nèi)容拷貝到另一個文件中
          */
          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字節(jié)
          ????????????????}
          ????????????????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("拷貝失敗!");
          ?????????????????}??
          ?????????????}
          ??????}


          緩沖流:對節(jié)點流封裝,起緩沖作用,提高讀寫效率。
          流的幾種經(jīng)典流向:
          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)


          編碼的轉換(中文出現(xiàn)亂碼現(xiàn)象)
          如:
          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");?? //給文件以字節(jié)的形式寫入字符串
          ????????????rf1.close();
          ????
          ????????????RandomAccessFile rf2 = new RandomAccessFile("c:/toAppend.txt","r");? //設置從toAppend.txt中讀內(nèi)容
          ????????????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);? //最后將讀出來的內(nèi)容打印到命令控制臺上
          ????????????}
          ????????????rf2.close();
          ????????????}catch(Exception e)
          ????????????{
          ???????????????????e.printStackTrace();?
          ????????????}??
          ??????}
          }

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


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 濉溪县| 淮南市| 台江县| 宁城县| 永兴县| 治县。| 囊谦县| 长海县| 镇坪县| 东乡| 武宁县| 黄冈市| 黔西县| 平凉市| 北京市| 徐水县| 武强县| 淮北市| 故城县| 云南省| 临澧县| 攀枝花市| 桓仁| 阳曲县| 咸宁市| 尚志市| 子长县| 镇江市| 遵义市| 临安市| 富民县| 德惠市| 临洮县| 富锦市| 桂阳县| 五华县| 邵阳市| 特克斯县| 抚远县| 亳州市| 通城县|