AI智能
改变未来

使用输出流FileOutputStream把数据写入本地文件

使用输出流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();}}}

}

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 使用输出流FileOutputStream把数据写入本地文件