如何用JAVA生成TXT文件
1个回答
展开全部
生成TXT的方法有很多的。常用位字节流和字符流
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class TextFileGenerator {
public static void main(String[] args) throws Exception {
method1();
method2();
}
private static void method1() throws Exception {
String txtContent = "Hello World!";
File file = new File("test1.txt");
FileWriter fw = new FileWriter(file);
fw.write(txtContent);
fw.close();
}
private static void method2() throws Exception {
String txtContent = "Hello World!";
File file = new File("test2.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(txtContent.getBytes());
fos.close();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class TextFileGenerator {
public static void main(String[] args) throws Exception {
method1();
method2();
}
private static void method1() throws Exception {
String txtContent = "Hello World!";
File file = new File("test1.txt");
FileWriter fw = new FileWriter(file);
fw.write(txtContent);
fw.close();
}
private static void method2() throws Exception {
String txtContent = "Hello World!";
File file = new File("test2.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(txtContent.getBytes());
fos.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询