编写一个HelloWorld.java,选择合适的的输入输出流将原程序文件复制一份到”Test.Java”文件中、并同时将文件的内容打印到命令提示符窗口中
时间: 2024-05-13 17:17:09 浏览: 15
以下是代码:
```java
import java.io.*;
public class HelloWorld {
public static void main(String[] args) {
try {
// 读取原文件
FileInputStream fileInputStream = new FileInputStream("HelloWorld.java");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
}
bufferedReader.close();
fileInputStream.close();
// 写入目标文件
FileOutputStream fileOutputStream = new FileOutputStream("Test.java");
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferedWriter.write(stringBuilder.toString());
bufferedWriter.close();
fileOutputStream.close();
// 输出文件内容到命令提示符窗口
System.out.println(stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
此程序会读取当前目录下的 HelloWorld.java 文件,将其内容复制到 Test.java 文件中,并输出文件内容到命令提示符窗口。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)