Java System类

作者: Tinyspot | 来源:发表于2022-07-30 17:58 被阅读0次

System 类

获取系统属性

1.1 私有构造方法

public final class System {
    private System() {
    }
}

1.2 System.out.println()

3 个类变量:in, out, err

public final static InputStream in = null;
public final static PrintStream out = null;
public final static PrintStream err = null;

2. 常用方法

  • static native void arraycopy(...)
  • static native long currentTimeMillis()
  • static void gc()
  • static void exit(int status)
  • System.getProperties()

2.1 arraycopy(...)

数组复制

/**
 * src – the source array.
 * srcPos – starting position in the source array.
 * dest – the destination array.
 * destPos – starting position in the destination data.
 * length – the number of array elements to be copied.
 */
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
int[] src = {1, 2, 3, 4, 5};
int[] desc = {10, 11, 12, 13};
System.arraycopy(src, 0, desc, 1, 2);
// [10, 1, 2, 13]

int[] desc2 = new int[10];
System.arraycopy(src, 1, desc2, 0, src.length - 1);
// [2, 3, 4, 5, 0, 0, 0, 0, 0, 0]

2.2 Arrays.copyOf(...)

public static int[] copyOf(int[] original, int newLength) {
    int[] copy = new int[newLength];
    System.arraycopy(original, 0, copy, 0,
                    Math.min(original.length, newLength));
    return copy;
}

public static <T> T[] copyOf(T[] original, int newLength) {
    return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    @SuppressWarnings("unchecked")
    T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
            Math.min(original.length, newLength));
    return copy;
}

数组拷贝,返回一个新的数组

String[] strSrc = {"aaa", "bbb", "ccc", "ddd"};
String[] newArray = Arrays.copyOf(strSrc, 1);
// [bbb, ccc]
String[] newArray2 = Arrays.copyOfRange(strSrc, 1, 3);

2.3 currentTimeMillis()

可以用来计时,文件命名,随机数的种子数

使用一:计时

long start = System.currentTimeMillis();
// ...
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start));

使用二:获取系统当前时间

Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String now = format.format(date);

3. Properties类

Properties:属性集合
属性名和属性值都是字符串类型

系统属性 Properties properties = System.getProperties();
自定义属性

Properties properties = new Properties();
properties.setProperty("name", "tinyspot");
properties.setProperty("work", "Java");
Set<String> propertyNames = properties.stringPropertyNames();
for (String propertyName : propertyNames) {
    
}

和流有关的方法

PrintWriter out = new PrintWriter("demo.txt");
properties.list(out);
out.close();

FileOutputStream fos = new FileOutputStream("demo.properties");
properties.store(fos, "注释");
fos.close();

Properties properties2 = new Properties();
FileInputStream fis = new FileInputStream("demo.properties");
properties2.load(fis);
fis.close();
System.out.println(properties2.toString());

4. Scanner 控制台输入

public Scanner(InputStream source) {
  this(new InputStreamReader(source), WHITESPACE_PATTERN);
}

public String next() {}
public int nextInt() {}
public double nextDouble() {}

若输入类型不匹配的数据,会抛异常 java.util.InputMismatchException

/**
   * Math类在java.lang包下,所以不需要导包
   * random():返回带正号的double值,该值大于等于0.0且小于1.0
  * 更强的类 Random
  * 可以直接返回整形类型的随机数  new Random().nextInt(int)
   */
public static void main(String[] args) {
    int number = (int) (Math.random() * 100) + 1;
    // 标准输入
    Scanner scanner = new Scanner(System.in);
    // String str = scanner.nextLine();
    while (true) {
        System.out.print("请输入你的数字: ");
        int guess = scanner.nextInt();
        // String name = input.next();
        // ...
    }
}

相关文章

  • java-System & Runtime

    System类 System类介绍   System类代表Java程序运行平台,程序不能创建该对象,但是Syste...

  • 2020-08-24--Java--day01【常用API】

    主要内容 System类 StringBuilder类 包装类 1.System类 java.lang.Syste...

  • Java System类

    System 类 获取系统属性 1.1 私有构造方法 1.2 System.out.println() 3 个类变...

  • Java高级API

    System java.lang.System System 类包含一些有用的类字段和方法。它不能被实例化,Sys...

  • java中的system类

    System作为系统类,在JDK的java.lang包中,可见它也是一种java的核心语言特性。System类的构...

  • 04-类库

    Java API Object类 Class类 System类 封装类 Integer类 Character类 S...

  • Java实例-异常处理

    1、Java 实例 - 异常处理方法:使用 System 类的 System.err.println() 来展示异...

  • Java异常处理

    Java 实例 - 异常处理方法 以下实例演示了使用 System 类的 System.err.println()...

  • Object & Math & System的API

    Object & Math& System的API Object类 •Object类是Java中其他所有类的基类;...

  • Javax系统相关类

    System System代表当前Java程序的运行平台,所以程序不能创建System对象.一般我们使用其类常量和...

网友评论

    本文标题:Java System类

    本文链接:https://www.haomeiwen.com/subject/noqhwrtx.html