File tree Expand file tree Collapse file tree 4 files changed +41
-9
lines changed
src/java.base/share/classes
javax/security/auth/callback Expand file tree Collapse file tree 4 files changed +41
-9
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright (c) 1996, 2024 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1996, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
50
50
* BufferedReader in = new BufferedReader(new InputStreamReader(anInputStream));
51
51
* }
52
52
*
53
+ * <P>To read from {@link System#in}, use the system property value
54
+ * {@link System##stdin.encoding stdin.encoding} as the {@code Charset}:
55
+ *
56
+ * {@snippet lang=java :
57
+ * new InputStreamReader(System.in, System.getProperty("stdin.encoding"));
58
+ * }
59
+ *
53
60
* @see BufferedReader
54
61
* @see InputStream
55
62
* @see Charset
63
+ * @see System##stdin.encoding stdin.encoding
56
64
*
57
65
* @author Mark Reinhold
58
66
* @since 1.1
Original file line number Diff line number Diff line change @@ -124,10 +124,19 @@ private System() {
124
124
*
125
125
* @apiNote
126
126
* The typical approach to read character data is to wrap {@code System.in}
127
- * within an {@link java.io.InputStreamReader InputStreamReader} or other object
128
- * that handles character encoding. After this is done, subsequent reading should
129
- * use only the wrapper object; operating directly on {@code System.in} results
130
- * in unspecified behavior.
127
+ * within the object that handles character encoding. After this is done,
128
+ * subsequent reading should use only the wrapper object; continuing to
129
+ * operate directly on {@code System.in} results in unspecified behavior.
130
+ * <p>
131
+ * Here are two common examples. Using an {@link java.io.InputStreamReader
132
+ * InputStreamReader}:
133
+ * {@snippet lang=java :
134
+ * new InputStreamReader(System.in, System.getProperty("stdin.encoding"));
135
+ * }
136
+ * Or using a {@link java.util.Scanner Scanner}:
137
+ * {@snippet lang=java :
138
+ * new Scanner(System.in, System.getProperty("stdin.encoding"));
139
+ * }
131
140
* <p>
132
141
* For handling interactive input, consider using {@link Console}.
133
142
*
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright (c) 2003, 2023 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2003, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
78
78
* }
79
79
* }
80
80
*
81
- * <p>As another example, this code allows {@code long} types to be
81
+ * <p>This code allows {@code long} types to be
82
82
* assigned from entries in a file {@code myNumbers}:
83
83
* {@snippet :
84
84
* Scanner sc = new Scanner(new File("myNumbers"));
87
87
* }
88
88
* }
89
89
*
90
+ * <p>This code uses a {@code Scanner} to read lines from {@link System#in}. The
91
+ * {@code Scanner} uses the system property value of
92
+ * {@link System##stdin.encoding stdin.encoding} as the {@code Charset}. Specifying
93
+ * the charset explicitly is important when reading from {@code System.in}, as it
94
+ * may differ from the {@link Charset#defaultCharset() default charset} depending
95
+ * on the host environment or user configuration:
96
+ * {@snippet :
97
+ * Scanner sc = new Scanner(System.in, System.getProperty("stdin.encoding"));
98
+ * while (sc.hasNextLine()) {
99
+ * String aLine = sc.nextLine();
100
+ * }
101
+ * }
102
+ *
90
103
* <p>The scanner can also use delimiters other than whitespace. This
91
104
* example reads several items in from a string:
92
105
* {@snippet :
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright (c) 1999, 2015 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1999, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -113,7 +113,9 @@ public interface CallbackHandler {
113
113
* System.err.print(nc.getPrompt());
114
114
* System.err.flush();
115
115
* nc.setName((new BufferedReader
116
- * (new InputStreamReader(System.in))).readLine());
116
+ * (new InputStreamReader(
117
+ * System.in,
118
+ * System.getProperty("stdin.encoding")))).readLine());
117
119
*
118
120
* } else if (callbacks[i] instanceof PasswordCallback) {
119
121
*
You can’t perform that action at this time.
0 commit comments