这两个都是打印流,第一个是字节打印流,第二个是字符打印流。都是用来将数据写到文本文件的。
类似于OutputSteam、BufferWriter、Writer
但是这个会更高效和简洁,因为里面封装了BufferWriter方法,所以在速度方面不比字符缓冲流慢,同时比字符缓冲流更简介
使用字符缓冲流将字符串的数据写到word.txt文件里:
public class a {
public static final Logger log = LoggerFactory.getLogger("test_01.class");
public static void main(String[] args) throws IOException {
// 需要写到文本文件中的字符串
String str = null;
if (true) {
str = "atch the star that holds your destiny, the one that forever twinkles within your heart. Take advantage of " +
"precious opportunities while they still sparkle before you. Always believe that your ultimate goal is attainable as long as you commit yourself to it.\n" +
"追随能够改变你命运的那颗星,那颗永远在你心中闪烁的明星。当它在你面前闪耀时,抓住这宝贵的机会。请谨记,只要你坚持不懈,最终的目标总能实现。\n" +
"Though barriers may sometimes stand in the way of your dreams, remember that your destiny is hiding behind them. " +
"Accept the fact that not everyone is going to approve of the choices you've made, " +
"have faith in your judgment, catch the star that twinkles in your heart, and it will " +
"lead you to your destiny's path. Follow that pathway and uncover the sweet sunrises that await you.\n" +
"尽管实现梦想的途中有时会遇到障碍,要知道这是命运对你的挑战。不是每个人都会赞成你的选择,接受这个现实,并相信自我的判断,追随那颗在你心中闪烁的明星," +
"它会引领你踏上命运的征途。坚持不懈,你就能享受那些幸福时刻。";
}
BufferedWriter bos = new BufferedWriter(new FileWriter("word.txt"));
long start_time = System.currentTimeMillis();
log.info("-------------开始-----------");
// 将str字符串的内容写到文本文件里
bos.write(str,0,str.length());
bos.close();
long end_time = System.currentTimeMillis();
log.info("共花了" + (end_time - start_time)/1000.0 + "毫秒" );
log.info("-------------结束-----------");
}
}
运行结果:
使用PrintWriter流将数据写到文本文件中:
public static void main(String[] args) throws IOException {
// 需要写到文本文件中的字符串
String str = null;
if (true) {
str = "atch the star that holds your destiny, the one that forever twinkles within your heart. Take advantage of " +
"precious opportunities while they still sparkle before you. Always believe that your ultimate goal is attainable as long as you commit yourself to it.\n" +
"追随能够改变你命运的那颗星,那颗永远在你心中闪烁的明星。当它在你面前闪耀时,抓住这宝贵的机会。请谨记,只要你坚持不懈,最终的目标总能实现。\n" +
"Though barriers may sometimes stand in the way of your dreams, remember that your destiny is hiding behind them. " +
"Accept the fact that not everyone is going to approve of the choices you've made, " +
"have faith in your judgment, catch the star that twinkles in your heart, and it will " +
"lead you to your destiny's path. Follow that pathway and uncover the sweet sunrises that await you.\n" +
"尽管实现梦想的途中有时会遇到障碍,要知道这是命运对你的挑战。不是每个人都会赞成你的选择,接受这个现实,并相信自我的判断,追随那颗在你心中闪烁的明星," +
"它会引领你踏上命运的征途。坚持不懈,你就能享受那些幸福时刻。";
}
PrintWriter bos = new PrintWriter(new FileWriter("word.txt"));
// PrintWriter bos = new PrintWriter("word.txt"); 可以直接写文件路径
long start_time = System.currentTimeMillis();
log.info("-------------开始-----------");
// 将str字符串的内容写到文本文件里
bos.write(str,0,str.length());
bos.close();
long end_time = System.currentTimeMillis();
log.info("共花了" + (end_time - start_time)/1000.0 + "毫秒" );
log.info("-------------结束-----------");
}
对于字符缓冲流,速度和打印流一样,对于字节缓冲流来说,字节打印流速度会更快而且更高效