JAVA I/O使用方法(转)
下面四张图表明了类之间的继承关系,其中红色、加粗的类名是常用的类。
常用转换
FileReader——>BufferedReader
BufferedReader in= new BufferedReader(new FileReader("Text.java"));
InputStream——>InputStreamReader——>BufferedReader
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String——>byte[]——>ByteArrayInputStream——>DataInputStream
DataInputStream in= new DataInputStream(new ByteArrayInputStream(str.getBytes()));
FileInputStream——>BufferedInputStream——>DataInputStream
DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
FileWriter——>BufferedWriter——>PrintWriter
PrintWriter pw=new PrintWriter(new BufferedWriter("text.out"));
System.out(PrintStream)——>PrintWriter
PrintWriter pw=new PrintWriter(System.out,true);
FileOutputStream——>BufferedOutputStream——>PrintStream
PrintStream ps= new PrintStream(new BufferedOutputStream(new FileOutputStream("text.out")));
FileOutputStream——>BufferedOutputStream——>DataOutputStream
DataOutputStream dos= new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
程序举例
import java.io.*; public class IOStreamDemo { public static void main(String[] args) throws IOException { /* 1.要想打开文件读取字符,你得先用String或File对象创建一个FileReader。
为了提高速度,你应该对这个文件作缓冲,因此你得把FileReader的reference 交给BufferedReader。
BufferedReader提供了readLine()方法,当你读到文件的末尾时readLine()会返回一个null,于是就退出while()循环了。
String sb是用来累加文件内容的,(再加一个换行符“\n”因为readLine()会把它们都剥掉).
最后用close()来清空缓冲区。*/
String s = "test";
StringBuilder source = new StringBuilder();
source.append(s);
source.append(System.getProperty("line.separator")); /* 2. 用System.in生成一个能读取控制台输入的流。System.in是一个InputStream,
而BufferedReader需要一个Reader作参数,所以要先通过InputStreamReader来转转手。
Java遵循标准I/O的模型,提供了Syetem.in,System.out,以及System.err。
System.out是一个已经预先处理过的,被包装成PrintStream的对象。
System.err也是一个PrintStream;
System.in是一个未经处理的InputStream。
也就是说,虽然你可以直接往System.out和System.err上写,但是要想读System.in的话,就必须先做处理了。*/
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line:");
System.out.println(reader.readLine()); /* 3. 用String s2作参数创建一个StringReader。然后用StringReader的read()方法把字符读出来,再送到控制台上去。
read()会把读出来的byte当作int,所以要想正常打印的话,你得先把它们转成char。*/
StringReader stringReader = new StringReader(source.toString());
int c;
while ((c = stringReader.read()) != -1)
System.out.print((char) c); /*
4. 要想读取"格式化"的数据,你就得用DataInputStream了,DataInputStream是一个面向byte的I/O类,不是面向char的。
因此你只能从头到底一直用InputStream了。
当然你可以把所有的东西都当成byte。
然后用InputStream读出来。
但这里是String。要想把String当成byte数组。可以用String的getBytes()方法。而ByteArrayInputStream是可以处理byte数组的。
*/ System.out.println("===============ByteArrayInputStream begin===============");
DataInputStream byteArrayInputStream = new DataInputStream(
new ByteArrayInputStream(source.toString().getBytes()));
byte[] bytes = new byte[100];
while ((byteArrayInputStream.read(bytes)) != -1) {
System.out.print(new String(bytes));
}
System.out.println(System.getProperty("line.separator"));
System.out.println("===============ByteArrayInputStream end==============="); /* 5.向文件中写数据。先创建一个FileWriter,BufferedWriter是免不掉的。
然后再让PrintWriter去排版。这样就能得出能够读得懂的,普通的文本文件了。
输入流用完之后,readLine()会返回null。
最后调用close()方法清空缓冲区。*/ BufferedReader bufferedReader = new BufferedReader(new StringReader(source.toString()));
PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
while ((s = bufferedReader.readLine()) != null)
printWriter.println(lineCount++ + ": " + s);
printWriter.close(); DataOutputStream dataOutputStream = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream("Data.txt"))); /*6. 存储和恢复数据
PrintWriter会对数据进行格式化,这样人就能读懂了。
但是如果数据输出之后,还要恢复出来供其它流用,那你就必须用DataOutputStream来写数据,再用DataInputStream来读数据了。
当然,它们可以是任何流,不过我们这里用的是一个经缓冲的文件。
DataOutputStream和DataInputStream是面向byte的,因此这些流必须都是InputStream和OutputStream。
如果数据是用DataOutputStream写的,那么不管在哪个平台上,DataInputStream都能准确地把它还原出来。
这一点真是太有用了,因为没人知道谁在为平台专属的数据操心。如果你在两个平台上都用Java,那这个问题就根本不存在了。
用DataOutputStream写String的时候,要想确保将来能用DataInputStream恢复出来,唯一的办法就是使用UTF-8编码。
UTF-8是Unicode的一种变形。Unicode用两个字节来表示一个字符。
但是,如果你处理的全部,或主要是ASCII字符(只有7位),那么无论从存储空间还是从带宽上看,就都显得太浪费了,
所以UTF-8 用一个字节表示ASCII字符,用两或三个字节表示非ASCII的字符。
此外,字符串的长度信息存在(字符串)的头两个字节里。writeUTF( )和readUTF( )用的是Java自己的UTF-8版本(JDK文档里有关于这个字符集的完整讲解,
就在这两个方法的文档里),
所以如果你要用一个Java程序读取writeUTF( )写的字符串的话,就必须进行一些特殊处理了。
有了writeUTF( )和readUTF( ),你就能放心地把String和其它数据混在一起交给DataOutputStream了,
因为你知道String是以Unicode的形式存储的,而且可以很方便地用DataOutputStream恢复出来。
writeDouble( )会往流里写double,而它"影子"readDouble( )则负责把它恢复出来(其它数据也有类似的读写方法)。
但是要想让读取方法能正常工作,你就必须知道流的各个位置上都放了些什么数据。因为你完全可以把double读成byte,char,或其它什么东西。
所以要么以固定的格式写文件,要么在文件里提供额外的解释信息,然后一边读数据一边找数据。先提一下,对于复杂数据的存储和恢复,对象的序列化可能会比较简单。*/
dataOutputStream.writeDouble(3.14159);
dataOutputStream.writeUTF("That was pi");
dataOutputStream.writeDouble(1.41413);
dataOutputStream.writeUTF("Square root of 2");
dataOutputStream.close();
DataInputStream dataInputStream = new DataInputStream(
new BufferedInputStream(new FileInputStream("Data.txt")));
System.out.println(dataInputStream.readDouble());
System.out.println(dataInputStream.readUTF());
System.out.println(dataInputStream.readDouble());
System.out.println(dataInputStream.readUTF()); }
}
http://blog.sina.com.cn/s/blog_4cc16fc50100bvyb.html
InputStream/OutputStream:
1)抽象类,2)面向字节形式的I/O操作(8 位字节流) 。
Reader/Writer:
1)抽象类,2)面向字符的 I/O操作(16 位的Unicode字符) 。
InputStreamReader:
可以将InputStream转换为 Reader。
OutputStreamWriter:
可以将OutputStream转换为Writer。
Java I/O的核心采用了Decorator(装饰)模式。
http://wentao365.iteye.com/blog/1183951
JAVA I/O使用方法(转)的更多相关文章
- 获取当前应用的系统路径工具类和java的System.getProperty()方法介绍
java的System.getProperty()方法可以获取的值,如下: 对于Java程序,无论是未打包的还是打包的JAR或WAR文件,有时候都需要获取它运行所在目录信息,如何做到这一点呢? /** ...
- Java实现时间动态显示方法汇总
这篇文章主要介绍了Java实现时间动态显示方法汇总,很实用的功能,需要的朋友可以参考下 本文所述实例可以实现Java在界面上动态的显示时间.具体实现方法汇总如下: 1.方法一 用TimerTask: ...
- Java Native Interfce三在JNI中使用Java类的普通方法与变量
本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 前面我们学习了如何在JNI中通过参数来使用J ...
- 浅谈Java中的hashcode方法
哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: 1 public native int hashCode(); 根据 ...
- 0019 Java学习笔记-面向对象-方法
方法属于谁 方法要么属于类,要么属于对象 static修饰的方法属于类 没有static修饰的方法属于对象 方法只能定义在类里面,不能独立定义 不能独立的执行方法,要么通过类调用,要么通过方法调用 一 ...
- [Java] - 格式字符串替换方法
Java 字符串格式替换方法有两种,一种是使用String.format(...),另一种是使用MessageFormat.format(...) 如下: import java.text.Messa ...
- atitit.java给属性赋值方法总结and BeanUtils 1.6.1 .copyProperty的bug
atitit.java给属性赋值方法总结and BeanUtils 1.6.1 .copyProperty的bug 1. core.setProperty(o, "materialId&qu ...
- JAVA常见错误处理方法 和 JVM内存结构
OutOfMemoryError在开发过程中是司空见惯的,遇到这个错误,新手程序员都知道从两个方面入手来解决:一是排查程序是否有BUG导致内存泄漏:二是调整JVM启动参数增大内存.OutOfMemor ...
- 千万不要误用 java 中的 HashCode 方法
刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...
- java中的native方法和修饰符(转)
Java中的native修饰符 今天偶然看代码,发现别人有这样写的方法,并且jar里面有几个dll文件,比较奇怪,于是把代码打开,发现如下写法. public native String GSMMod ...
随机推荐
- Deepin 15.3 下罗技蓝牙键盘连接
Deepin 15.3中,由于罗技蓝牙键盘需要配对码,所以无法在设置界面连接,本次尝试通过bluetoothctl软件,成功连接,步骤如下: 1.打开系统蓝牙 sudo service bluetoo ...
- 在mysql 中两种锁定问题
mysql 中15.2.10.5 中描述了两个问题,且分别给出了解决办法. 1.向子表中写入数据,但写入之前需确保父表中存在其相应信息. 可能出现,在已经读取父表中的数据,但另一请求将其删除. 办法: ...
- jQuery学习-事件之绑定事件(六)
在jQuery中,为了屏蔽event对象在各浏览器中的差异性,它使用了自定的Event对象,如下: 1 jQuery.Event = function( src, props ) { 2 ...
- 重启VirtualBox里面的系统提示VT-x features locked or unavailable in MSR错误
有次不小心设置了一下virtualbox里面的一些配置,然后启动系统时出现了如下提示 在网上找了一些资料尝试了一些方法偶然有一次成功 原来是自己把那个cpu个数设置成了2,改成1就好了,不知道为什么做 ...
- Android Studio优化之启用Shift+Ctrl+O导入所有的包
在使用Eclipse开发Android应用时,开发者往往会使用Shift+Ctrl+O快捷键来快速导入所有的包,和移除已经导入但还未使用的包.但这个快捷键在Android Studio没人是给有开启的 ...
- 《Android底层接口与驱动开发技术详解》digest
第一章:IDE:Eclipse ADT for java developer其它: Apache Ant Java SE Development Kit5或6 Linux和Mac上使用Apache A ...
- Keli Linux与网络安全(1)——在VMWare中安装Keli系统
Kali Linux是基于Debian的Linux发行版, 设计用于数字取证和渗透测试.由OffensiveSecurity Ltd维护和资助.最先由Offensive Security的Mati A ...
- poj3589---判断两个数有多接近
#include <stdio.h> #include <stdlib.h> int main() { ],s2[]; int a,b,i,j,n; scanf("% ...
- 全国计算机等级考试二级教程-C语言程序设计_第16章_文件
写入一段文本到文件 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> main() { ...
- Spring流程
Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...