.java文件转换txt
时间: 2023-07-27 21:12:37 浏览: 118
您可以使用以下代码将Java文件转换为txt文件:
```java
import java.io.*;
public class JavaToTxt {
public static void main(String[] args) {
String fileName = "file.java";
String outputFileName = "file.txt";
try {
File file = new File(fileName);
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
FileWriter writer = new FileWriter(outputFileName);
BufferedWriter bw = new BufferedWriter(writer);
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
System.out.println("Java file has been converted to txt file successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
您需要将`fileName`变量设置为要转换的Java文件的名称,并将`outputFileName`变量设置为要输出的txt文件的名称。然后运行该程序即可。
阅读全文