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开发 ...
随机推荐
- Sql Server函数全解(二)数学函数
数学函数主要用来处理数值数据,主要的数学函数有:绝对值函数,三角函数(包括正弦函数,余弦函数,正切函数,余切函数).对数函数,随机函数等.在错误产生时,数学函数将返回空值null.本次介绍各种数学 ...
- “全能”选手—Django 1.10文档中文版Part4
第一部分传送门 第二部分传送门 第三部分传送门 3.2 模型和数据库Models and databases 3.2.2 查询操作making queries 3.3.8 会话sessions 2.1 ...
- 1.什么是Code First(EF Code First 系列)
EF4.1中开始支持Code First .这种方式在领域设计模式中非常有用.使用Code First模式,你可以专注于领域设计,根据需要,为你一个领域的对象创建类集合,而不是首先来设计数据库,然后来 ...
- 7.5 数据注解特性--MaxLength&&MinLength
MaxLength attribute can be applied to a string or array type property of a domain class. EF Code Fir ...
- jQuery对话框右上角带关闭×
jQuery弹出可关闭遮罩层:http://hovertree.com/texiao/layer/1/ 代码如下: <!DOCTYPE html> <html> <hea ...
- spring Mvc + Mybatis 中使用junit
在Spring Mvc + Mybatis的项目中我们有时候需要在测试代码中注入Dao操作数据库,对表进行增删改查,实现如下: 这是一般的maven项目项目结构 测试代码一般写在src/test/ja ...
- AJAX与PHP(PHP笔记)--动态验证用户名
在PHP基础的学习过程中经常会遇到对页面的局部刷新. 比如说,我们在填写用户名的同时,对数据库中的信息进行验证,检查是否充分. 这时就要用到AJAX实现页面的动态加载. 下面例子是简单的PHP与AJA ...
- php和js中json的编码和解码
php中 1)编码 $jsonStr = json_encode($array) 2)解码 $arr = json_decode($jsonStr) <?php echo json_encode ...
- durex-word
"(半夜没睡着) “你是不是饿了,哎呀我也饿了.”" "(聊到合拍处) “我和你有一万句me too想要说.”" "(异地恋) “我辞职,去你那儿吧! ...
- js中this的绑定
人们对于this的绑定常常有两个误解,一:指向函数本身,二:指向函数作用域.这两种想法都是错的,this并不指向函数本身,也不指向函数作用域. function foo(){ this.count++ ...