Java学习笔记-----Java几个与运行环境交互类

1.使用 Scanner 类获取键盘输入
主要方法:
	hasNextXxx():是否还有下一项,XXX代表基本数据类型;
	hasNext():仅仅判断是否包含下一个字符串;
	nextXxx():获取下一个输入项;
	boolean hasNextLine():返回输入源中是否还有下一行;
	String nextLine():返回输入源中下一行的字符串
Scanner使用空白(空格,Tab,回车)作为多个输入项之间的分隔符。
eg:
import java.util.Scanner;

public class ScannerKeyBoardTest {
	public static void main(String[] args) {
		// system.in为键盘输入
		Scanner sc = new Scanner(System.in);
		// 只把空格当做分隔符
		sc.useDelimiter(" ");
		// 判断是否还有下一项输入
		while (sc.hasNext()) {
			System.out.println("键盘输入内容" + sc.next());
		}
	}
}
如果想判断还有下一个long整数,可以把hasNext()改成hasNextLong();

2.使用 Scanner 读取文件输入
eg:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerFileTest {
	public static void main(String[] args) throws FileNotFoundException {
		// F:\\。。。为被读取文件的路劲,文件注意编码格式
		Scanner sc = new Scanner(new File("F:\\workspace\\JavaPractice\\src\\test.txt"));
		System.out.println("test.text文件内容如下");
		// 判断是否还有下一行
		while (sc.hasNextLine()) {
			// 输出下一行
			System.out.println(sc.nextLine());
		}
	}
}

3.System 类
  3.1 getenv()/getenv(String name)获取系统所有/指定的系统变量
  3.2 getProperties()获取所有的系统属性
  3.3 getProperty(String name)获取指定系统的属性
  3.4 currentTimeMillis()获取当前的系时间,毫秒为单位
  3.5 nanoTime()获取当前的系时间,纳秒为单位
  3.6 in、out、err分别代表系统的标准输入、输出、错误输出流
  3.7 identityHashCode(objec x)精确返回指定对象的hashCode值,可以唯一标识该对象
  3.8 gc()通知系统进行垃圾回收  runFinalization() 通知系统进行资源清理
eg:
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Properties;

public class SystemTest {
	public static void main(String[] args) throws Exception {
		// 获取系统所有的环境变量
		Map<String, String> evn = System.getenv();
		for (String name : evn.keySet()) {
			System.out.println(name + "---->" + evn.get(name));
		}
		// 获取指定环境变量的值
		System.out.println(System.getenv("JAVA_HOME"));
		// 获取所有系统属性
		Properties props = new Properties();
		// 将获得的系统属性用txt文档保存在C盘中
		props.store(new FileOutputStream("c://store.txt"), "System Properties");
		// 获取指定的系统属性
		System.out.println(System.getProperty("os.name"));
		// 获取当前的系时间,毫秒为单位
		System.out.println(System.currentTimeMillis());
		// 获取当前的系时间,纳秒为单位
		System.out.println(System.nanoTime());
	}
}

4.Runtime 类
  4.1 gc()通知系统进行垃圾回收  runFinalization() 通知系统进行资源清理
  4.2 load(String filenmane) 加载文件
  4.3 loadLibrary(String libname) 加载动态链接库
  4.4 availableProcessors() 获取处理器数
  4.5 freeMemory() 获取空闲内存数
  4.6 totalMemory() 获取总内存数
  4.7 maxMemory() 获取可用最大内存
  4.8 exec() 运行指定操作系统命令
eg:
import java.io.IOException;

public class RuntimeTest {
	public static void main(String[] args) throws IOException {
		// 获取JAVA关联对象
		Runtime rt=Runtime.getRuntime();
		System.out.println("处理器数:"+rt.availableProcessors());
		System.out.println("空闲内存数:"+rt.freeMemory());
		System.out.println("总内存数:"+rt.totalMemory());
		System.out.println("可用最大内存:"+rt.maxMemory());
		// 运行记事本程序
		rt.exec("notepad.exe");
	}
}

5.Object 类
	Object类是所有类的父类包括数组,枚举类。
  5.1 clone()  创建并返回此对象的一个副本。 
  5.2 equals(Object obj) 指示其他某个对象是否与此对象“相等”。 
  5.3 protected  void finalize() 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 
  5.4 Class<?> getClass() 返回此 Object 的运行时类。 
  5.5 int hashCode()返回该对象的哈希码值。 
  5.6 void notify()唤醒在此对象监视器上等待的单个线程。 
  5.7 void notifyAll()唤醒在此对象监视器上等待的所有线程。 
  5.8 String toString()返回该对象的字符串表示。 
  5.9 void wait()在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。 
  5.10 void wait(long timeout)在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待。 
  5.11 void wait(long timeout, int nanos) 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,
	   或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待
eg:
class Adress {
	String detail;

	public Adress(String detail) {
		this.detail = detail;
	}
}

class User implements Cloneable {
	int age;
	Adress address;

	public User(int age) {
		this.age = age;
		address = new Adress("杭州");
	}

	public User clone() throws CloneNotSupportedException {
		return (User) super.clone();
	}
}

public class CloneTest {
	public static void main(String[] args) throws CloneNotSupportedException {
		User u1 = new User(29);
		// clone得到u1对象的副本
		User u2 = u1.clone();
		// 判断u1 u2是否相等
		System.out.println(u1 == u2);
		// 判断u1 u2的address 是否相同
		System.out.println(u1.address == u2.address);
	}
}

6.ThreadLocalRandom与Random 随机数类
  ThreadLocalRandom是Java7新增加的类,是Random类的加强版。
eg:
import java.util.Arrays;
import java.util.Random;

public class RandomTest {
	public static void main(String[] args) {
		Random rand = new Random();
		System.out.println("rand.nextBoolean():" + rand.nextBoolean());
		byte[] buffer = new byte[16];
		rand.nextBytes(buffer);
		System.out.println(Arrays.toString(buffer));
		// 随机生成一个0.0到1.0的double数
		System.out.println("rand.nextDouble():" + rand.nextDouble());
		System.out.println("rand.nextFloat():" + rand.nextFloat());
		System.out.println("rand.nextGaussian():" + rand.nextGaussian());
		System.out.println("rand.nextInt():" + rand.nextInt());
		// 生成0到30的随机整数
		System.out.println("rand.nextInt():" + rand.nextInt(30));
		System.out.println("rand.nextLong():" + rand.nextLong());
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值