现在算算已经做java开发两年了,回过头想想还真是挺不容易的,java的东西是比较复杂但是如果基础功扎实的话能力的提升就很快,这次特别整理了点有关文件操作的常用代码和大家分享

1.文件的读取(普通方式)

(1)第一种方法

  1. File f=new File("d:"+File.separator+"test.txt");
  2. InputStream in=new FileInputStream(f);
  3. byte[] b=new byte[(int)f.length()];
  4. int len=0;
  5. int temp=0;
  6. while((temp=in.read())!=-1){
  7. b[len]=(byte)temp;
  8. len++;
  9. }
  10. System.out.println(new String(b,0,len,"GBK"));
  11. in.close();

这种方法貌似用的比较多一点

(2)第二种方法

  1. File f=new File("d:"+File.separator+"test.txt");
  2. InputStream in=new FileInputStream(f);
  3. byte[] b=new byte[1024];
  4. int len=0;
  5. while((len=in.read(b))!=-1){
  6. System.out.println(new String(b,0,len,"GBK"));
  7. }
  8. in.close();

2.文件读取(内存映射方式)

  1. File f=new File("d:"+File.separator+"test.txt");
  2. FileInputStream in=new FileInputStream(f);
  3. FileChannel chan=in.getChannel();
  4. MappedByteBuffer buf=chan.map(FileChannel.MapMode.READ_ONLY, 0, f.length());
  5. byte[] b=new byte[(int)f.length()];
  6. int len=0;
  7. while(buf.hasRemaining()){
  8. b[len]=buf.get();
  9. len++;
  10. }
  11. chan.close();
  12. in.close();
  13. System.out.println(new String(b,0,len,"GBK"));

这种方式的效率是最好的,速度也是最快的,因为程序直接操作的是内存

3.文件复制(边读边写)操作

(1)比较常用的方式

  1. package org.lxh;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. public class ReadAndWrite {
  8. public static void main(String[] args) throws Exception {
  9. File f=new File("d:"+File.separator+"test.txt");
  10. InputStream in=new FileInputStream(f);
  11. OutputStream out=new FileOutputStream("e:"+File.separator+"test.txt");
  12. int temp=0;
  13. while((temp=in.read())!=-1){
  14. out.write(temp);
  15. }
  16. out.close();
  17. in.close();
  18. }
  19. }

(2)使用内存映射的实现

    1. package org.lxh;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.nio.ByteBuffer;
    6. import java.nio.channels.FileChannel;
    7. public class ReadAndWrite2 {
    8. public static void main(String[] args) throws Exception {
    9. File f=new File("d:"+File.separator+"test.txt");
    10. FileInputStream in=new FileInputStream(f);
    11. FileOutputStream out=new FileOutputStream("e:"+File.separator+"test.txt");
    12. FileChannel fin=in.getChannel();
    13. FileChannel fout=out.getChannel();
    14. //开辟缓冲
    15. ByteBuffer buf=ByteBuffer.allocate(1024);
    16. while((fin.read(buf))!=-1){
    17. //重设缓冲区
    18. buf.flip();
    19. //输出缓冲区
    20. fout.write(buf);
    21. //清空缓冲区
    22. buf.clear();
    23. }
    24. fin.close();
    25. fout.close();
    26. in.close();
    27. out.close();
    28. }
    29. }

java常用的文件读写操作的更多相关文章

  1. Java 字节流实现文件读写操作(InputStream-OutputStream)

    Java 字节流实现文件读写操作(InputStream-OutputStream) 备注:字节流比字符流底层,但是效率底下. 字符流地址:http://pengyan5945.iteye.com/b ...

  2. Golang: 常用的文件读写操作

    Go 语言提供了很多文件操作的支持,在不同场景下,有对应的处理方式,今天就来系统地梳理一下,几种常用的文件读写的形式. 一.读取文件内容 1.按字节读取文件 这种方式是以字节为单位来读取,相对底层一些 ...

  3. Java 字符流实现文件读写操作(FileReader-FileWriter)

    Java 字符流实现文件读写操作(FileReader-FileWriter) 备注:字符流效率高,但是没有字节流底层 字节流地址:http://pengyan5945.iteye.com/blog/ ...

  4. java文件读写操作类

    借鉴了项目以前的文件写入功能,实现了对文件读写操作的封装 仅仅需要在读写方法传入路径即可(可以是绝对或相对路径) 以后使用时,可以在此基础上改进,比如: 写操作: 1,对java GUI中文本框中的内 ...

  5. [转]Android - 文件读写操作 总结

     转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...

  6. Kotlin入门(27)文件读写操作

    Java的文件处理用到了io库java.io,该库虽然功能强大,但是与文件内容的交互还得通过输入输出流中转,致使文件读写操作颇为繁琐.因此,开发者通常得自己重新封装一个文件存取的工具类,以便在日常开发 ...

  7. Kotlin入门-文件读写操作

    转 https://blog.csdn.net/aqi00/article/details/83241762 Java的文件处理用到了io库java.io,该库虽然功能强大,但是与文件内容的交互还得通 ...

  8. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

  9. PHP文件读写操作之文件写入代码

    在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基本的PHP开发 ...

随机推荐

  1. Sql Server函数全解(二)数学函数

      数学函数主要用来处理数值数据,主要的数学函数有:绝对值函数,三角函数(包括正弦函数,余弦函数,正切函数,余切函数).对数函数,随机函数等.在错误产生时,数学函数将返回空值null.本次介绍各种数学 ...

  2. “全能”选手—Django 1.10文档中文版Part4

    第一部分传送门 第二部分传送门 第三部分传送门 3.2 模型和数据库Models and databases 3.2.2 查询操作making queries 3.3.8 会话sessions 2.1 ...

  3. 1.什么是Code First(EF Code First 系列)

    EF4.1中开始支持Code First .这种方式在领域设计模式中非常有用.使用Code First模式,你可以专注于领域设计,根据需要,为你一个领域的对象创建类集合,而不是首先来设计数据库,然后来 ...

  4. 7.5 数据注解特性--MaxLength&&MinLength

    MaxLength attribute can be applied to a string or array type property of a domain class. EF Code Fir ...

  5. jQuery对话框右上角带关闭×

    jQuery弹出可关闭遮罩层:http://hovertree.com/texiao/layer/1/ 代码如下: <!DOCTYPE html> <html> <hea ...

  6. spring Mvc + Mybatis 中使用junit

    在Spring Mvc + Mybatis的项目中我们有时候需要在测试代码中注入Dao操作数据库,对表进行增删改查,实现如下: 这是一般的maven项目项目结构 测试代码一般写在src/test/ja ...

  7. AJAX与PHP(PHP笔记)--动态验证用户名

    在PHP基础的学习过程中经常会遇到对页面的局部刷新. 比如说,我们在填写用户名的同时,对数据库中的信息进行验证,检查是否充分. 这时就要用到AJAX实现页面的动态加载. 下面例子是简单的PHP与AJA ...

  8. php和js中json的编码和解码

    php中 1)编码 $jsonStr = json_encode($array) 2)解码 $arr = json_decode($jsonStr) <?php echo json_encode ...

  9. durex-word

    "(半夜没睡着) “你是不是饿了,哎呀我也饿了.”" "(聊到合拍处) “我和你有一万句me too想要说.”" "(异地恋) “我辞职,去你那儿吧! ...

  10. js中this的绑定

    人们对于this的绑定常常有两个误解,一:指向函数本身,二:指向函数作用域.这两种想法都是错的,this并不指向函数本身,也不指向函数作用域. function foo(){ this.count++ ...