Java学习之常用类(三.Math类;四File类 五.枚举类型)

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 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对象

File静态属性String separator存储了当前系统的路径分隔符。

static final的,便于跨平台。正斜杠都是对的\

 

如何让区分是文件还是路径:isFile()方法

boolean

isFile()
          Tests whether the file denoted by this abstract pathname is a normal file.

 

 

通过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.

通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)

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.

例子:

 1 package bjsxt;//注意:打包后的父路径不是bjsxt,它会把bjsxt作为一起java bjsxt.TestFile.java

 2 

 3 import java.io.*;

 4 

 5 public class TestFile {

 6 

 7 public static void main(String[] args) {

 8 

 9 String separator = File.separator;

10 

11 String filename = "myfile.txt";

12 

13 String directory = "mydir1" + separator + "mydir2";

14 

15 //String directory = "mydir1/mydir2";

16 

17 //String directory = "mydir1\\mydir2";

18 

19 File f = new File(directory, filename);

20 

21 if (f.exists()) {

22 

23 System.out.println("文件名:" + f.getAbsolutePath());

24 

25 System.out.println("文件大小:" + f.length());

26 

27 } else {

28 

29 f.getParentFile().mkdirs();//求包上层的上层路径

30 

31 try {

32 

33 f.createNewFile();

34 

35 } catch (IOException e) {

36 

37 e.printStackTrace();

38 

39 }

40 

41 }

42 

43 }

44 

45 }

 

练习:编写一个程序,在命令行中以树状结构展现特定的文件夹及其文件(夹)

 1 import java.io.*;

 2 

 3 public class FileList{

 4 

 5 public static void main(String args[]){

 6 

 7 File f = new File("d:/java/S");

 8 

 9 System.out.println(f.getName());

10 

11 tree(f,1);

12 

13 }

14 

15 private static void tree(File f,int level){

16 

17 String preStr = "";

18 

19 for(int i=0;i<level;i++){

20 

21 preStr +=" ";

22 

23 }

24 

25 File[] childs = f.listFiles();

26 

27 for(int i=0;i<childs.length;i++){

28 

29 System.out.println(preStr+childs[i].getName());

30 

31 if(childs[i].isDirectory()){

32 

33 tree(childs[i],level+1);

34 

35 }

36 

37 }

38 

39 }

40 

41 }

 

java.lang.Enum枚举类型

枚举类型:

只能够取特定值中的一个

使用enum关键字

是java.lang.Enum类型

如果错误要产生的话就提前让它产生

不能取枚举以外的值,只能写预先定义好的

 1 public class TestEnum {

 2 

 3 public enum MyColor { red, green, blue };

 4 

 5 public enum MyDoorOpener {me, mywife};

 6 

 7 public static void main(String[] args) {

 8 

 9 MyColor m = MyColor.red;

10 

11 switch(m) {

12 

13 case red:

14 

15 System.out.println("red");

16 

17 break;

18 

19 case green:

20 

21 System.out.println("green");

22 

23 break;

24 

25 default:

26 

27 System.out.println("default");

28 

29 }

30 

31 System.out.println(m);

32 

33 }

34 

35 }

 

 

 

你可能感兴趣的:(Java学习)