从键盘中输入一组字符串,利用文件输入流输出这组字符串对应的ASCCII码值。
时间: 2024-02-09 16:08:57 浏览: 25
以下是一个示例代码,可以从键盘中读取一行字符串,然后将其转换为ASCII码,并使用文件输入流输出到文件中:
```java
import java.io.*;
public class StringToASCII {
public static void main(String[] args) {
try {
// 从键盘中读取一行字符串
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入字符串:");
String str = reader.readLine();
// 将字符串转换为ASCII码,并写入文件
FileOutputStream fos = new FileOutputStream("ascii.txt");
for (int i = 0; i < str.length(); i++) {
int asciiCode = (int) str.charAt(i);
fos.write(asciiCode);
fos.write(' ');
}
fos.close();
System.out.println("ASCII码已写入文件 ascii.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该程序会在运行时提示用户输入字符串,并将其转换为ASCII码写入到名为`ascii.txt`的文件中。