wonderer's program

everything will be better
posts - 19, comments - 6, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2007年12月27日

去SA面试的时候,面试官问我平时用Java的什么数据结构,答曰:Vector。又问:哪有用过其他的的吗?例如List和Map之类的。答曰:甚少。(自己汗一个,没水平)既然不会就要学习啦。

翻开《Java学习笔记》,里面对对象容器的描述不错。

1. ArrayList和LinkedList

ArrayList使用了数组结构实现List的数据。所以ArraryList用来快速定位对象是非常有效率的。但是如果要对ArraryList中间插入或者删除,效率会非常低。

LinkedList使用链表来实现的List。所以跟ArrayList相反,LinkedList对于插入和删除是非常有优势,反之对于快速定位,是LinkedList的弱项。

1)ArrayListDemo

public class ArrayListDemo {
    public static void main(String[] args) {
        
        //用Scanner类,可以轻松获得commander的输入
        Scanner scanner = new Scanner(System.in);
        
        List<String> list = new ArrayList<String>();
        
        //在控制台输入,quit退出
        while(true) {
            System.out.print("Rokey@console# ");
            String input = scanner.next();
            if(input.equals("quit")) {
                break;
            }
            list.add(input);
        }
        
        System.out.print("显示输入:");
        
        //使用5.0的foreach功能对List进行遍历
        for(String s:list) {
            //5.0的类C的输出格式
            System.out.printf("%s ",s);
        }
    }
}

输出:

Rokey@console# 一二三
Rokey@console# 三二一
Rokey@console# quit
显示输入:一二三 三二一 

 

2)用LinkedList实现的一个字符串栈

/**
 *
 * @author Rokey
 * 用LinkedList构建一个字符栈,先进先出
 */
public class StringStack {

    private LinkedList<String> linkList;

    public StringStack() {
        linkList = new LinkedList<String>();
    }

    public void push(String s) {
        //将元素加入链表第一个位置
        linkList.addFirst(s);
    }

    public String pop() {
        //删除链表第一个元素,并返回
        return linkList.removeFirst();
    }

    public String top() {
        //返回链表第一个元素,但并不删除
        return linkList.getFirst();
    }

    public boolean isEmpty() {
        //检查链表是否为空
        return linkList.isEmpty();
    }
}
public class StringStackDemo {

    public static void main(String[] args) {

        //用Scanner类,可以轻松获得commander的输入
        Scanner scanner = new Scanner(System.in);

        StringStack stack = new StringStack();

        //在控制台输入,quit退出
        while (true) {
            System.out.print("Rokey@console# ");
            String input = scanner.next();
            if (input.equals("quit")) {
                break;
            }
            stack.push(input);
        }

        System.out.print("显示输入:");
        //使用5.0的foreach功能对List进行遍历
        
        while(!stack.isEmpty()) {
            //5.0的类C的输出格式
            System.out.printf("%s ", stack.pop());
        }
    }
}

输出:

Rokey@console# 一二三
Rokey@console# 三二一
Rokey@console# quit
显示输入:三二一 一二三 

posted @ 2007-12-27 23:05 wonderer 阅读(3057) | 评论 (0)编辑 收藏

2007年12月23日

OYM中的任务中,有一项对文件内容的检查挺有意思的,就是要检查字符是否是全角的,例如“GY”(not“GY”),并且把这些字符改为半角的。
想起了在研发中心的一个朋友的抱怨:“昨天写了一整天的程序,发到广大教务处那边居然说不能用,然后亲自跑了一躺,发现不是我的程序有问题,是那边的人输入个全角字符,搜半角的字符,当然不行了”
恩,Betty写的需求真有意思,考虑的问题很周全,是一个很厉害的项目经理。如果从输入这里解决了字符是否是半角的,那么,以后的情况就容易解决很多了。恩,网上搜了一下资料,查了一下书,得出了以下代码:
public void testChar() {
  String s1 
= "123";
  String s2 
= "abc";
  String s3 
= "123abc";
  System.out.println(s1);
  System.out.println(s2);
  System.out.println(s3);
  
for (int i = 0; i < s1.length(); i++) {
   
int j = s1.charAt(i);
   
if (j > 256) {
    
int temp = j - 65248;
    
if (temp >= 0) {
     System.out.print((
char)j+"-->:" + (char) temp);
    } 
else {
      System.out.print((
char) j);
    }
   } 
else {
    System.out.print((
char) j);
   }
  }
  System.out.println();
  
  
for (int i = 0; i < s2.length(); i++) {
   
int j = s2.charAt(i);
   
if (j > 256) {
    
int temp = j - 65248;
    
if (temp >= 0) {
     System.out.print((
char)j+"-->:" + (char) temp);
    } 
else {
     System.out.print((
char) j);
    }
   } 
else {
    System.out.print ((
char) j);
   }
  }
  System.out.println();
  
  
for (int i = 0; i < s3.length(); i++) {
   
int j = s3.charAt(i);
   
if (j > 256) {
    
int temp = j - 65248;
    
if (temp >= 0) {
      System.out.print((
char)j+"-->:" + (char) temp);
    } 
else {
     System.out.print((
char) j);
    }
   } 
else {
    System.out.print((
char) j);
   }
  }
  System.out.println();
 
 }
