45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:IO笔记的知识大全

IO笔记的知识大全

2016-08-24 17:10:25 来源:www.45fan.com 【

IO笔记的知识大全

1System
1.1I/O流
l publicstaticInputStreamin:读取字符的标准输入流。
l publicstaticPrintStreamout:标准输出流。
l publicstaticPrintStreamerr:标准错误输出流。
1.2 系统属性
(1)属性
l java.version版本号java.vendor销售商
l java.vendor.urljava.home安装目录
l java.class.version类版本java.class.path类路径
l os.nameos.arch支持的体系结构
l os.versionfile.separator
l path.separatorline.separator行分隔符
l user.nameuser.homehome目录
l user.dir用户当前目录
(2)方法
l publicstaticPropertiesgetProperties():取系统属性。如果当前系统属性集不存在,则创建并初始化。
l publicstaticvoidsetProperties(Propertiesprops)
l publicstaticStringgetProperty(Stringkey)
返回名为key的属性值。等效于System.getProperties().getProperty(key);
l publicstaticStringgetProperty(Stringkey,StringdefaultValue)
返回名为key的属性值。若没定义则返回defaultValue。它等效于System.getProperties().getProperty(key,def);

(3) 属性值的解码属性值转换成数值(对象)
l staticbooleanBoolean.getBoolean(Stringname)
name为”true”(不区分大小写)时,返回true,否则false
l staticIntegerInteger.getInteger(Stringname)
如果没有数字形式,返回null。
l staticIntegerInteger.getInteger(Stringname,Integerdef):如果没有数字形式,返回缺省值def
l staticLongLong.getLong(Stringnm)
l staticLongLong.getLong(Stringnm,Longdef)
(4)Eample
publicstaticFilepersonal(StringfileName){
Stringhome=System.getProperty("user.home");
if(home==null) returnnull;
else returnnewFile(home,fileName);
}
1.3安全性
(1) SecurityManager
l 抽象类,可实现各种安全策略
l 提供各种check方法控制是否能打开网络接口、是否允许文件存娶线程创建等
(2)System与安全性
l staticvoidsetSecurityManager(SecurityManagers)
置系统安全管理对象。此值只能置一次。以后无论谁启动系统安全机制都将取决于先前置的值而不能改变它。
l publicstaticSecurityManagergetSecurityManager()
1.4杂项
l publicstaticlongcurrentTimeMillis()
返回以微秒为单位的GMT时间(按UTC标准从1970年1月1日的00:00:00开始计时)。在292280995年前它不会溢出.如复杂情况可用Date类。
l publicstaticvoidarraycopy(Objectsrc,intsrcPos,Objectdst,intdstPos,intcount)
将源数组中src[srcPos]开始的count个元素拷贝到目的数组中以dst[dstPos]开始的相应元素中。

2Runtime&Process
2.1Runtime
l 每个应用程序对应一个Runtime实例,与运行环境相接
l 用getRuntime()方法获得:staticRuntimegetRuntime()
l 应用程序无法创建自己的Runtime类实例
2.2内存管理
(1)有关方法
l publiclongfreeMemory():返回可用内存字节数。
l publiclongtotalMemory():返回总的内存字节数。
l voidgc():使java虚拟机回收无用对象所占内存。
l voidrunFinalization():使java虚拟机调用对象的finalize()释放资源,如文件等
(2)Example
publicstaticvoidfullGC(){
Runtimert=Runtime.getRuntime();
longisFree=rt.freeMemory();
longwasFree;
do{ wasFree=isFree;
rt.gc();
isFree=rt.freeMemory();
}while(isFree>wasFree);
rt.runFinalization();
}

2.3进程管理
(1)创建进程的方法(Runtime)
l Processexec(Stringcommand)throwsIOException
各个参数都在字符串command中,用空格隔开。如:
Stringcmd="/bin/ls"+opts+""+dir;
Processchild=Runtime.getRuntime.exec(cmd);
l publicProcessexec(Stringcommand,Stringenvp[])
envp:环境数组,格式为name=value,名中不能有空格
l publicProcessexec(Stringcmdarray[])
cmdarray[0]中的字符串是命令名,其他为命令行参数。
l publicProcessexec(Stringcmdarray,Stringenvp[])

新创建的进程称为子进程。反之,创建进程者称为父进程。
由exec为每个创建的子进程返回一个Process对象。
l 注意:对Process实例的引用赋null值并不能杀死子进程。子进程可以与已存的Java进程同步执行。

