在项目文件夹下建一个in.txt文件,输入ke wen xue yuan,然后将内容转化成对应的ASCII码并输出Java代码
时间: 2024-03-03 22:51:22 浏览: 23
好的,以下是Java代码实现你的需求:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("in.txt"))) {
String text = br.readLine();
int[] asciiList = new int[text.length()];
// 将每个字符转换为对应的ASCII码
for (int i = 0; i < text.length(); i++) {
asciiList[i] = (int) text.charAt(i);
}
// 输出ASCII码
for (int ascii : asciiList) {
System.out.print(ascii + " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这段Java代码与Python代码的实现思路类似,区别在于Java需要使用BufferedReader类来读取文件内容。运行这段代码时,输出结果应该是:
```
107 101 32 119 101 110 32 120 117 101 32 121 117 97 110
```
其中每个数字都是对应字符的ASCII码。