java常用的文件读写操作
现在算算已经做java开发两年了,回过头想想还真是挺不容易的,java的东西是比较复杂但是如果基础功扎实的话能力的提升就很快,这次特别整理了点有关文件操作的常用代码和大家分享
1.文件的读取(普通方式)
(1)第一种方法
- File f=new File("d:"+File.separator+"test.txt");
- InputStream in=new FileInputStream(f);
- byte[] b=new byte[(int)f.length()];
- int len=0;
- int temp=0;
- while((temp=in.read())!=-1){
- b[len]=(byte)temp;
- len++;
- }
- System.out.println(new String(b,0,len,"GBK"));
- in.close();
这种方法貌似用的比较多一点
(2)第二种方法
- File f=new File("d:"+File.separator+"test.txt");
- InputStream in=new FileInputStream(f);
- byte[] b=new byte[1024];
- int len=0;
- while((len=in.read(b))!=-1){
- System.out.println(new String(b,0,len,"GBK"));
- }
- in.close();
2.文件读取(内存映射方式)
- File f=new File("d:"+File.separator+"test.txt");
- FileInputStream in=new FileInputStream(f);
- FileChannel chan=in.getChannel();
- MappedByteBuffer buf=chan.map(FileChannel.MapMode.READ_ONLY, 0, f.length());
- byte[] b=new byte[(int)f.length()];
- int len=0;
- while(buf.hasRemaining()){
- b[len]=buf.get();
- len++;
- }
- chan.close();
- in.close();
- System.out.println(new String(b,0,len,"GBK"));
这种方式的效率是最好的,速度也是最快的,因为程序直接操作的是内存
3.文件复制(边读边写)操作
(1)比较常用的方式
- package org.lxh;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class ReadAndWrite {
- public static void main(String[] args) throws Exception {
- File f=new File("d:"+File.separator+"test.txt");
- InputStream in=new FileInputStream(f);
- OutputStream out=new FileOutputStream("e:"+File.separator+"test.txt");
- int temp=0;
- while((temp=in.read())!=-1){
- out.write(temp);
- }
- out.close();
- in.close();
- }
- }
(2)使用内存映射的实现
- package org.lxh;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- public class ReadAndWrite2 {
- public static void main(String[] args) throws Exception {
- File f=new File("d:"+File.separator+"test.txt");
- FileInputStream in=new FileInputStream(f);
- FileOutputStream out=new FileOutputStream("e:"+File.separator+"test.txt");
- FileChannel fin=in.getChannel();
- FileChannel fout=out.getChannel();
- //开辟缓冲
- ByteBuffer buf=ByteBuffer.allocate(1024);
- while((fin.read(buf))!=-1){
- //重设缓冲区
- buf.flip();
- //输出缓冲区
- fout.write(buf);
- //清空缓冲区
- buf.clear();
- }
- fin.close();
- fout.close();
- in.close();
- out.close();
- }
- }
java常用的文件读写操作的更多相关文章
- Java 字节流实现文件读写操作(InputStream-OutputStream)
Java 字节流实现文件读写操作(InputStream-OutputStream) 备注:字节流比字符流底层,但是效率底下. 字符流地址:http://pengyan5945.iteye.com/b ...
- Golang: 常用的文件读写操作
Go 语言提供了很多文件操作的支持,在不同场景下,有对应的处理方式,今天就来系统地梳理一下,几种常用的文件读写的形式. 一.读取文件内容 1.按字节读取文件 这种方式是以字节为单位来读取,相对底层一些 ...
- Java 字符流实现文件读写操作(FileReader-FileWriter)
Java 字符流实现文件读写操作(FileReader-FileWriter) 备注:字符流效率高,但是没有字节流底层 字节流地址:http://pengyan5945.iteye.com/blog/ ...
- java文件读写操作类
借鉴了项目以前的文件写入功能,实现了对文件读写操作的封装 仅仅需要在读写方法传入路径即可(可以是绝对或相对路径) 以后使用时,可以在此基础上改进,比如: 写操作: 1,对java GUI中文本框中的内 ...
- [转]Android - 文件读写操作 总结
转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...
- Kotlin入门(27)文件读写操作
Java的文件处理用到了io库java.io,该库虽然功能强大,但是与文件内容的交互还得通过输入输出流中转,致使文件读写操作颇为繁琐.因此,开发者通常得自己重新封装一个文件存取的工具类,以便在日常开发 ...
- Kotlin入门-文件读写操作
转 https://blog.csdn.net/aqi00/article/details/83241762 Java的文件处理用到了io库java.io,该库虽然功能强大,但是与文件内容的交互还得通 ...
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- PHP文件读写操作之文件写入代码
在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基本的PHP开发 ...
随机推荐
- “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中的最小值超出了数据库时间类 ...
- Web 组合查询加 分页
使用ADO.NET 数据访问技术制作web端组合查询加分页的功能关键在于查询SQL语句的拼接 以Car 表为例 每页显示3条数据 数据访问类使用查询方法,tsql 查询的连接字符串,查询的参数放到Ha ...
- CSS画猪
效果查看:http://hovertree.com/texiao/css3/6/ CSS3画猪头: http://hovertree.com/texiao/css3/6/1/ 代码如下: <!D ...
- FASTSOCKET
FASTSOCKET It looks like there are like 3 separate optimizations, but I think the most important one ...
- SSRF安全威胁在JAVA代码中的应用
如上图所示代码,在进行外部url调用的时候,引入了SSRF检测:ssrfChecker.checkUrlWithoutConnection(url)机制. SSRF安全威胁: 很多web应用都提供 ...
- UDS(ISO14229-2006) 汉译(No.2参考标准)
下列参考文件对本文件的系统是不可或缺的.注明日期的参考,仅关于对其引用的版本适用.未注明日期的,仅最新引用的文档(包括任何修改)适用. ISO 7498-1,信息技术——开放系统互联(OSI)——基本 ...
- c中使用gets() 提示warning: this program uses gets(), which is unsafe.
今天在C代码中使用gets()时提示“warning: this program uses gets(), which is unsafe.”,然后这个程序还能运行,无聊的我开始查阅资料,为啥gets ...
- thinkcmf 常用操作
11-16 thinkcmf 核心文件结构:simplewind--model--lite--model.class.php /controller.class.php Mobile---contr ...
- jQuery实现选项联动轮播
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- IE7浏览器窗口大小改变事件执行多次bug(转)
var resizeTimer = null; $(window).resize(function() { if (resizeTimer) clearTimeout(resizeTimer); re ...