(2)Process的输入输出(Process中)
l publicabstractOutputStringgetOutputString()
返回一个连接到子进程输入的OutputString,写入到这个流的数据子进程作为输入读入。该流是缓冲的。
l publicabstractInputStringgetInputString()
返回一个连接到子进程输出的InputString,当子进程在输出端写数据时,可从这个流读取数据。该流是缓冲的。
l publicabstractInputStringgetErrorString()
返回一个连接到子进程错误输出的InputString,当子进程在它的错误输出端写数据时,可从这个流读取数据。该流不是缓冲的,这样就保证错误能立即报告。
l Example
publicstaticProcessuserProg(Stringcmd)throwsIOException{
Processproc=Runtime.getRuntime().exec(cmd);
plugTogether(System.in,proc.getOutputString());
plugTogether(System.out,proc.getInputString());
plugTogether(System.err,proc.getErrorString());
returnproc;
}

(3)Process的控制(Process中)
l abstractintwaitFor()throwsInterruptedException
不确定的等待进程结束,返回它送给System.exit或等价值(通常0成功,非0失败)。若进程已终止,直接返回值。
l publicabstractintexitValue()
返回进程退出值。若尚没结束,IllegalStateException。
l publicabstractvoiddestroy()
杀死进程。若进程已完成,则什么也不做。作为废区收集了的Process对象本身并没消亡而仅是不能用于处理。

l Example
//Wehaveimportjava.io.*andjava.util.*
publicString[]ls(Stringdir,Stringopts)
throwsLSFailedException{
try{ //startupthecommand
String[]cmdArray={"/bin/ls",opts,dir}:
Processchild=Runtime.getRuntime().exec(cmdArray);
DataInputStream
in=newDateInputStream(child.getInputStream());
//readthecommand'soutput
Vectorlines=newVector();
Stringline;
while((line=in.readLine())!=null)
lines.addElement(line);
if(child.waitFor()!=0)//ifthelsfailed
throwsnewLSFailedException(child.exitValue());
String[]retval=newString[lines.size()];
lines.copyInfo(retval);
returnretval;
}catch(LSFailedExceptione){
throwe;
}catch(Exceptione){
thrownewLSFailedException(e.toString());
}
}

(4)voidexit(intstatus):(Runtime/System中)终止当前运行的java虚拟机,状态码0成功完成任务,非0失败。
l Example:杀死当前组中所有线程,然后调用exit
publicstaticvoidsafeExit(intstatus){
//Getthelistofallthreads
ThreadmyThrd=Thread.currentThread();
ThreadGroupthisGroup=myThrd.getThreadGroup();
intcount=thisGroup.activeCount();
Thread[]thrds=newThread[count+20];
thisGroup.enumerate(thrds);
for(inti=0;i<thrds.length;i++){//stopallthreads
if(thrds[i]!=null&&thrds[i]!=myThrd)
thrds[i].stop();
}
for(inti=0;i<thrds.length;i++){//waitforallthreadstocomplete
if(thrds[i]!=null&&thrds[i]!=myThrd{
try{
thrds[i].join();//等待线程进入死亡状态
}catch(InterruptedExceptione){
//justskipthisthread
}
}
}
System.exit(status);//System.exit()callsRuntime.exit()
}

2.4流的本地化
l InputStreamgetLocalizedInputStream(InputStreamin)
l OutputStreamgetLocalizedOutputStream(OutputStreamo)
如键盘产生8位Oriya字符码,用System.in输入时Oriya字符被转换成/u0b00至/u0b7f的16位Unicode。本地化的输出流做反向的转换。

2.5杂项
traceInstructions/traceMethodCalls,布尔型参数为true则打开指令跟踪或方法调用跟踪(打印执行细节),为false则关闭。

3Math
Math类由一组静态常数和方法组成,所有运算都以double进行。
Math.E代表e,Math.PI代表π。角度用弧度制
功能 含义
sin(a) a的正弦
cos(a) a的余弦
tan(a) a的正切
asin(v) v的反正弦,v的范围[-1.0,1.0]
acos(v) v的反余弦,v的范围[-1.0,1.0]
atan(v) v的反正切,返回的范围[-π/2,π/2]
atan2(x,y) x/y的反正切,返回的范围[-π,π]
exp(x) ex
pow(y,x) yx
log(x) x的自然对数
sqrt(x) x的平方根
ceil(x) 大于等于x的最小整数
floor(x) 小于等于x的最大整数
rint(x) x取整,不舍入
round(x) 对x四舍五入,即(int)floor(x+0.5)
abs(x) x的绝对值
max(x,y) 返回x,y的大者
min(x,y) 返回x,y的小者
IEEERemander (x,y):按IEEE-754标准计算余数(取模)。(x/y)*y+x%y==x若x%y为z,则改变x或y中的任一个符号仅使z的符号改变。但绝对值是不变的。例如,7%2.5为2.0,-7%2.5为-2.0。
random():返回范围在0.0≤r〈1.0的伪随机数r。

本文地址:http://www.45fan.com/dnjc/67039.html
Tags: 笔记 1System1.1I
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部