编译Java程序,实现内容为“abcABC\n我爱你,祖国,我的母亲!”
时间: 2024-03-17 22:43:30 浏览: 137
您可以使用以下Java代码来实现:
```java
import java.io.*;
public class Main {
public static void main(String[] args) {
String content = "abcABC\n我爱你,祖国,我的母亲!";
try {
FileWriter fw = new FileWriter("mytest.txt");
fw.write(content);
fw.close();
System.out.println("文件已成功写入。");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这段代码会将指定的字符串写入到名为“mytest.txt”的文件中。您可以在运行该程序之后,在当前目录下查找该文件。
如果您想要在程序运行之后将文件复制到E盘根目录下并更改文件名称,可以使用以下代码:
```java
import java.io.*;
public class Main {
public static void main(String[] args) {
String content = "abcABC\n我爱你,祖国,我的母亲!";
try {
FileWriter fw = new FileWriter("mytest.txt");
fw.write(content);
fw.close();
System.out.println("文件已成功写入。");
File originalFile = new File("mytest.txt");
File copiedFile = new File("E:/copytest.txt");
InputStream in = new FileInputStream(originalFile);
OutputStream out = new FileOutputStream(copiedFile);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("文件已成功复制到E盘根目录下并更名为copytest.txt。");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这段代码会在文件写入之后,将该文件复制到E盘根目录下,并将文件名称更改为“copytest.txt”。