java io (一)
对于文件内容的操作主要分为两大类
分别是:字符流
字节流
其中,字符流有两个抽象类:Writer Reader
其对应子类FileWriter和FileReader可实现文件的读写操作
BufferedWriter和BufferedReader能够提供缓冲区功能,用以提高效率
同样,字节流也有两个抽象类:InputStream OutputStream
其对应子类有FileInputStream和FileOutputStream实现文件读写
BufferedInputStream和BufferedOutputStream提供缓冲区功能
初学者在学会使用字符流和字节流之后未免会产生疑问:什么时候该使用字符流,什么时候又该使用字节流呢?
其实仔细想想就应该知道,所谓字符流,肯定是用于操作类似文本文件或者带有字符文件的场合比较多
而字节流则是操作那些无法直接获取文本信息的二进制文件,比如图片,mp3,视频文件等
说白了在硬盘上都是以字节存储的,只不过字符流在操作文本上面更方便一点而已
此外,为什么要利用缓冲区呢?
我们知道,像迅雷等下载软件都有个缓存的功能,硬盘本身也有缓冲区
试想一下,如果一有数据,不论大小就开始读写,势必会给硬盘造成很大负担,它会感觉很不爽
人不也一样,一顿饭不让你一次吃完,每分钟喂一勺,你怎么想?
因此,采用缓冲区能够在读写大文件的时候有效提高效率
字符流 写入
package com.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class fileTest {
public static void main(String[] args) {
//创建要操作的文件路径和名称
//其中,File.separator表示系统相关的分隔符,Linux下为:/ Windows下为:\\
String path ="d:"+ File.separator +"love"+File.separator+ "file.txt" ;//d:\\love\\file.txt love文件夹必须存在
FileWriter w = null;
try {
//以path为路径创建一个新的FileWriter对象
//如果需要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法
w = new FileWriter(path);
//将字符串写入到流中,\r\n表示换行想有好的
//循环写入txt中 换行
for (int i = 0; i < 5; i++) {
String s = "我爱曼曼!!! \r\n";
w.write(s);
}
System.out.println("完毕");
//如果想马上看到写入效果,则需要调用w.flush()方法
w.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
//如果前面发生异常,那么是无法产生w对象的
//因此要做出判断,以免发生空指针异常
if(w != null) {
try {
//关闭流资源,需要再次捕捉异常
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字节流写入
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamWirte {
public static void main(String[] args) {
String path ="d:"+ File.separator + "file11.txt" ;
FileOutputStream o = null;
try {
o = new FileOutputStream(path);
String str = "Nerxious is a good boy\r\n";
byte[] buf = str.getBytes();
//也可以直接使用o.write("String".getBytes());
//因为字符串就是一个对象,能直接调用方法
o.write(buf);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(o != null) {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字符流读取
package com.test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class fileRead {
public static void main(String[] args) {
String path ="d:\\dd.txt";
FileReader r = null;
try {
r = new FileReader(path);
//方式一:读取单个字符的方式
//每读取一次,向下移动一个字符单位
/*int temp1 = r.read();
System.out.println((char)temp1);
int temp2 = r.read();
System.out.println((char)temp2);*/
//方式二:循环读取
//read()方法读到文件末尾会返回-1
/*while (true) {
int temp = r.read();
if (temp == -1) {
break;
}
System.out.print((char)temp);
}*/
//方式三:循环读取的简化操作
//单个字符读取,当temp不等于-1的时候打印字符
int temp = 0;
while ((temp = r.read()) != -1) {
System.out.print((char)temp);
}
//方式四:读入到字符数组
/*
char[] buf = new char[1024];
int temp = r.read(buf);
//将数组转化为字符串打印,后面参数的意思是
//如果字符数组未满,转化成字符串打印后尾部也许会出现其他字符
//因此,读取的字符有多少个,就转化多少为字符串
System.out.println(new String(buf,0,temp));
*/
//方式五:读入到字符数组的优化
//由于有时候文件太大,无法确定需要定义的数组大小
//因此一般定义数组长度为1024,采用循环的方式读入
/*
char[] buf = new char[1024];
int temp = 0;
while((temp = r.read(buf)) != -1) {
System.out.print(new String(buf,0,temp));
}
*/
} catch (IOException e) {
e.printStackTrace();
} finally {
if(r != null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字节流读取
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileOutputStreamRead {
public static void main(String[] args) {
String path ="d:\\dd.txt";
FileInputStream i = null;
try {
i = new FileInputStream(path);
//方式一:单个字符读取
//需要注意的是,此处我用英文文本测试效果良好
//但中文就悲剧了,不过下面两个方法效果良好
/*int ch = 0;
while((ch=i.read()) != -1){
System.out.print((char)ch);
}*/
//方式二:数组循环读取
byte[] buf = new byte[1024];
int len = 0;
while((len = i.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
//方式三:标准大小的数组读取
/*
//定一个一个刚好大小的数组
//available()方法返回文件的字节数
//但是,如果文件过大,内存溢出,那就悲剧了
//所以,亲们要慎用!!!上面那个方法就不错
byte[] buf = new byte[i.available()];
i.read(buf);
//因为数组大小刚好,所以转换为字符串时无需在构造函数中设置起始点
System.out.println(new String(buf));
*/
} catch (IOException e) {
e.printStackTrace();
} finally {
if(i != null) {
try {
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.test;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class testCharIO {
public static void main(String[] args) {
readTest();
}
//写入方法
public static void writeTest(){
//创建要操作的文件路径和名称
//其中,File.separator表示系统相关的分隔符,Linux下为:/ Windows下为:\\
/*String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";*/
String path = "e:\\testt\\demo.txt"; //testt文件夹必须存在
//由于IO操作会抛出异常,因此在try语句块的外部定义FileWriter的引用
FileWriter w = null;
try {
//w = new FileWriter(path);
w = new FileWriter(path,true);//循环写入 设置true
//将字符串写入到流中,\r\n表示换行想有好的
for (int i = 0; i < 3; i++) {
w.write("chenfujian is a good boy\r\n");
}
w.flush();
System.out.println("完毕");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(w!=null){
try {
//关闭流资源,需要再次捕捉异常
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//读取方法
public static void readTest(){
String path = "e:\\testt\\demo.txt"; //testt文件夹必须存在
FileReader r = null;
try {
r = new FileReader(path);
/*int temp1 = r.read();
System.out.println((char)temp1);*/
//方式二:循环读取
//read()方法读到文件末尾会返回-1
while (true) {
int temp = r.read();
if (temp == -1) {
break;
}
System.out.print((char) temp);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class testByteIO {
public static void main(String[] args) {
readTest();
}
//写入
public static void writeTest(){
String path = "e:\\testt\\demo2.txt"; //testt文件夹必须存在
FileOutputStream o = null;
try {
o = new FileOutputStream(path);
String str = "Nerxious is a good boy\r\n";
byte[] buf = str.getBytes();
//也可以直接使用o.write("String".getBytes());
//因为字符串就是一个对象,能直接调用方法
o.write(buf);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(o != null) {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//读取
public static void readTest(){
String path = "e:\\testt\\demo2.txt"; //testt文件夹必须存在
FileInputStream i = null;
try {
i = new FileInputStream(path);
byte[] buf = new byte[1024];
int len = 0;
while((len = i.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(i != null) {
try {
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
参考博客:http://blog.csdn.net/yczz/article/details/38761237
http://www.cnblogs.com/nerxious/archive/2012/12/15/2818848.html
java io (一)的更多相关文章
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- Java IO之字符流和文件
前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
- java Io流向指定文件输入内容
package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...
- java Io文件输入输出流 复制文件
package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...
- java Io流更新文件内容
package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- java.io.NotSerializableException: test.io.file.Student
java.io.NotSerializableException: test.io.file.Student at java.io.ObjectOutputStream.writeObject0 ...
- java.io.IOException: mark/reset not supported
java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:348) at ...
- Java IO流学习总结
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
随机推荐
- Linux下运行windows程序
现在Winxp停止了支持,那我们的windows程序是否可以再linux上执行呢,如下是一些参考的信息 在您的 Linux/Mac 操作系统上运行 Windows 软件 http://www.wine ...
- Windows下修改Android手机的hosts
Windows下修改Android手机的hosts 1.首先,手机是Root过的. 2.连接手机和电脑, adb shell 进入命令行. 3.获取root用户权限: su -root 4.不知道为何 ...
- Android Handler机制(三)----Looper源码解析
一.Looper Looper对象,顾名思义,直译过来就是循环的意思,从MessageQueue中不断取出message. Class used to run a message loop for a ...
- 操作系统开发系列—12.a.从Loader到内核 ●
Loader要做两项工作,我们先来做第一项,把内核加载到内存: 1.加载内核到内存. 2.跳入保护模式. 首先编译无内核时: nasm boot.asm -o boot.bin nasm loader ...
- JAVA Web 实现会话跟踪的技术笔记
1.HTTP协议无状态:客户端的请求与服务器的响应所发生的一系列行为简单的说是客户发送了请求,服务器就给客户端响应,它们彼此之间都没有记录下来.如: 顾客与自动售货机 普通顾客(非会员)与商场 2.c ...
- 【代码笔记】iOS-背景色随机显示
一,效果图. 二,工程图. 三,代码. RootViewController.h RootViewController.m - (void)viewDidLoad { [super viewDidLo ...
- Dex Loader] Unable to execute dex: Multiple dex files define
在打包的过程中可能会出现这样的问题,原因是有重复的.jar被引用,可以查看你的build path或Java build path,尤其是Android Dependencies等相关android包 ...
- Nexus Repository Manager 3.0 发布
著名仓库管理工具Nexus,在2016年4月6日发布3.0版本(包括OSS版),相较2.*版本有很大的改变: 1. 从底层重构,从而提高性能,增强扩展能力,并改善用户体验 2. 升级界面,增加更多的浏 ...
- get set 中 快捷键生成的get方法中 renturn 没有 this.对象 中的this 解决方法
选EDIT 进行修改
- Asp.net MVC使用Model Binding解除Session, Cookie等依赖
上篇文章"Asp.net MVC使用Filter解除Session, Cookie等依赖"介绍了如何使用Filter来解除对于Session, Cookie的依赖.其实这个也可以通 ...