使用输出流FileOutputStream把数据写入本地文件
直接上代码哈
package com.pccc.springBoot.createTxt;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class GenerateFile {
private static String PATH = \"E:/file/\";private static String fileName;private static String TYPE = \".txt\";private static File filename ;public static void main(String[] args) throws IOException {//创建文件 test2.txtgenerateFile(\"test2\");//把123456 写到 test2.txtwriteContent(\"123456\");System.out.println(\"==========文件已经生成==========\");}public static void generateFile(String name) throws IOException{fileName = PATH + name + TYPE;filename = new File(fileName);File fileParent = filename.getParentFile();if(!fileParent.exists()){fileParent.mkdirs();}if(!filename.exists()){filename.createNewFile();}}public static void writeContent(String content) throws IOException{String filein = content ;FileOutputStream fos = null;try {StringBuffer buf = new StringBuffer();buf.append(filein);fos = new FileOutputStream(filename);fos.write(buf.toString().getBytes());fos.close();} catch (FileNotFoundException e) {e.printStackTrace();}finally{if(fos != null){fos.close();}}}
}