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 ...
随机推荐
- 自动配置IP地址.bat
※※※※※※※※※※※※※※※※※※※※※※※※※※※※ @echo ※ ※ @echo ...
- VS 本机调试
VS~通过IIS网站启用"域名"调试 在我们开发网站时,对某些信息进行序列化时,通常使用session,cookies,nosql等技术,而为了安全,我们在服务器上很多情况都做了防 ...
- 1.PHP内核探索:从SAPI接口开始
SAPI:Server Application Programming Interface 服务器端应用编程端口.研究过PHP架构的同学应该知道这个东东的重要性,它提供了一个接口,使得PHP可以和其他 ...
- 耦合 Coupling the object-oriented paradigm && data coupling
Computer Science An Overview _J. Glenn Brookshear _11th Edition 耦 两个人一起耕地 one of the benefits of the ...
- ATT 汇编语法
在研华的pc104上使用看门狗要使用汇编.使用汇编来修改CMOS里面的参数.也就是内联汇编.linux下gcc只支持ATT汇编.所以这儿有必要将ATT语法学习学习.以后需要的时候翻出来温习温习. ...
- Flink - Working with State
All transformations in Flink may look like functions (in the functional processing terminology), but ...
- Bootstrap 模态框插件
一.基本使用 使用模态框的弹窗组件需要三层 div 容器元素,分别为 modal(模态声明层). dialog(窗口声明层).content(内容层).在内容层里面,还有三层,分别为 header(头 ...
- java NIO ;mvn
http://ifeve.com/java-nio-scattergather/ mvn introduction(install,conf, proxy,plugin,samples) http:/ ...
- android部分机型(HTC D610) menu键的显示问题
今天遇到了一个很恶心的问题... htc某些机器的menu键是在屏幕里的,可以由系统控制显示和隐藏.今天遇到了一个问题,有两个应用,一个运行后显示menu键,另一个不显示... 找了好一会儿,发现是 ...
- RTSP协议详解
RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议.RTSP对流媒体提 ...