posts - 1,  comments - 25,  trackbacks - 0




          對(duì)于我們常用的GBK中,英文是占用1個(gè)字節(jié),中文是2個(gè)

            對(duì)于UTF-8,英文是1個(gè),中文是3個(gè)

            對(duì)于Unicode,英文中文都是2個(gè)

            Java的流操作分為字節(jié)流和字符流兩種。

          2.3 Java 中相關(guān)實(shí)現(xiàn)方法

          字符串類 String 中的內(nèi)容是 UNICODE 字符串:

          // Java 代碼,直接寫中文
          String
           string = "中文123";

          // 得到長度為 5,因?yàn)槭?5 個(gè)字符
          System.out.println(string.length());

          字符串 I/O 操作,字符與字節(jié)轉(zhuǎn)換操作。在 Java 包 java.io.* 中,以“Stream”結(jié)尾的類一般是用來操作“字節(jié)串”的類,以“Reader”,“Writer”結(jié)尾的類一般是用來操作“字符串”的類。

          // 字符串與字節(jié)串間相互轉(zhuǎn)化

          // 按照 GB2312 得到字節(jié)(得到多字節(jié)字符串)

          byte
           [] bytes = string.getBytes("GB2312");

          // 從字節(jié)按照 GB2312 得到 UNICODE 字符串
          string = new String(bytes, "GB2312");

          // 要將 String 按照某種編碼寫入文本文件,有兩種方法:

          // 第一種辦法:用 Stream 類寫入已經(jīng)按照指定編碼轉(zhuǎn)化好的字節(jié)串

          OutputStream os = new FileOutputStream("1.txt");
          os.write(bytes);
          os.close();

          // 第二種辦法:構(gòu)造指定編碼的 Writer 來寫入字符串
          Writer ow = new OutputStreamWriter(new FileOutputStream("2.txt"), "GB2312");
          ow.write(string);
          ow.close();

          /* 最后得到的 1.txt 和 2.txt 都是 7 個(gè)字節(jié) */

          如果 java 的源程序編碼與當(dāng)前默認(rèn) ANSI 編碼不符,則在編譯的時(shí)候,需要指明一下源程序的編碼。比如:

          E:\>javac -encoding BIG5 Hello.java

          以上需要注意區(qū)分源程序的編碼與 I/O 操作的編碼,前者是在編譯時(shí)起作用,后者是在運(yùn)行時(shí)起作用。

          IO分兩種流 

          字節(jié)流 InputStream OutputStream 

          字符流 Reader Writer 

          他們都是抽象類 

          具體實(shí)現(xiàn) 
          字節(jié)流 FileInputStream FileOutputStream 
          字符流 FileReader FileWriter  

                 字符流處理的單元為2個(gè)字節(jié)的Unicode字符,分別操作字符、字符數(shù)組或字符串,而字節(jié)流處理單元為1個(gè)字節(jié), 操作字節(jié)和字節(jié)數(shù)組。所以字符流是由Java虛擬機(jī)將字節(jié)轉(zhuǎn)化為2個(gè)字節(jié)的Unicode字符為單位的字符而成的,所以它對(duì)多國語言支持性比較好!如果是 音頻文件、圖片、歌曲,就用字節(jié)流好點(diǎn),如果是關(guān)系到中文(文本)的,用字符流好點(diǎn). 
               所有文件的儲(chǔ)存是都是字節(jié)(byte)的儲(chǔ)存,在磁盤上保留的并不是文件的字符而是先把字符編碼成字節(jié),再儲(chǔ)存這些字節(jié)到磁盤。在讀取文件(特別是文本文件)時(shí),也是一個(gè)字節(jié)一個(gè)字節(jié)地讀取以形成字節(jié)序列. 
                字節(jié)流可用于任何類型的對(duì)象,包括二進(jìn)制對(duì)象,而字符流只能處理字符或者字符串; 2. 字節(jié)流提供了處理任何類型的IO操作的功能,但它不能直接處理Unicode字符,而字符流就可以。 


          字節(jié)流轉(zhuǎn)換成字符流可以用 InputSteamReader OutputStreamWriter 

          轉(zhuǎn)換成BufferdReader BufferedWriter 他們具有緩沖區(qū) 

          例如:讀取文件 從字節(jié)流輸入到字符流輸入 
          定義一個(gè)字節(jié)流: 
          FileInputStream fileInputStream = new FileInputStream("d:/text.txt"); // 定義一個(gè)指 

          向D:/TEXT.TXT 的字節(jié)流 

          InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); 
          //字節(jié)流轉(zhuǎn)換成InputStreamReader 
          BufferedReader bufferedReader = new BufferedReader(inputSteamReader); 
          //InputStreamReader 轉(zhuǎn)換成帶緩存的bufferedReader 

          可以把讀出來的內(nèi)容賦值給字符 

          String ss = new String(); 
          String s; 
          while((s = bufferedReader.readLine())!=null){ 
          ss += s; 


          例如:寫入文件 從字節(jié)流輸出到字符流輸出 

          FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt"); //定義一個(gè) 

          指向D:/TEXT.TXT文件 

          OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); 

          BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); 

          bufferedWriter.write(s); 

          bufferedWriter.close(); 
          outputStreamWriter.close(); 
          fileOutputStream.close(); 


          例程: 
               將字符串轉(zhuǎn)化為字節(jié)流#region 將字符串轉(zhuǎn)化為字節(jié)流 
                  /**//// <summary> 
                  /// 將字符串轉(zhuǎn)化為字節(jié)流 
                  /// </summary> 
                  /// <param name="_Source">字串</param> 
                  /// <returns>字節(jié)流</returns> 
                  public static byte[] String2Bytes(string strSource) 
                  { 
                      System.IO.MemoryStream   memoryStream=new   System.IO.MemoryStream();   
                      System.IO.BinaryWriter   binaryWriter=new   System.IO.BinaryWriter(memoryStream);   
                      binaryWriter.Write( strSource ); 
                      byte[]   buffer=memoryStream.GetBuffer(); 
                      return buffer;    
                  } 
                  #endregion 

                  
                  將字節(jié)流轉(zhuǎn)化為字符串#region 將字節(jié)流轉(zhuǎn)化為字符串 
                  /**//// <summary> 
                  /// 將字節(jié)流轉(zhuǎn)化為字符串 
                  /// </summary> 
                  /// <param name="bytData">字節(jié)流</param> 
                  /// <returns>字串</returns> 
                  public static string Bytes2String(byte[] bytData) 
                  { 
                      //字節(jié)流->字符串   
                      System.IO.MemoryStream   memoryStream2 = new   System.IO.MemoryStream(bytData);   
                      System.IO.BinaryReader   binaryReader = new   System.IO.BinaryReader(memoryStream2);   
                      string   s2=binaryReader.ReadString();   
                      return s2; 
                  } 
                  #endregion
          posted on 2010-08-16 17:16 Daniel 閱讀(1790) 評(píng)論(1)  編輯  收藏 所屬分類: CoreJava

          FeedBack:
          # re: Java的字符流和字節(jié)流
          2011-12-23 20:39 | mlzry0612
          留下備忘!
          文件拷貝:
          Java代碼
          public static void copyFile(File src,File dest) throws Exception{
          try {
          // Create channel on the source
          FileChannel srcChannel = new FileInputStream(src).getChannel();

          // Create channel on the destination
          FileChannel dstChannel = new FileOutputStream(dest).getChannel();

          // Copy file contents from source to destination
          dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

          // Close the channels
          srcChannel.close();
          dstChannel.close();
          } catch (IOException e) {
          }

          }

          文件重命名:
          Java代碼
          public static renameFile(File src,String newFilename)throws Exception{
          src.renameTo(new File(newFilename));//請(qǐng)寫明完整路徑
          }

          將文件讀成字符串:
          Java代碼
          public static String ReadFileToString(String pathAndFilename) {
          StringBuffer str = new StringBuffer();
          BufferedReader in = null;
          File inputFile = null;
          try {
          inputFile = new File(pathAndFilename);
          in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "utf-8"));
          String line = null;
          str = new StringBuffer((int) inputFile.length());
          while ((line = in.readLine()) != null) {
          str.append(line);
          }
          in.close();
          }
          catch (FileNotFoundException e2) {
          try {
          if (!new File(inputFile.getParent()).exists())
          new File(inputFile.getParent()).mkdirs();
          inputFile.createNewFile();
          }
          catch (IOException e) {
          e.printStackTrace();
          }
          }
          catch (IOException e3) {
          e3.printStackTrace();
          }
          return str.toString();
          }   回復(fù)  更多評(píng)論
            
          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          主站蜘蛛池模板: 诏安县| 绩溪县| 邹城市| 土默特右旗| 马关县| 无为县| 乌拉特后旗| 宿松县| 泗阳县| 夏河县| 宁明县| 江城| 唐河县| 繁峙县| 东明县| 旬邑县| 土默特左旗| 平果县| 卓资县| 武义县| 黎川县| 横峰县| 伊川县| 团风县| 淮阳县| 垫江县| 全州县| 稻城县| 璧山县| 兴国县| 怀宁县| 南开区| 全州县| 汾阳市| 禄丰县| 遂平县| 呼图壁县| 诸暨市| 措勤县| 平乡县| 桂东县|