a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 106|回复: 1

[其他] JAVA技巧:j2me实现类似j2se中类Properties.java

[复制链接]
发表于 2012-8-4 12:28:23 | 显示全部楼层 |阅读模式
实现了J2SE的java.util.Properties类,可以用来篡夺内三军似下面这样的设置装备摆设文件:   ===============================================* k8 D3 y7 K* {0 ~2 K. X
  #这是注释" b0 F9 ~5 O& ]; h: O
  screen_width=240# D% `$ e, O4 w; \
  screen_height=238
6 G0 i9 X! T, F9 R  myname = rocks  M6 k- j* Y5 f: A9 [
  mybirth = u00d7u00f7u00d5u00dfu00c9u00fau00c8u00d5
2 E$ T6 b# H- U  i  ===============================================
) E. M) i- B9 m, R5 F  这样使用:9 W9 G* {; U1 B0 L  c! A. N+ w0 p
  Properties prop = new Properties();
( V$ L2 k9 t. q& @# J  InputStream is = this.getClass().getResourceAsStream("conf.prop"); prop.load(is); is.close(); String name = prop.getProperty("myname"); ...为了省空间和提高机能我把解析unicode的那部门注释起来了,如不美观你需冲要持中文(就像膳缦沔的mybirth那样的),把那些注释起来的代码恢复就可以了。; A7 {  F1 u0 A: |; R' e
  代码:) j* a* Q9 V8 X( O. z' d0 S/ ~0 ]
  --------------------------------------------------------------------------------( P2 H0 G/ Q+ q5 [& R: [" |" r
  package com.joyamigo.util; import java.io.*; import java.util.Hashtable; import java.util.Enumeration; public class Properties extends java.util.Hashtable {% d, U$ W* V/ P9 c7 \7 J0 C
  public Properties() {/ G+ d4 J% m$ x* }1 O
  super();( x* V5 V4 d, f3 p) Y: d
  }
7 [# p7 x$ l7 A  public String getProperty(String key) {
( B0 C8 s$ b& r8 ^. z9 u1 I, b$ |  return (String)this.get(key);4 i0 g) |6 x; h8 @
  }, m/ B1 J% g' e
  public String getProperty(String key, String defaultValue) { Object ret = this.get(key); return ret == null ? defaultValue : (String)ret;
/ K2 y( E# E# O+ M1 F# w. n7 @  }& S6 w7 h: n2 }" L5 Y6 l" I
  public Object setProperty(String key, String value) { return this.put(key, value);! |' y5 p: j6 K( W( c
  }
" D- _: ?8 ~8 J3 O  public void list(PrintStream out) {6 {5 a" ^6 c7 g! T8 s
  Enumeration e = this.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); Object value = this.get(key); out.println(key+"="+value);
& P; |3 P* B: s! h, _7 v# ~  }7 Q0 B  ~9 p) c. a
  }
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:28:24 | 显示全部楼层

JAVA技巧:j2me实现类似j2se中类Properties.java

  public void load(InputStream is) throws IOException {
% H1 T, S' s: ]2 D6 D5 k& k) U  this.load(is, "ISO8859_1");
$ f* ]1 ^+ v5 ~) \4 p$ {8 N  }, j- @, Z1 J8 F* y- D6 T8 |
  public void load(InputStream is, String enc) throws IOException { Reader reader = new InputStreamReader(is, enc);0 s* r# I& \$ F  ], Z; ]5 w
  boolean reading = true;1 [+ o  n; N9 R) W
  StringBuffer buf = new StringBuffer();
5 f9 ^( z3 R% _  R  while (reading) {
! N1 H# {  H( D: C1 o$ z  int c = reader.read();
) t+ v( ]8 P6 z- _$ b  switch (c) {
" r+ v0 Y" j; s2 o  case -1:6 k, b% Y: C0 o1 N* ?  R# [8 ^
  reading = false;) d+ A0 _* q+ r# S& N
  break;
) g2 l7 j% I; Z( I  case (int)'r':
- S' |4 s. J0 _5 f1 @1 ]. z3 \  break;
" C( [* J6 K+ L  case (int)'n':
" I- Y: z% w5 I1 H  Object[] pair = parseLine(buf.toString()); if (pair != null) { this.put(pair[0], pair[1]);* e) y; Q" ?9 N( B1 r# g7 v
  }* q2 Z* f5 z. z, j) U$ i/ c8 t. x
  buf.setLength(0);- G1 |, {$ X+ U+ S+ r* P* u
  break;4 q8 d% l1 y6 Q
  default:- N' a- L7 L9 F8 K. n
  buf.append((char)c);
! v! b! [8 D  j  }
3 x# o3 ?- C. f% H! u5 ~* _2 d( }  j  }# i2 N0 Y6 E- B* y- F
  Object[] pair = parseLine(buf.toString()); if (pair != null) { this.put(pair[0], pair[1]);
; o( w+ `0 X7 W2 z( B  }
7 |* V( e8 R: |- L  }
( p9 B6 N" `  R# |% {  public void store(OutputStream out, String header) throws IOException { this.store(out, "ISO8859_1", header);; F/ B8 s4 V, j6 \" a1 B
  }$ s$ f/ c7 k" Z/ W0 `$ m$ P+ S
  public void store(OutputStream out, String enc, String header) throws IOException { Writer writer = new OutputStreamWriter(out, enc); if (header != null) { writer.write("#"+header+"n");
- Q3 a$ F9 R! ~5 g* A4 \1 L" q% r  }
8 {# v1 \3 T; V  Enumeration e = this.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); String value = (String)this.get(key); //writer.write(key+"="+rconvUnicode(value)+"n"); writer.write(key+"="+value+"n");1 f6 q7 z: m. S# T1 _: }
  }5 u' X& a" |  U' E8 I
  }
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-21 14:00 , Processed in 0.158037 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表