我的java培训日记

                                                  传智播客java培训日记-1

1. 同学沈龙的一个问题
他用javac(1.4)编译,用java(1.6)运行,出现不支持的类版本错误。我们用javac -version和java -version各看了一下版本,就发现了这个问题。然后查看path环境变量,发现他计算机上安装了oracle,oracle/bin排在了整个path的前面,调整path,让jdk1.6/bin排在最前面即可。
2. 由内存中一切皆二进制引出字符编码的讲解
大陆写的“中”到台湾显示不正常的原理分析,翻译软件的翻译原理,GBK-->“中”-->BIG5。讲解UTF-8编码和unicode编码,ASCII与Unicode码的转换规则,演示记事本显示字符时的趣味问题,分析乱码原因,GB2312码中也包含英文字符,如何识别GB2312码中的英文与汉字。最后课结束时讲了GB18030,如何支持?
3. xml 文档中设置encoding 属性的重要性
xml文档保存的编码类型和encoding指定的编码方式是两码事,前者表示xml文档内容的编码方式而后者是告诉解析器以什么编码方式来解析xml文档。二者必须一致,否则在浏览器中打开xml文档就会出现问题。默认情况下encoding属性的值是UTF-8。
沈继龙:以为指定了encoding="GBK",那文件就用GBK保存了,这正好理解反了。我是张,我戴顶帽子说明我是张,以便解析软件知道我是谁,如果我戴顶李的帽子,解析软件就认为我是李,显然就出了问题。
4. CDATA
要小心的是XML文档是通过一个程序生成出来的,如下所示的一个jsp程序,对于用变量生成的信息,
要特别注意加上CDATA区。
<%@page pageEncoding="GBK"%>
<%@page contentType="text/xml;charset=UTF-8"%>
<?xml version="1.0" encoding="UTF-8"?>
<country>
              <name>中国</name>
              <info><![CDATA[${param.info}]]></info>
</country>
这个JSP文件涉及了两个编码:源文件是用什么码写的,程序执行后以什么码输出结果。
为了加深理解处理软件必须文件中的字符编码的情况,又演示了javac编译utf-8源文件的问题。
与CDATA区类似的一个小技巧:
              <%@page pageEncoding="GBK"%>
              <form>
                     <input type="hidden" name="email" value="${param.email}"/>
                     请输入用户名:<input type="text" name="name" />
              </form>
如果给隐藏字段传递的参数是“zxx" a="b”,那么也会出问题,对于程序生成值时,一定要考虑值的特殊情况。
5. 编码转换
编码转换程序:
              char [] codes = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
              byte b = (byte)Integer.parseInt(args[0]);
              System.out.print(codes[((b>>4) & 0x0f)]);
              System.out.println(codes[(b & 0x0f)]);      
得到字符的big5码
<%@page pageEncoding="GBK"%>
<%@page contentType="text/html;charset=GBK"%>//修改contentType="audio/x-mp3"说明头的作用
<%
       String china="中一二";
       for(int i=0;i<china.length();i++)
       {
              out.println(Integer.toHexString((int)china.charAt(i)));
       }
       System.out.println();
       byte[] bufGBK = china.getBytes("GBK");
       for(int i=0;i<bufGBK.length;i++)
       {
              out.println(Integer.toHexString(bufGBK [i]));
       }
       byte[] buf = china.getBytes("Big5");
       for(int i=0;i<buf.length;i++)
       {
              out.println(Integer.toHexString(buf[i]));
       }
       byte [] buf ={0xa4, 0xa4,0xa4,0x40,0xa4,0x47}
%>
输出big5码给浏览器:
<%
       byte [] buf ={(byte)0xa4, (byte)0xa4,(byte)0xa4,(byte)0x40,(byte)0xa4,(byte)0x47};
       response.getOutputStream().write(buf);
%>
6. 区分中文英文的程序代码
buf[] = new byte[1024];
int len = System.in.read(buf);
boolean halfChinese = false;
for(int i=0;i<len;i++)
{
       if(halfChinese)
       {
              //buf[i-1] + buf[i]合并
              System.out.write('[');
              Systen.out.write(buf[i-1]);
              Systen.out.write(buf[i]);
              System.out.write(']');
              halfChinese = false;
              continue;
       }
       if((buf[i] & 0x80) == 0)
       {
              //为什么必须用write???
              System.out.write('[');
              Systen.out.write(buf[i]);
              System.out.write(']');
       }
       else
       {
              halfChinese = true;
              //等下一个,暂时不处理
              /*
              System.out.write('[');
              Systen.out.write(buf[i++]);
              Systen.out.write(buf[i]);
              System.out.write(']');
              */
       }
}
System.out.write();方法要用flush();刷新缓存,才能正常显示。
我写的代码,但还有地方没弄明白:为什么去掉了if()判断是否有'/r','/n'后,显示的结果不正常。
import java.io.*;
class TestInputChinese {
       public static void main(String[] args) {
              byte[] buf = new byte[1024];
              int len = 0;
              try {
                     len = System.in.read(buf);
              }catch(IOException e) {
                     e.printStackTrace();
              }
              change(buf, len);
       }
       public static void change(byte[] b, int len) {
              for( int i=0; i<len; i++ ) {
                     if(b[i] == '/r' || b[i] == '/n' ) {
                            continue;
                     }
                     if((b[i] & 0x80) == 0 ) {
                            System.out.write('[');
                            System.out.write(b[i]);
                            System.out.write(']');
                     } else {
                            System.out.write('[');
                            System.out.write(b[i++]);
                            System.out.write(b[i]);
                            System.out.write(']');
                     }
              }
              System.out.flush();
       }
}
 
详细查询: www.itcast.cn 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值