现在算算已经做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. “SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。”

    原因: .NET中  DateTime最小值为: 0001-1-1 0:00:00 数据库中DateTime最小值为: 1753-1-1 0:00:00, 很明显:.NET中的最小值超出了数据库时间类 ...

  2. Web 组合查询加 分页

    使用ADO.NET 数据访问技术制作web端组合查询加分页的功能关键在于查询SQL语句的拼接 以Car 表为例 每页显示3条数据 数据访问类使用查询方法,tsql 查询的连接字符串,查询的参数放到Ha ...

  3. CSS画猪

    效果查看:http://hovertree.com/texiao/css3/6/ CSS3画猪头: http://hovertree.com/texiao/css3/6/1/ 代码如下: <!D ...

  4. FASTSOCKET

    FASTSOCKET It looks like there are like 3 separate optimizations, but I think the most important one ...

  5. SSRF安全威胁在JAVA代码中的应用

    如上图所示代码,在进行外部url调用的时候,引入了SSRF检测:ssrfChecker.checkUrlWithoutConnection(url)机制. SSRF安全威胁:   很多web应用都提供 ...

  6. UDS(ISO14229-2006) 汉译(No.2参考标准)

    下列参考文件对本文件的系统是不可或缺的.注明日期的参考,仅关于对其引用的版本适用.未注明日期的,仅最新引用的文档(包括任何修改)适用. ISO 7498-1,信息技术——开放系统互联(OSI)——基本 ...

  7. c中使用gets() 提示warning: this program uses gets(), which is unsafe.

    今天在C代码中使用gets()时提示“warning: this program uses gets(), which is unsafe.”,然后这个程序还能运行,无聊的我开始查阅资料,为啥gets ...

  8. thinkcmf 常用操作

    11-16  thinkcmf 核心文件结构:simplewind--model--lite--model.class.php /controller.class.php Mobile---contr ...

  9. jQuery实现选项联动轮播

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. IE7浏览器窗口大小改变事件执行多次bug(转)

    var resizeTimer = null; $(window).resize(function() { if (resizeTimer) clearTimeout(resizeTimer); re ...