【JAVA笔记】将字符串信息写入xml文件的方法

拿到的第一个任务的要求,结果最后又不用了。扔了怪可惜的,就留着看看吧。


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.StringReader;

/**
 * 将相应的信息写入XML文件
 * 
 * @author wyd
 * 
 */
public class XMLWriter {


	public void writeStringFile(String Data, String filePath) {
		BufferedReader bufferedReader = null;
		BufferedWriter bufferedWriter = null;
		File distFile = null;
		try {
			distFile = new File(filePath);
			if (!distFile.getParentFile().exists())
				distFile.getParentFile().mkdirs();
			bufferedReader = new BufferedReader(new StringReader(Data));
			bufferedWriter = new BufferedWriter(new FileWriter(distFile));
			char buf[] = new char[1024]; // 字符缓冲区
			int len;
			while ((len = bufferedReader.read(buf)) != -1) {
				bufferedWriter.write(buf, 0, len);
			}
			bufferedWriter.flush();
			bufferedReader.close();
			bufferedWriter.close();
		} catch (Exception e) {
			writeStringFile(Data,filePath);
		}
	}
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页