Java中while (true) { String input = in.readLine(); if (input == null) { return; }改成多行读取
时间: 2024-02-21 09:02:28 浏览: 46
可以使用 `while` 循环和 `in.ready()` 方法来实现多行读取,具体代码如下:
```java
while (true) {
StringBuilder sb = new StringBuilder();
while (in.ready()) {
String input = in.readLine();
if (input == null) {
return;
}
sb.append(input);
sb.append(System.lineSeparator());
}
String input = sb.toString().trim();
// 处理输入
}
```
上述代码中,首先定义了一个 `StringBuilder` 对象 `sb`,用来存储多行输入的内容。然后进入 `while` 循环,使用 `in.ready()` 方法检查输入流是否有数据可读,如果有数据可读,则使用 `in.readLine()` 方法读取一行数据,并将其添加到 `sb` 对象中。如果读取到的数据为 `null`,则表示输入流已经结束,可以退出循环。最后,使用 `sb.toString().trim()` 方法获取完整的输入内容,并去除开头和结尾的空格和换行符。最后一步是为了确保输入内容的正确性。最后,你可以在注释的地方添加你的输入处理代码。
相关问题
while (true) { String input = in.readLine(); if (input == null) { return; }改成多行读取
可以使用 `while` 循环和 `in.ready()` 方法来实现多行读取,具体代码如下:
```java
while (true) {
if (in.ready()) {
String input = in.readLine();
if (input == null) {
return;
}
// 处理输入
} else {
// 等待输入
Thread.sleep(100);
}
}
```
上述代码中,`in.ready()` 方法用来检查输入流是否有数据可读。如果有数据可读,则使用 `in.readLine()` 方法读取一行数据。如果读取到的数据为 `null`,则表示输入流已经结束,可以退出循环。如果没有数据可读,则等待一段时间后再次检查。这里使用 `Thread.sleep(100)` 方法等待 100 毫秒,减少 CPU 的占用率,提高性能。
java中的删除所有注释
以下是一个简单的Java程序,用于删除Java代码中的所有注释:
```
import java.io.*;
public class RemoveComments {
public static void main(String[] args) throws IOException {
String inputFileName = "inputFile.java";
String outputFileName = "outputFile.java";
BufferedReader reader = new BufferedReader(new FileReader(inputFileName));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName));
String line = reader.readLine();
boolean inBlockComment = false;
while (line != null) {
if (!inBlockComment) {
line = removeLineComments(line);
}
if (line.contains("/*")) {
inBlockComment = true;
line = removeBlockCommentStart(line);
}
if (inBlockComment) {
if (line.contains("*/")) {
inBlockComment = false;
line = removeBlockCommentEnd(line);
} else {
line = "";
}
}
writer.write(line);
writer.newLine();
line = reader.readLine();
}
reader.close();
writer.close();
}
private static String removeLineComments(String line) {
int index = line.indexOf("//");
if (index != -1) {
line = line.substring(0, index);
}
return line;
}
private static String removeBlockCommentStart(String line) {
int index = line.indexOf("/*");
if (index != -1) {
line = line.substring(0, index);
}
return line;
}
private static String removeBlockCommentEnd(String line) {
int index = line.indexOf("*/");
if (index != -1) {
line = line.substring(index + 2);
}
return line;
}
}
```
该程序将从名为“inputFile.java”的文件中读取Java代码,并将删除所有注释后将结果写入名为“outputFile.java”的文件中。
该程序使用三个帮助方法来删除不同类型的注释:
- `removeLineComments`方法删除单行注释(以“//”开头的注释)。
- `removeBlockCommentStart`方法删除块注释的开始部分(以“/*”开头的注释)。
- `removeBlockCommentEnd`方法删除块注释的结束部分(以“*/”结尾的注释)。
该程序还使用一个布尔变量`inBlockComment`来跟踪是否当前正在处理块注释。如果是,则程序将跳过所有行,直到找到块注释的结尾。否则,程序将删除单行注释并处理块注释的开始。
请注意,该程序仅删除Java代码中的注释,并且可能无法正确处理某些情况,例如在字符串或字符字面量中的注释。因此,请谨慎使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)