io与nio的使用

package test;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;


//传统io和nio的区别
public class ioAndNio {
public static void main(String[] args)throws Exception{
//file代表文件或者是文档的路径,在不存在的情况下不会自己

//创建,需要使用mkdirs()或者是createNewFile()来创建

                //创建文本文件的时候必须事先有创建好的文佳夹

                //创建文件夹的时候不需要,若是没有,它会一层一层的创建

                //mkdirs()是创建多个文件夹,而mkdir是单个文件夹

// File file = new File("e:\\io.txt");
// File file2 = new File(file, "io2.txt");
// if(!file.exists()){
// //创一个文本文档
file.createNewFile();
// //创建文件夹
// file.mkdir();
// }
// file2.mkdirs();
// System.out.println(file.exists());
//下面的输出是一样的不同的是第一个返回值绝对路径的String字符串
//第二个返回值代表这个路径的file对象
// System.out.println(file2.getAbsolutePath());
// System.out.println(file2.getAbsoluteFile());
// System.out.println(file2.getCanonicalPath());
// System.out.println(File.listRoots());
//下面的语句返回盘符的根目录
// for(File file3 : File.listRoots()){
// System.out.println(file3.getAbsoluteFile());
// }
// System.out.println(file.getClass().getClassLoader().getResource("/src/t1.java"));
// System.out.println(file.getTotalSpace());
// for(String s : file.list()){
// System.out.println(s.toString());
// }
// String[] namelist = file.list(new FilenameFilter() {
// @Override
// public boolean accept(File dir, String name) {
// if(name.endsWith(".txt")){
// return true;
// }
//
// return false;
// }
// });
// for(String string : namelist){
// System.out.println(string);
// }
// }
//其他的流跟下面的用法一样不过是访问的对象不同
// FileInputStream fileInputStream = new FileInputStream(new File("E:\\io.txt\\ios.txt"));
// //对于流来说若文件不存在则会自动的创建该文件
// FileOutputStream fileOutputStream = new FileOutputStream(new File("e:\\io.txt\\ios1.txt"));
// byte[] by = new byte[1024];
// int len = 0;
// while((len = fileInputStream.read(by))!=-1){
// try{
// fileOutputStream.write(by, 0, len);
// }catch(Exception e){}finally{fileInputStream.close();fileOutputStream.close();}
// }
// //下面的语句是用来访问数组的
// ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(new byte[1024]);
//randomAccesssFile,任意访问文件
// File file = new File("e:\\io.txt\\ios.txt");
// RandomAccessFile accessFile = new RandomAccessFile(file, "rw");
// byte[] b = new byte[1024];
// while(accessFile.read(b))
//使用nio
//nio都是非阻塞式的io,传统io都是阻塞式的io,
//在没有读取到数据的时候该线程就会一直处于
//阻塞状态
//从输入流中获取到的channel只能执行读操作
// File position = new File("e:\\io.txt\\ios1.txt");
// FileChannel channel = new FileInputStream(position).getChannel();
// //下面的语句是将文件中的内容全部的存进buffer中
// ByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0, position.length());
// while(buffer.hasRemaining()){
// System.out.println((char)buffer.get());
// }
//使用channel来读写文本数据
// FileChannel channel = 
// new FileInputStream(new File("e:\\io.txt\\channel.txt")).getChannel();
// FileChannel channel2 = 
// new FileOutputStream(new File("e:\\io.txt\\channels.txt")).getChannel();
// ByteBuffer buffer = 
// channel.map(MapMode.READ_ONLY, 0, 
// new File("e:\\io.txt\\channel.txt").length());
// channel2.write(buffer);
// //因为已经执行过write操作所以buffer中的内容全部输出掉了,因此不能再次读取到内容
// Charset charset = Charset.forName("GBK");
// CharBuffer buffer2 = charset.decode(buffer);
buffer2.flip();
// while(buffer2.hasRemaining()){
// System.out.println("元素:"+buffer2.get());
// }
//向源文件中追加数据,使用randomAccessFile来进行文本的读写
FileChannel channel = 
new RandomAccessFile(new File("e:\\io.txt\\channel.txt"), "rw").getChannel();
//将文件的指针移向最后面
channel.position(new File("e:\\io.txt\\channel.txt").length());
ByteBuffer buffer = ByteBuffer.allocate(1024);
String s = "最新追加的内容";
buffer.put(s.getBytes());
buffer.flip();
channel.write(buffer);
}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值