System.in System.out

System.in

主要是作為鍵盤輸入串流。

透過getClass(),可以知道System.in的執行類型是BufferedInputStream,Buffered的意思是讀取的位址是在記憶體緩衝區。

1
System.out.println(System.in.getClass());
class java.io.BufferedInputStream

Scanner

Scanner掃描器,它的建構子參數是System.in鍵盤輸入串流。

1
Scanner scanner = new Scanner(System.in);

Scanner會去鍵盤輸入串流中取得資料。

next(),nextInt(),nextDouble(),charAt(0)

當執行到next開頭的方法,終端機會等待使用者輸入,直到使用者輸入才執行下一行程式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Test {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("請輸入名字:");
    String name = scanner.next();  // 字串
    System.out.println("請輸入性別:");
    char sex = scanner.next().charAt(0);  // 字元
    System.out.println("請輸入年齡:");
    int age = scanner.nextInt();  // int
    System.out.println("請輸入體重");
    double weight = scanner.nextDouble();
    System.out.println("name = " + name + ", sex = " + sex + ", age = " + age + ", weight = " + weight);
  }
}
請輸入名字:
cici
請輸入性別:
女
請輸入年齡:
5
請輸入體重
50
name = cici, sex = 女, age = 5, weight = 50.0

Scanner與迴圈

使用時,Scanner放在迴圈之外。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    Scanner scanner = new Scanner(System.in);
    int i = 1;
    do {
      System.out.println("==== " + i + " ====");
      System.out.println("請輸入名字:");
      String name = scanner.next();  // 字串
      System.out.println("請輸入性別:");
      char sex = scanner.next().charAt(0);  // 字元
      System.out.println("請輸入年齡:");
      int age = scanner.nextInt();  // int
      System.out.println("請輸入體重");
      double weight = scanner.nextDouble();
      System.out.println("name = " + name + ", sex = " + sex + ", age = " + age + ", weight = " + weight);
      i++;
    } while (i <= 2);
==== 1 ====
請輸入名字:
cici
請輸入性別:
女
請輸入年齡:
5
請輸入體重
15
name = cici, sex = 女, age = 5, weight = 15.0
==== 2 ====
請輸入名字:
bill
請輸入性別:
男
請輸入年齡:
20
請輸入體重
70
name = bill, sex = 男, age = 20, weight = 70.0

System.out

執行類型是PrintStream,將資料顯示在螢幕。

1
System.out.println(System.out.getClass());
class java.io.PrintStream

PrintStream的write()

PrintStream繼承OutputStream,自然會有父類別的write方法。

1
2
3
4
5
6
7
public class Test2 {
  public static void main(String[] args) throws IOException {
    PrintStream out = System.out;
    out.write("測試".getBytes());
    out.close();
  }
}

設定PrintStream輸出文件位置

預設是顯示在顯示器,可以透過建構子,設定輸出的文件位置。

PrintStream(String fileName)

使用System.setOut

1
System.setOut(new PrintStream("文件路徑"));

範例1

1
2
3
4
5
6
7
8
9
public class Test2 {
  public static void main(String[] args) throws IOException {
    // 使用System.setOut
    System.setOut(new PrintStream("/Users/cici/testc/print_out"));
    PrintStream out = System.out;
    out.write("測試".getBytes());
    out.close();
  }
}

範例2

1
2
3
4
5
6
public class Test2 {
  public static void main(String[] args) throws IOException {
    System.setOut(new PrintStream("/Users/cici/testc/print_out"));
    System.out.println("哈囉哈囉");
  }
}

PrintWriter

建構子是System.out,輸出到螢幕。

注意!使用時一定要close(),因為close()方法是寫入資料,如果沒呼叫close(),螢幕就不會顯示任何東西。

1
2
3
4
5
6
7
8
public class Test3 {
  public static void main(String[] args) {
    PrintWriter writer = new PrintWriter(System.out);
    writer.println("測試測試2");
    // 一定要close
    writer.close();
  }
}

建構子是FileWriter,輸出到檔案。

1
2
3
4
5
6
7
8
public class Test3 {
  public static void main(String[] args) throws IOException {
    PrintWriter writer = new PrintWriter(new FileWriter("/Users/cici/testc/print_out"));
    writer.println("測試測試2");
    // 一定要close
    writer.close();
  }
}

results matching ""

    No results matching ""