Java学习之常用类(三.Math类;四File类 五.枚举类型)
2013-03-24 17:41
435 查看
Math类:
java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。
abs绝对值
acos,asin,atan,cos,sin,tan
sqrt平方根
pow(double a,double b)a的b次幂
log自然对数
exp e为底指数
max(double a,double b)
min(double a,double b)
random()返回0.0到1.0的随机数
long round(double a)double型的数据a转换为long型(四舍五入)
toDegress(double angrad)弧度->角度
toRadians(double angdeg)角度->弧度
实际中用的不多
public class TestMath{
public static void main(String args[]){
double a =Math.random();
double b = Math.random();
System.out.println(Math.sqrt(a*a+b*b));
System.out.println(Math.pow(a,8));
System.out.println(Math.round(b));
System.out.println(Math.log(Math.pow(Math.E,15)));// 15.0
double d =60.0,r = Math.PI/4;
System.out.println(Math.toRadians(d));
System.out.println(Math.toDegrees(r));//45.00
}
}
File类:
Java.io.File类:代表系统文件名(路径和文件名),注意它只是文件名,读不出的。
File类的常见构造方法:
File静态属性String separator存储了当前系统的路径分隔符。
static final的,便于跨平台。正斜杠都是对的\
如何让区分是文件还是路径:isFile()方法
通过File对象可以访问文件的属性
public+
通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
例子:
练习:编写一个程序,在命令行中以树状结构展现特定的文件夹及其文件(夹)
java.lang.Enum枚举类型
枚举类型:
只能够取特定值中的一个
使用enum关键字
是java.lang.Enum类型
如果错误要产生的话就提前让它产生
不能取枚举以外的值,只能写预先定义好的
java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。
abs绝对值
acos,asin,atan,cos,sin,tan
sqrt平方根
pow(double a,double b)a的b次幂
log自然对数
exp e为底指数
max(double a,double b)
min(double a,double b)
random()返回0.0到1.0的随机数
long round(double a)double型的数据a转换为long型(四舍五入)
toDegress(double angrad)弧度->角度
toRadians(double angdeg)角度->弧度
实际中用的不多
public class TestMath{
public static void main(String args[]){
double a =Math.random();
double b = Math.random();
System.out.println(Math.sqrt(a*a+b*b));
System.out.println(Math.pow(a,8));
System.out.println(Math.round(b));
System.out.println(Math.log(Math.pow(Math.E,15)));// 15.0
double d =60.0,r = Math.PI/4;
System.out.println(Math.toRadians(d));
System.out.println(Math.toDegrees(r));//45.00
}
}
File类:
Java.io.File类:代表系统文件名(路径和文件名),注意它只是文件名,读不出的。
File类的常见构造方法:
File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname. 注意: 不是在硬盘上创建这样一个文件或一个路径,而是在内存里创建名字为xx的File对象 以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储 |
File(String parent, String child) Creates a new File instance from a parent pathname string and a child pathname string.以parent为父路径,child为子路经创建File对象 |
static final的,便于跨平台。正斜杠都是对的\
如何让区分是文件还是路径:isFile()方法
boolean | isFile () Tests whether the file denoted by this abstract pathname is a normal file. |
public+
boolean | canRead() Tests whether the application can read the file denoted by this abstract pathname. |
boolean | canWrite() Tests whether the application can modify the file denoted by this abstract pathname. |
boolean | exists() Tests whether the file or directory denoted by this abstract pathname exists. |
boolean | isDirectory() Tests whether the file denoted by this abstract pathname is a directory. |
boolean | isFile() Tests whether the file denoted by this abstract pathname is a normal file. |
boolean | isHidden() Tests whether the file named by this abstract pathname is a hidden file. |
long | lastModified() Returns the time that the file denoted by this abstract pathname was last modified. |
long | length() 高效率存多少毫秒 Returns the length of the file denoted by this abstract pathname. |
String | getName() Returns the name of the file or directory denoted by this abstract pathname. |
String | getPath() Converts this abstract pathname into a pathname string. |
boolean | createNewFile() Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. |
boolean | delete() Deletes the file or directory denoted by this abstract pathname. |
boolean | mkdir() Creates the directory named by this abstract pathname. |
boolean | mkdirs() Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. |
String | getAbsolutePath() Returns the absolute pathname string of this abstract pathname. |
package bjsxt;//注意:打包后的父路径不是bjsxt,它会把bjsxt作为一起java bjsxt.TestFile.java import java.io.*; public class TestFile { public static void main(String[] args) { String separator = File.separator; String filename = "myfile.txt"; String directory = "mydir1" + separator + "mydir2"; //String directory = "mydir1/mydir2"; //String directory = "mydir1\\mydir2"; File f = new File(directory, filename); if (f.exists()) { System.out.println("文件名:" + f.getAbsolutePath()); System.out.println("文件大小:" + f.length()); } else { f.getParentFile().mkdirs();//求包上层的上层路径 try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } }
练习:编写一个程序,在命令行中以树状结构展现特定的文件夹及其文件(夹)
import java.io.*; public class FileList{ public static void main(String args[]){ File f = new File("d:/java/S"); System.out.println(f.getName()); tree(f,1); } private static void tree(File f,int level){ String preStr = ""; for(int i=0;i<level;i++){ preStr +=" "; } File[] childs = f.listFiles(); for(int i=0;i<childs.length;i++){ System.out.println(preStr+childs[i].getName()); if(childs[i].isDirectory()){ tree(childs[i],level+1); } } } }
java.lang.Enum枚举类型
枚举类型:
只能够取特定值中的一个
使用enum关键字
是java.lang.Enum类型
如果错误要产生的话就提前让它产生
不能取枚举以外的值,只能写预先定义好的
public class TestEnum { public enum MyColor { red, green, blue }; public enum MyDoorOpener {me, mywife}; public static void main(String[] args) { MyColor m = MyColor.red; switch(m) { case red: System.out.println("red"); break; case green: System.out.println("green"); break; default: System.out.println("default"); } System.out.println(m); } }
相关文章推荐
- Java学习笔记之集合(二):List集合的常用方法
- java学习——常用数字运算方法
- 共同学习Java源码--常用数据类型--String(十四)
- Java学习常用网站收集(不断补充...)
- java学习笔记之sql常用语句
- JAVA学习第三十七课(常用对象API)— Set集合:TreeSet集合、比较器
- 对Java中的枚举类型的学习总结
- JAVA学习第三十八课(常用对象API— Map集合及其常用方法
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(五)
- java与.net比较学习系列(1) 开发环境和常用调试技巧
- Java学习常用网站收集
- Java学习常用网站收集
- Java基础学习总结(70)——开发Java项目常用的工具汇总
- JAVA学习笔记(6)--String的常用方法
- 共同学习Java源码--常用数据类型--String(三)
- java学习总结(16.07.14)String类及其的常用方法
- Java学习之容器上(Collection接口常用方法,Iterator接口,使用foreach循环遍历Collection集合元素,Set集合通用知识(Hashset类,hashcode()与Lin
- 共同学习Java源码--常用数据类型--String(十)
- Java常用容器类学习总结
- java学习总结(16.06.28)包装类和包装类的常用方法