.java文件转换txt
时间: 2023-07-27 11:12:37 浏览: 43
您可以使用以下代码将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文件的名称。然后运行该程序即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)