IO流将指定内容写入指定目录文件内

时间:2023-01-21 18:14:24

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


/**
  * 将指定内容写入指定目录文件内
  * @param directory--文件路径
  * @param newFileName_dat --文件名
  * @param s--内容
  */
 public void   writerContent(String directory,String newFileName_dat,String s){
   //本地创建目录并且将内容S写入文件内
        File file=new File(directory);
        if(!file.exists()){
         file.mkdir();
        }
        File file2 = new File(directory,newFileName_dat);
        if(!file2.exists()){
         try{
          file2.createNewFile();
         }catch(IOException e){
          e.printStackTrace();
         }
        }
        FileWriter fileWriter;
        try{
            fileWriter = new FileWriter(file2);
            //向指定文件中写入内容
            BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
            bufferedWriter.write(s.toString());
            bufferedWriter.close();
        }catch(IOException e){
         e.printStackTrace();
        }
 }