您的位置:首页 > 职场人生

黑马程序员--java中的常用类

2013-05-20 15:39 78 查看
-------Android培训、Java培训、期待与您交流! ----------
控制台输入/输出:

很多的程序在运行过程中要和用户交互:接受用户的键盘输入,然后将结果显示在标准输出设备显示器上.这种程序被称为控制台应用程序.这种设备叫标准I/O或控制台I/O.

java.lang.System类的上成员变量提供了I/O操作功能

System.out可向标准输出设备输出

它是一个static final PrintStream对象,它被初始关联到显示器

System.in可从标准的输入设备输入

它是一个static final InputStream对象,被初始关联到键盘

System.err可向标准的错误设备输出

它是一个PrintStream对象

从键盘输入例子:

public static void main(String[] args) {
InputStreamReader  reader = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(reader);
System.out.println("输入exit退出程序");
String str = null;
try {
while( (str = buffer.readLine()) != null ){
if(str.equals("exit"))
break;
System.out.println("读入的数据:" + str);
}
buffer.close();
reader.close();
} catch (IOException e) {
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()方法,打印它的返回字符串
向标准设备输出例子:

public class SystemInOrOut{
public static void main(String args[])
{
int a = 100;
boolean b = true;
System.out.print("echo an int primitive type data:");
System.out.println(a);
System.out.print("echo a boolean primitive type data:");
System.out.println(b);
System.out.print("echo an object:");
Object o = new Object();
System.out.println(o);
}
}


Math类:

Math类中包含了一组数学函数

截取:ceil、floor、round

变量的max、min、abs

三角函数:sin、cos、tan、asin、acos、atan、toDegrees和toRadians

对数指数:log和exp

其它:sqrt、pow、random

常数:PI、E

public class TestMath {
public static void main(String[] args) {
double rad = Math.random();//得到一个随机数
System.out.println(rad);
//取0-100中的随机数
int intrad = (int) Math.floor(rad*100);
System.out.println(intrad);
//得到PI
System.out.println(Math.PI);
}
}


String类:

String对象代表一组不可改变的Unicode字符序列

它的方法可用来创造新的字符串:concat、replace、substring、toLowerCase、toUpperCase和trim。

查找字符的方法:endWith、startWith、 indexOf、 lastIndexOf。

比较字符的方法:equals、equalsIgnoreCase、compareTo。

其它:charAt、length()

String对象的创建:

法一:
String str = new String(“string”);
法二:
String str = “string”;
package lesson.Util2;

public class EqualString {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
System.out.println("s1==s2:"+(s1==s2));
System.out.println("s1.equals(s2):"+(s1.equals(s2)));

String s3 = new String("abc");
String s4 = new String("abc");
System.out.println("s3==s4:"+(s3==s4));
System.out.println("s3.equals(s4):"+(s3.equals(s4)));
}
}


String的不可改变性: String类型数据一旦被创建就不能改变

public static void main(String[] args) {
String str = "ABCD";
System.out.println(str);
String str1 =str.toLowerCase();
System.out.println(str1);
System.out.println(str);
}


StringBuffer类:StringBuffer是字符串缓冲区。

是一个容器。

特点:

1,长度是可变化的。

2,可以字节操作多个数据类型。

3,最终会通过toString方法变成字符串。

StringBuffer对象代表一组可改变的Unicode字符序列

构建器:

StringBuffer() 创建一个空的字符缓冲,长度为16个字符容量;

StringBuffer(int capacity) 用指定的初始容量创建一个空的字符缓冲;

StringBuffer(String initString) 创建包含initString的字符缓冲,并加上16个字符的备用空间。

缓冲的修改操作:append、insert、reverse、setCharAt、setLength。

1,存储。

StringBuffer append():将指定数据作为参数添加到已有数据结尾处。

StringBuffer insert(index,数据):可以将数据插入到指定index位置。

2,删除。

StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end。

StringBuffer deleteCharAt(index):删除指定位置的字符。

3,获取。

char charAt(int index)

int indexOf(String str)

int lastIndexOf(String str)

int length()

String substring(int start, int end)

4,修改。

StringBuffer replace(start,end,string);

void setCharAt(int index, char ch) ;

5,反转。

StringBuffer reverse();

6,

将缓冲区中指定数据存储到指定字符数组中。

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

public static void main(String[] args) {
StringBuffer s1 = new StringBuffer("Double");
s1.append(" J ");
s1.append(true);
s1.append(" ");
s1.append(Math.PI);
s1.insert(9,"blooean:");
s1.insert(22,"PI:");
System.out.println(s1.toString());
s1.delete(0,9);
System.out.println(s1.toString());
}


JDK1.5 版本之后出现了StringBuilder.

StringBuffer是线程同步。

StringBuilder是线程不同步。

开发,建议使用StringBuilder

升级三个因素:

1,提高效率。

2,简化书写。

3,提高安全性

File对象常用方法:

在java中,File类不仅可以操作文件,还可以操作目录

和文件名相关

String getName()

String getPath()

String getAbsolutePath()

String getParent()

boolean renameTo(File newName)

文件检测

boolean exists()

boolean canWrite()

boolean canRead()

boolean isFile()

boolean isDirectory()

获取常规文件信息

long lastModified()

long length()

boolean delete()

目录操作

boolean mkdir()

String[] list()

文件操作Demo:

public static void main(String[] args) {
File file = new File("C:\\Borland\\JBuilder2006\\jdk1.5");
try {
file.createTempFile("wusz", ".txt", file);
} catch (IOException e) {
e.printStackTrace();
}
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
System.out.println("文件:" + files[i]);
} else {
System.out.println("文件夹:" + files[i]);
}
}
}
}


文件过滤:
通过在File中的list()方法中加入FileNameFilter参数,可以只将满足条件的文件列出来

FileNameFilter是一个接口,只有一个accept()方法需要实现

Random类(这个查资料来的)

java.util.Random类提供了一系列产生随机数的方法

nextInt() 产生下一个int类型的随机数,大于等于0

nextInt(int n) 产生下一个int类型的随机数,值大于等于0,并且小于n

nextFloat() 产生一个float类型的随机数,大于等于0并且小于1.0

nextDouble() 产生一个double类型的随机数,大于等于0并且小于1.0

nextLong() 产生一个long类型的随机数,值位于long类型的取值范围

public static void main(String[] args) {
Random random = new Random();
for(int i=0;i<5;i++){
System.out.print(" " + random.nextInt(10));
}
}


处理日期的类:

Java语言提供了2个类来处理日期

java.util.Date : 包装了一个long类型的数据,表示与GMT标准时间1970年1月1日 00:00:00 这一刻相差的毫秒数

java.text.DateFormat: 对日期进行格式化

Date类:

//Date类以毫秒来表示特定的日期
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
}
//DateFormat抽象类用于指定日期格式,它有一个具体的子类为java.text.SimpleDateFormat类,
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat format = new   SimpleDateFormat("yyyy-MM-dd
hh:mm:ss");
System.out.println(format.format(date));
}


Date例子计算时间差

public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1 = df.parse("2006-3-23");
Date d2 = df.parse("2002-4-16");
System.out.println("相差:" + getPeriod(d1,d2) + "天" );
} catch (ParseException e) {
e.printStackTrace();
}
}
public static long getPeriod(Date date1,Date date2){
long  per= date1.getTime() - date2.getTime();
return per/(1000*60*60*24);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: