17编写字符界面应用(上)_第1页
17编写字符界面应用(上)_第2页
17编写字符界面应用(上)_第3页
17编写字符界面应用(上)_第4页
17编写字符界面应用(上)_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、编写字符界面应用(上) 命令行参数 常用系统属性 Properties类 System类中和属性有关的操作 从属性文件中读取属性 标准输入输出命令行参数 在windows下,通过java.exe可执行程序来运行Java程序,格式如下 java ClassName para_list 在启动Java应用程序时可以一次性地向应用程序中传递0多个参数-命令行参数; 命令行参数通过public static void main(String args)中的main方法接收命令行参数例子(示例9-1)public class ConsoleParamspublic static void main(St

2、ring args)if(args.length!=2)System.out.println(请按下列方式执行:java ConsoleParams 参数1 参数2);System.exit(0);String param1 = args0;String param2 = args1;System.out.print(你好,+param1+,你今年);System.out.println(2004-Integer.parseInt(param2)+岁); 系统属性 Java中系统属性就是Java的环境变量 System.getProperties()方法会返回系统属性值。 System.get

3、Property()方法返回一个String来代表系统属性。 在命令行中可用java D来加入一个系统属性Properties类 Properties类实现了从名字到值的映射 propertyNames()方法返回一个包含所有属性名的Enumeration对象 getProperty()方法返回一个代表该属性值的字符串 使用load()或store()方法能从文件读入属性集或将属性集写入文件 Properties在java.util包中系统属性例子(示例9-2)import java.util.Properties;import java.util.Enumeration;public cla

4、ss TestProperties public static void main(String args) Properties props = System.getProperties(); Enumeration prop_names = pertyNames(); while ( prop_names.hasMoreElements() ) String prop_name = (String) prop_names.nextElement(); String property = props.getProperty(prop_name); System.out.pr

5、intln(property + prop_name + is + property + ); 从文件中读取属性的例子(示例9-3)oracle_url=jdbc:oracle:thin:localhost:1521:O920oracle_name = O920oracle_user = scottoracle_pwd= tigerfile_path=c:cctvfilesvirtual_path=examples/从文件重读取属性的例子(con.)import java.util.*;import java.io.*;public class ReadProprivate String or

6、acle_url,oracle_name,oracle_user,oracle_pwd;private String file_path,virtual_path;public ReadPro()tryProperties props = new Properties();File f=new File(C:OracleSperties);FileInputStream in = new FileInputStream(f);props.load(in);in.close();oracle_url = props.getProperty(oracle_url);. .catch

7、(IOException e)System.out.println(e);. .控制台输入/输出 System.out可向标准输出设备输出 它是一个PrintStream对象 System.in可从标准的输入设备输入 它是一个InputStream对象 System.err可向标准的错误设备输出 它是一个PrintStream对象从键盘输入例子(示例9-4)import java.io.*;public class KeyboardInput public static void main (String args) String s; /创建一个BufferedReader对象从键盘逐行读入

8、数据 InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir); System.out.println(Unix: Type ctrl-d or ctrl-c to exit. + nWindows: Type ctrl-c to exit.); try / 每读入一行,向标准输出设备输出 while (s = in.readLine() != null) System.out.println(Read: + s); / 关闭流,这步动作在对流的操作完成

9、后一定要做。 in.close(); catch (IOException e) / Catch any IO exceptions. e.printStackTrace(); 向标准设备输出 使用System.out.println/System.out.print两个常用的方法向标准设备输出 println()方法将参数打印出来,并加上”n”字符。 print()方法,打印参数,但不加新行 print和println方法对多数简单数据类型进行了重载(boolean, char, int, long, float, double)和char, Object以及String print(Object)或println(Object)将会调用该对象的toString()方法,打印它的返回字符串向标准设备输出例子(示例9-5)public class Echopublic static void main(String args)int a = 100;boolean b = true;System.out.print(echo an int primitive type data:);System.out.println(a);Sys

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论