io流相关总结
2018-12-30 22:28
40 查看
1.目录
- java.io.file类的使用
- io原理及流的分类
- 节点流
- 缓冲流
- 转换流
- 标准输入输出
- 打印流
- 数据流
- 对象流
- 随机存取文件
- NIO中的path,paths,file类的使用。
1. java.io.file类的使用
public class File extends Object implements Serializable, Comparable<File>
- 文件和目录的抽象表示形式,与平台无关
- file能新建,删除,重命名文件和目录,但是File不能访问文件的内容。
- 如果需要访问文件的内容,这时候就需要使用到输入,输出流。
- 路径名:
File file=new File("E:\\WorkSpace\\java\\Practice\\io\\hello.txt"); // 当然可以直接使用 File file=new File("E:/WorkSpace/java/Practice/io/hello.txt"); // 原因是在windows系统中,\是转义的意思
- 绝对路径:带盘符的:
E:\\WorkSpace
- 相对路径:相对于当前的工程来说的,
2. 字符流
- reader以及writer(抽象基类),一般处理的是“.txt”文件的,也属于解码
3. 字节流
- inputStream,OutputStream.(抽象基类)
实现类
- FileInputStream,FileOutputStream 属于文件流,也是字节流。
- BufferedInputStream,BufferedOutPutStream,处理流的一种,提高数据传输的速度。
! - 转换流
/** * @Auther: linghaoDo * @Date:Created at 2018/12/30 * @Description: 1. 流的分类 * 2. 流的流向 * 3. 操作的数据类型 * 4. 角色流(文件流) 处理流 * 5. */ public class FileTest { // File file=new File("E:\\WorkSpace\\java\\Practice\\io\\hello.txt"); public static void main(String[] args) { // FileTest.textFileInputStream1(); // FileTest.testFileOutputStream(); try { File file = new File("hello.txt"); createFixLengthFile(file, 2147483333); } catch (IOException e) { e.printStackTrace(); } } public static void textFileInputStream1() { FileInputStream fileInputStream = null; /*创建File类的对象*/ File file = new File("hell.txt"); /*2. 创建FileInputStream流的对象 拿到节点*/ try { fileInputStream = new FileInputStream(file); /*3. 读取数据的操作,这一步相当于指针的初始化*/ // int data=fileInputStream.read(); // while(data!=-1){ // /*4. 这一步相当于继续移动指针*/ // System.out.print((char) data); // data=fileInputStream.read(); // } byte[] buffer = new byte[5]; int len; while ((len = fileInputStream.read(buffer)) != -1) { /*读进去几个就取几个,不能使用buffer.length*/ // for(int i=0;i<len;i++){ // System.out.println((char) buffer[i]); // } /*方式二*/ String str = new String(buffer, 0, len); System.out.print(str); } } catch (IOException e) { e.printStackTrace(); } finally { /*流的资源需要自己手动关闭*/ if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void testFileOutputStream() { File file = new File("abc.txt"); FileOutputStream fos = null; byte[] buffer = "i love China i love Beijing".getBytes(); try { fos = new FileOutputStream(file); fos.write(buffer); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void testFileInputAndFileoutPut() { /*1. 提供源文件和目标文件*/ File src = new File("hello.txt"); File dec = new File("hello1.txt"); /*2. 创建输入流以及输出流*/ FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(src); fos = new FileOutputStream(dec); /*读取数据并写出*/ byte[] buffer = new byte[5]; int len; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } /*关闭资源,一般从下往上找*/ try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void createFixLengthFile(File file, long length) throws IOException { long start = System.currentTimeMillis(); FileOutputStream fos = null; FileChannel output = null; fos = new FileOutputStream(file); output = fos.getChannel(); output.write(ByteBuffer.allocate(1), length - 1); if (output != null) { output.close(); } if (fos != null) { fos.close(); } long end = System.currentTimeMillis(); System.out.println("total times" + (end - start)); } public static void create(File file, long length) throws IOException { long start = System.currentTimeMillis(); RandomAccessFile r = null; try { r = new RandomAccessFile(file, "rw"); r.setLength(length); } finally { if (r != null) { try { r.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = System.currentTimeMillis(); System.out.println(end - start); } }
public class BufferedTest { public static void main(String[] args){ } public static void testBuffered() throws IOException { File file=new File("abc.txt"); File dext=new File("hello.txt"); FileInputStream fis=null; FileOutputStream fos=null; fis=new FileInputStream(file); fos=new FileOutputStream(dext); /*创建缓冲流*/ BufferedInputStream bis=new BufferedInputStream(fis); BufferedOutputStream bos=new BufferedOutputStream(fos); /*读写操作*/ byte[] buffer=new byte[1024]; int len; while ((len=bis.read())!=-1){ bos.write(buffer,0,len); } /*关闭资源 从外往里关:形象的例子:穿衣服的时候是先穿里面的,脱衣服的时候是先脱外面的*/ bos.close(); bis.close(); fos.close(); fis.close(); } }
参考文献
相关文章推荐
- 黑马程序员:IO流相关知识总结
- Java基础回顾--java IO流相关的类的分类和总结 3 【Java File类中list()、listFiles()的使用及区别,认识和使用匿名内部类】
- java IO流相关的类的分类和总结
- Android 蓝牙开发相关知识总结
- java IO流总结篇(二)
- memcached服务及其相关库以及php的memcached扩展的安装总结
- 总结Mysql索引相关知识
- python 日期相关的各种操作总结
- VNC介绍及相关命令总结
- Java中IO流学习总结
- Android相关经验积累总结
- Android横竖屏切换相关总结
- 字符串数组初始化相关问题总结
- 有关UNICODE、ANSI字符集和相关字符串操作的总结!
- linux vdso与vsyscall相关内容总结
- 黑马程序员-Java中IO流知识点总结
- 【方法总结】创建、读取、删除文件相关操作
- linux platform总线的相关总结
- QT界面(控件)相关设计的一些技巧总结
- 性能测试、指标和优化 -- 性能相关总结