集册 Java实例教程 将字符串写入文件

将字符串写入文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

468
将字符串写入文件


import java.io.BufferedInputStream;/**来自 时代Java公众号 - nowjava.com**/

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.URL;

import java.net.URLConnection;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.List;
/*来自 
 N o w  J a v a  .   c o m*/

import java.util.Map.Entry;

import java.util.Set;

import org.apache.log4j.Logger;


public class Main{

    private static Logger logger = Logger

            .getLogger(FileAndDatastructureUtil.class);

    public static void writeStringToFile(String filename, String content,

            boolean overwrite) {

        File destFile = new File(filename);

        try {

            if (destFile.exists()) {

                if (overwrite)

                    destFile.delete();

                else

                    return;

            }

            destFile.createNewFile();

            DataOutputStream outputStream = new DataOutputStream(

                    new FileOutputStream(destFile));

            outputStream.writeBytes(content);

            outputStream.close();

        } catch (IOException e) {

            logger.debug(e.getMessage());

            e.printStackTrace();

        }

    }

    
展开阅读全文