控制台程序,将一列字符串写入到文件中。

 import java.io.*;
import java.nio.file.*;
import java.nio.charset.Charset; public class WriterOutputToFile {
public static void main(String[] args) {
String[] sayings = {"A nod is as good as a wink to a blind horse.",
"Least said, soonest mended.",
"There are 10 kinds of people in the world, " +
"those that understand binary and those that don't.",
"You can't make a silk purse out of a sow's ear.",
"Hindsight is always twenty-twenty.",
"Existentialism has no future.",
"Those who are not confused are misinformed.",
"Better untaught that ill-taught."}; Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("Sayings.txt");
try {
// Create parent directory if it doesn't exist
Files.createDirectories(file.getParent());
} catch(IOException e) {
System.err.println("Error creating directory: " + file.getParent());
e.printStackTrace();
System.exit(1);
}
try(BufferedWriter fileOut = Files.newBufferedWriter(
file, Charset.forName("UTF-16"))){
// Write saying to the file
for(int i = 0 ; i < sayings.length ; ++i) {
fileOut.write(sayings[i], 0, sayings[i].length());
fileOut.newLine(); // Write separator
}
System.out.println("File written.");
} catch(IOException e) {
System.err.println("Error writing file: " + file);
e.printStackTrace();
}
}
}

Java基础之写文件——使用带缓冲的Writer写文件(WriterOutputToFile)的更多相关文章

  1. Java基础-IO流对象之字符缓冲流(BufferedWriter与BufferedReader)

    Java基础-IO流对象之字符缓冲流(BufferedWriter与BufferedReader) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符缓冲流 字符缓冲流根据流的 ...

  2. Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)

    Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我们学习字 ...

  3. Java 基础 IO流(转换流,缓冲)

    一,前言 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputStr ...

  4. 如何将txt文件转换为带章节目录的mobi文件

    txt文件基本没什么排版可言.所以想要把txt转换为mobi文件方便阅读. 具体步骤如下: 打开txt 用notepad++打开所需要转换的txt文件.(或者使用其他的能够支持正则表达式的编辑器). ...

  5. Java基础—IO小结(二)缓冲流与其它流的使用

    一.缓冲流的使用 每个字节流都有对应的缓冲流: BufferedInputStream / BufferedOutputStream 构造器: 方法摘要与对应节点流类似 使用缓冲流实现文件复制:实际中 ...

  6. 【Java基础】Java IO流的总结

    Java IO流分为输入流和输出流,而输入流和输出流中又分字符流和字节流.顾名思义,输入流则是输入到程序中计算,输出流是把程序的结果输出到文件或者设备.而字符流输入输出以字符为单位,字节流则是以字节为 ...

  7. Java基础知识总结(超级经典)

    Java基础知识总结(超级经典) 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java ...

  8. 毕向东—Java基础知识总结(超级经典)

    Java基础知识总结(超级经典) 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java ...

  9. Java基础总结--IO总结1

    1.IO流(数据流)主要应用概述数据来源:存储在设备里面* IO流用来处理设备间数据之间的传输* Java对数据的操作是通过流的方式* Java用于对流的操作的对象都在IO包* 流按照流向分为:输出流 ...

随机推荐

  1. (转)经典SQL查询语句大全

    (转)经典SQL查询语句大全 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql s ...

  2. Bootstrap页面布局15 - BS带下拉菜单的按钮

    带下拉菜单的按钮 <div class='btn-toolbar'> <div class='btn-group'> <a href='javascript:;' cla ...

  3. PHP学习(三)----面向对象

    首先,还是建立一个好的理解模型: 1.什么是面向对象? 面向对象分为两个部分,那就是:什么是对象和什么是面向? 什么是对象: 对象的出现就是为了用代码更好的绘制我们现有的世界.那到底什么是对象呢? 一 ...

  4. 使用 Linux 搭建 VPN

    http://blog.csdn.net/catoop/article/details/7537012 VPN服务器的配置与应用 实验场景 通过将Linux配置VPN服务器允许远程计算机能够访问内网. ...

  5. overloading

    Computer Science An Overview _J. Glenn Brookshear _11th Edition_C Many programming languages allow t ...

  6. Greedy_algorithm

    https://en.wikipedia.org/wiki/Greedy_algorithm

  7. Demonstrating One-Shot Execution TimerTask Timer

    Listing -. Demonstrating One-Shot Execution import java.util.Timer; import java.util.TimerTask; publ ...

  8. ubuntu 制作deb 包

    ubuntu下打包制作deb安装包 http://www.th7.cn/system/lin/201406/61012.shtml   2014-06-22 20:16:45CSDN-yangbing ...

  9. [转]正则表达式相关:C# 抓取网页类(获取网页中所有信息)

    using System; using System.Data; using System.Configuration; using System.Net; using System.IO; usin ...

  10. [ASP.NET] Dictionary 和 Hashtable 区别

    Dictionary和Hashtable 是两个比较常用的表示键/值的集合,两者在实际使用过程中有何区别呢? 具体区别如下: 1. Hashtable不支持泛型,而Dictionary支持泛型. 2. ...