输出的结果如下:
123
-->ab-->bc--c
123a
-->ab-->bc--c


posted @ 2007-12-23 16:46 wonderer 阅读(1903) | 评论 (3)编辑 收藏

OYM的任务中,有个要求,上传一个Excel文件,检查他的内容是否合法,并返回信息。

今天想了一下,第一个要解决的问题就是上传一个Excel文件,上传文件的组件到挺多的,网上一搜,就有一大堆教程,但是现在并不是要上传一个文件到服务器以作存储之用,而是要上传一个文件到内存里,以Java的数据结构存储起来,并检查,把合乎要求的数据写到数据库里。所以在网上的一大堆上传文件的组件并不合用。于是又想自己写,思路就是从客户端那里获取一个InputStream,然后就对这个InputStream做一系列的检查。代码如下:

ServletInputStream sis =  request.getInputStream();
InputStreamReader isr = new InputStreamReader(sis);
             
int ch;
while((ch = isr.read()) != -1 ) {          
   out.println((char)ch);
}
             
System.out.flush();

结果的出去就是如下(输出东西写到页面):

-----------------------------7d7ea23120550 
Content-Disposition: form-data; name="file1"; 
filename="C:\Documents and Settings\Administrator\桌面\test.txt" 
Content-Type: text/plain 
my name is Rokey.Rokey。我的名字叫Rokey. 
-----------------------------7d7ea23120550 Content-Disposition: form-data; 
name="Submit" 上传 -----------------------------7d7ea23120550--
很明显,这里只有
my name is Rokey.Rokey。我的名字叫Rokey.

对我有用,这个也正是我的文件里面的内容,其它的都是关于这些form的其它信息。对我这个程序是没有用的。如果这里写下去的话,还要我去分析那些是数据,哪些是form的参数。好,到现在为止,我已经打消了自己写的念头了。我想,那些组件都可以把上传文件封装得那么好,能不能利用那些库,抽出文件的IO流,让我操作呢?

于是,就开始对O'Reilly的上传组件cos.jar的API看,看到里面有这么一段。

public class MultipartParser
extends java.lang.Object
A utility class to handle multipart/form-data requests, the kind of requests that support file uploads. This class uses a "pull" model where the reading of incoming files and parameters is controlled by the client code, which allows incoming files to be stored into any OutputStream. If you wish to use an API which resembles HttpServletRequest, use the "push" model MultipartRequest instead. It's an easy-to-use wrapper around this class.

This class can receive arbitrarily large files (up to an artificial limit you can set), and fairly efficiently too. It cannot handle nested data (multipart content within multipart content). It can now with the latest release handle internationalized content (such as non Latin-1 filenames).

It also optionally includes enhanced buffering and Content-Length limitation. Buffering is only required if your servlet container is poorly implemented (many are, including Tomcat 3.2), but it is generally recommended because it will make a slow servlet container a lot faster, and will only make a fast servlet container a little slower. Content-Length limiting is usually only required if you find that your servlet is hanging trying to read the input stram from the POST, and it is similarly recommended because it only has a minimal impact on performance.

而且里面的API已经封装程我想象得到的情况了。于是,我就觉得这样我就可以完成我的功能了。于是,就写了以下代码:

MultipartParser mp = new MultipartParser(request, 10 * 1024 * 1024);
Part part;
while ((part = mp.readNextPart()) != null) {
      if (part.isParam()) {
          // it's a parameter part
          ParamPart paramPart = (ParamPart) part;
          //out.println("param: name=" + name + "; value=" + value);
      } else if (part.isFile()) {
          FilePart filePart = (FilePart) part;
          InputStream is = filePart.getInputStream();
          InputStreamReader isr = new InputStreamReader(is);

          int ch;
          while ((ch = isr.read()) != -1) {

              out.print((char) ch);
          }

          System.out.flush();
          isr.close();
          is.close();
      }
}
               

出去结果如下:

my name is Rokey.Rokey。
我的名字叫Rokey.
到现在,已经可以把这个流封装成一个文件流,送给Excel的组件去处理了。

posted @ 2007-12-23 00:52 wonderer 阅读(1431) | 评论 (0)编辑 收藏

2007年10月28日

     摘要: 什么是IOC呢,在网上搜到了一非常有意思的讲解。IoC就是Inversion of Control,控制反转。在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制。这称为控制反转。 下面我们以几个例子来说明什么是IoC 假设我们要设计一个Girl和一个Boy类,其中Girl有kiss方法,即Girl想要Kiss一个Boy。那么,我们的问题是,Girl如何能够认识这个B...  阅读全文

posted @ 2007-10-28 16:50 wonderer 阅读(665) | 评论 (0)编辑 收藏

2007年10月26日

Buffloa里的传递参数的编码是GBK。

buffalo.switchPart('body',url,false);如果url中包含汉字,是采用GBK编码的。在不改变tomcat的配置文件的情况下,在目标页面里获得url参数的正确方法是

   1: String name = new String(request.getParameter("name").getBytes(
   2:             "ISO8859-1"), "GBK");

