Java基础之写文件——创建通道并且写文件(TryChannel)
控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中。
import static java.nio.file.StandardOpenOption.*;
import java.nio.channels.WritableByteChannel;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.EnumSet;
import java.nio.file.*; public class TryChannel { public static void main(String[] args) {
String[] sayings = {"The more you plan the luckier you get.",
"The time to complete a project is the time one person would need to complete it " +
"multiplied by the number of people on the project.",
"If at first you don't succeed, remove any evidence that you tried.",
"A clever person solves problems, a wise person avoids them.",
"Death is nature's way of telling you to slow down.",
"A hen is an egg's way of making other eggs.",
"The earlier you begin coding the later you finish.",
"Anything you can't understand is intuitively obvious."}; String separator = System.lineSeparator(); Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("MoreSaying.txt"); // The file path
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);
}
System.out.println("New file is: " + file);
ByteBuffer buf = null; // Buffer to hold a saying
try(WritableByteChannel channel = Files.newByteChannel(file, EnumSet.of(CREATE, WRITE))){ // Write sayings to the file
for(String saying : sayings) {
buf = ByteBuffer.wrap((saying + separator).getBytes()); // Saying & separator in buffer
channel.write(buf);
}
System.out.println("File written.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java基础之写文件——创建通道并且写文件(TryChannel)的更多相关文章
- 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件
原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...
- Java 基础(四)| IO 流之使用文件流的正确姿势
为跳槽面试做准备,今天开始进入 Java 基础的复习.希望基础不好的同学看完这篇文章,能掌握泛型,而基础好的同学权当复习,希望看完这篇文章能够起一点你的青涩记忆. 一.什么是 IO 流? 想象一个场景 ...
- Java基础——NIO(一)通道与缓冲区
一.概述 1.什么是NIO NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多. 在Java ...
- Java基础之读文件——使用通道随机读写文件(RandomReadWrite)
控制台程序,使用通道随机读写primes_backup.bin文件. import static java.nio.file.StandardOpenOption.*; import java.nio ...
- Java基础之读文件——使用通道随机读取文件(RandomFileRead)
import java.nio.file.*; import java.nio.channels.FileChannel; import java.io.IOException; import jav ...
- Java基础-多线程-①线程的创建和启动
简单阐释进程和线程 对于进程最直观的感受应该就是“windows任务管理器”中的进程管理: (计算机原理课上的记忆已经快要模糊了,简单理解一下):一个进程就是一个“执行中的程序”,是程序在计算机上的一 ...
- JAVA基础之——使用idea创建maven项目 以及使用tomcat本地调试springmvc
前言:关于这个话题网上有很多,本文旨在引导实战纠偏,理论偏少,如果按照步骤还不能达到本文目的,请留言. 1 环境准备 1.1 软件准备 idea:官方下载社区版,下载后安装 maven:Apache- ...
- Java基础 FileInputStream/ FileOutputStream / 字节输入流 字节输出流实现文件的复制
FileInputStream/FileOutputStream的笔记: /**(FileInputStream/FileOutputStream四个步骤: ①声明②加载地址③read/write④c ...
- Java基础教程——多线程:创建线程
多线程 进程 每一个应用程序在运行时,都会产生至少一个进程(process). 进程是操作系统进行"资源分配和调度"的独立单位. Windows系统的"任务管理器&quo ...
随机推荐
- PL/SQL Developer 显示中文乱码问题
简单版本: 首先,通过 select userenv('language') from dual; 查询oracle服务器端的编码,如为:AMERICAN_AMERICA.ZHS16GBK; 在我们的 ...
- WPF 最大化最小化窗口
public static void FullOrMin(this Window window) { //如果是全屏,则最小化 if (win ...
- w win cmd VS broswer
php -f Fetch data from db , use php without any bugs to analyse the data and read and write db, with ...
- 12秒开机!ExpressCache SSD缓存加速
SSD固态硬盘的读写速度比传统硬盘快了很多,读取速度能到300M/s 写入速度大约在80M/S 但SSD硬盘的价格也笔记机械硬盘高了很多,128G的固态硬盘淘宝价大概在800左右,想想现在随便一个软件 ...
- App之百度云推送
集成SDK 下载最新的Android SDK压缩包并解压,在新建工程或已有工程中增加百度云推送功能. 我下载的是 ,里面有一个同名的文件夹,文件夹中有 导入云推送jar包和so文件: 将解压后的lib ...
- 由验证码和session丢失的引发原因
今天中午,突然完整验证码全部不能显示了,查看gd库是没问题的,然后发现网站登录不上,追查变天,无果. 然后两个小时过后,可能是网站压力小了,网站又恢复正常. 但是过一阵又来. 其实很明显不是代码的问题 ...
- AppDelegate
一.基础知识 1) main.m指定了程序的入口点 UIApplicationMain(argc, argv,nil,NSStringFromClass([StartingPointAppDelega ...
- Android App罕见错误和优化方案
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1.App如果被定义一个有参数构造函数,那么需要再定义一个无参数的,如果不则会在某些情况下初始化失败 ...
- Linux-modules software
简介 这里指的modules不是linux内核相关的module,只是用于软件多版本控制的一个开源软件包,比如说系统同时有neo4j的不同版本,使用modules软件就可以使得在需要的时候选择相应的软 ...
- SQL Server 2008 R2,显示SQL语句执行窗口。 编辑前200行,可以执行SQL语句