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 ...
随机推荐
- php获得网站根目录的几个方法
php获得网站根目录的几个方法 电脑软硬件应用网 45IT.COM 时间:2015-01-08 12:54 作者:佚名 在php中我们要得到网站根目录可以用很多全局变量实现了,如可以利用__file_ ...
- ecshop中$user对象
ecshop中$user对象 2011-10-01 18:41:23| 分类: ECSHOP | 标签:ecshop $user 对象 |举报|字号 订阅 http://blog.163.c ...
- HTML Questions:Front-end Developer Interview Questions
What's a doctype do? Instruct the browser to render the page. What's the difference between standard ...
- Solr高亮详解
hl.fl: 用空格或逗号隔开的字段列表.要启用某个字段的highlight功能,就得保证该字段在schema中是stored.如果该参数未被给出,那么就会高亮默认字段 standard handle ...
- lvs简单配置
负载均衡服务器将会用到两块网卡,eth0为公网地址(此处将局域网ip作为公网地址),IP地址为192.168.1.104,eth0:1,IP地址为192.168.2.1在负载均衡器上添加一个ip别名, ...
- php面向对象之__toString()
似曾相识,在php面向对象编程之魔术方法__set,曾经介绍了什么是魔术方法,这一章又介绍一个魔术方法__tostring(). __toString()是快速获取对象的字符串信息的便捷方式,似乎魔术 ...
- Java ArrayListSerialise
import java.io.*; import java.util.*; //ArrayListSerialise public class A { public static void main( ...
- Sql Server建立链接服务器访问Access的MDB数据库
EXEC master.dbo.sp_addlinkedserver @server = N'test', @srvproduct=N'OLE DB Provider for Jet', @provi ...
- (转)js闭包初入门
先看一段JS代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 function a(){ var num = 0; function ...
- php--数据库三范式
关系数据库的几种设计范式介绍1.第一范式(1NF) 在任何一个关系数据库中,第一范式(1NF)是对关系模式的基本要求,不满足第一范式(1NF)的数据库就不是关系数据库. 所谓第一范式(1NF)是指数据 ...