注意,如果这里用utf-8作为编码的转换的话,会出现乱码。

posted @ 2007-10-26 16:41 wonderer 阅读(411) | 评论 (0)编辑 收藏

2007年10月25日

最近在准备考试系统的开发,碰到了 request.getParameter乱码的问题。跟林彬讨论了一下,还是觉得用老方法管用。

如果是post的话,可以通过设置filter的方法来解决。

如果是get或者是超链接的话,以前是通过设置tomcat的配置文件server.xml来解决的,但这样不好,并不是所有的项目,我们都可以修改到服务器的tomcat的配置文件。具体代码如下:

   1: Connector port="8080" maxHttpHeaderSize="8192"
   2:                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
   3:                enableLookups="false" redirectPort="8443" acceptCount="100"
   4:                connectionTimeout="20000" disableUploadTimeout="true" uRIEncoding="gbk"/>

还是觉得老方法管用,只是有点麻烦:

   1: String id=new String(request.getParameter("id").getBytes("ISO8859-1"),"UTF-8");
   2: String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");

posted @ 2007-10-25 23:23 wonderer 阅读(13414) | 评论 (1)编辑 收藏

2007年9月11日

电脑搬回了宿舍,破解了校园网,多人公用一条宽带。要一个主机拨号。ip总不免要设来设去,总是要手工改,很麻烦,于是上网查了查,写了个改ip的bat文件。内容如下:

1: netsh interface ip set address name="本地连接" source=static addr=192.168.0.39

mask=255.255.255.0 gateway=192.168.0.1 gwmetric=1

   2: netsh interface ip set dns name = "本地连接" source = static addr = 202.116.128.1
   3: netsh interface ip add dns name = "本地连接" addr = 202.116.128.2

posted @ 2007-09-11 17:25 wonderer 阅读(2047) | 评论 (1)编辑 收藏

2007年8月3日

在写HTML中,并不是&nbsp才会产生一个空格。<td>hello (间隔一个空格)</td>输出的数据是: hello+一个空格.如果是对这数据进行修改然后再写回到数据库的话,这样就会产生错误。

如下写法是会错误的,

image

造成的结果是 image  仔细留意会发现运通后面多了一个空格

必须改成一下写法:

image 

注意</td>跟前面是没有空格的。这样运行结果就会是这样的image ,是没有空格的。

posted @ 2007-08-03 10:32 wonderer 阅读(651) | 评论 (0)编辑 收藏

2007年7月18日

     摘要: 写了个Spring的DAO入门例子。 DAO的接口 1: package dataSourceDemo; 2:   3: public interface IUserDAO { 4: public void insert(User user); 5: public User find(Integer id); 6:   7: } ...  阅读全文

posted @ 2007-07-18 14:04 wonderer 阅读(480) | 评论 (0)编辑 收藏

首先要导入包

1:Spring支持包:spring.jar , commons-logging.jar

2: JUnit支持包: JUnit.jar

image

建立Bean类,

   1: package refBeanDemo;
   2:  
   3: import java.util.Date;
   4:  
   5: public class HelloBean {
   6:     private String helloWorld;
   7:     private Date date;
   8:     public Date getDate() {
   9:         return date;
  10:     }
  11:     public void setDate(Date date) {
  12:         this.date = date;
  13:     }
  14:     public String getHelloWorld() {
  15:         return helloWorld;
  16:     }
  17:     public void setHelloWorld(String helloWorld) {
  18:         this.helloWorld = helloWorld;
  19:     }
  20:     
  21: }

 

建立配置文件,和在里面进行注入

   1: <?xml version="1.0" encoding="UTF-8"?>
   2: <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "../resources/spring-beans-2.0.dtd" >
   3: <beans>
   4:     <bean id="dateBean" class="java.util.Date"></bean>
   5:     
   6:     <bean id="helloBean" class="refBeanDemo.HelloBean">
   7:         <property name="helloWorld">
   8:             <value>你好,世界</value>
   9:         </property>
  10:         <property name="date" ref="dateBean"></property>
  11:     </bean>
  12: </beans>

写JUnit进行测试,方便管理,把JUnit的东东放到test包里。

   1: package refBeanDemo;
   2:  
   3: import org.springframework.context.ApplicationContext;
   4: import org.springframework.context.support.ClassPathXmlApplicationContext;
   5:  
   6: import junit.framework.TestCase;
   7:  
   8: public class TestRefBeanDemo extends TestCase {
   9:     private ApplicationContext context;
  10:  
  11:     public void setUp() {
  12:         context = new ClassPathXmlApplicationContext("refBeanDemo/NewFile.xml");
  13:     }
  14:  
  15:     public void testSpring() {
  16:         HelloBean helloBean = (HelloBean)context.getBean("helloBean");
  17:         System.out.println(helloBean.getDate());
  18:         assertEquals("你好,世界", helloBean.getHelloWorld());
  19:         
  20:     }
  21: }

 

运行JUnit测试

image

测试成功。类的分布如下:

image

image

posted @ 2007-07-18 11:12 wonderer 阅读(1340) | 评论 (0)编辑 收藏