输出(OutputStream Writer)程序->文档
输入(InputStream Reader) 文档 ->程序
字节流:OutputStream InputStream->操作byte类型数据
public abstract class OutputStream extends Object implements Closeable,Flushable
Closeable表示可关闭,Flushable表示可刷新
字符流:Writer Reader
Java中的IO操作步骤
1.使用File类打开一个文件
2.通过字节流或字符流的子类
3.进行读写操作
4.关闭输入/输出操作
-------->OutputStream
OutputStream是一个抽象类,必须通过子类实例化对象。
public void close() throws IOException
public void flush() throws IOException //刷新缓冲区
public void write(byte[] b) throws IOException //将byte数组写入数据流
public void write(byte[] b,int off,int len) throws IOException //将指定范围的byte数组写入数据流
public abstract void write(int b) throws IOException // 将一个字节写入数据流
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class TestJava{
}
class Demo{
public static void main(String[] args) throws Exception{
File fileFO = new File("C:"+File.separator+"JavaStudy"+
File.separator+"JavaIO"+File.separator+"test02.txt");
OutputStream oSwitch = new FileOutputStream(fileFO);
String strTemp01 = "Hello World!";
byte[] bReady = strTemp01.getBytes();
for(int i=0;i<bReady.length;i++){
oSwitch.write(bReady[i]); //put the byte array to the OutputStream
}
String strTemp02 = "\r\nHello World! Too!";
bReady = strTemp02.getBytes();
oSwitch.write(bReady);
oSwitch.close();
OutputStream oSwitchAppend = new FileOutputStream(fileFO,true);
String strTemp03 = "Append Hello World!";
bReady = strTemp03.getBytes();
oSwitchAppend.write(bReady);
oSwitchAppend.close();
}
}//Demo
output:
在text02.txt文件中写入了
Hello World!
Hello World! Too! Append Hello World!
本例中OutputStream被子类FileOutputStream实例化。所以oSwitch执行write方法时,执行FileOu拓扑图Stream的write方法(实现了OutputStream的abstract write()方法)。
字符串转换为字节数组getBytes()之后,可以用write(byte[])或者{write(int)+循环} ,来进行对文件的输出。
效果相同。不过重新执行后,新内容会覆盖旧内容。
想增加新内容?public FileOutputStream(File file,boolean append) throws FileNotFount
如果append的布尔值为true,则表示添加的内容不覆盖文件文本,而是添加到文件文本的末尾。
另外换行用\r\n
-------->InputStrem
public int available() throws IOException
public void close() throws IOException
public abstract int read() throws IOException //以数字方式读取,对应abstract void write(int n)
public int read(byte[] b) throws IOException // 将内容读取到byte[]中,同时返回读入的个数
FileInputStream构造:
public FileInputStream(File file) throws FileNotFoundException
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
public class TestJava{
}
class Demo{
public static void main(String[] args) throws Exception{
File fileFI = new File("C:"+File.separator+"JavaStudy"+File.separator+
"JavaIO"+File.separator+"test02.txt");
InputStream iSwitch = new FileInputStream(fileFI);
byte[] bReady = new byte[1024];
int lengthBReady = iSwitch.read(bReady);
//In this case,there must be a lot of space in array bReady
//System.out.println("The content readed is "+new String(bReady));
System.out.println("The content readed is "+
new String(bReady,0,lengthBReady)+",the length is "+lengthBReady);
/*lengthBReady = iSwitch.read(bReady);
for(int i=0;i<lengthBReady;i++){
System.out.print(bReady[i]+"/");
}
System.out.println();
*/
iSwitch.close();
}
}//Demo
output:The content readed is Hello World!
Hello World! Too!Append Hello World!,the length is 50
虽然输出没问题,不过byte[]数组的空间浪费不可小视。而且连续进行int x = obj.read(byte[] array)之后,byte数组的剩余位置也会发生变化。虽然最后指定了byte[]依靠new String(byte[],start,length)进行了截取,依然开辟了不少无用空间。
能否根据文件的数据量开辟空间?
File类中存在length可以取得文件大小。
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
public class TestJava{
}
class Demo{
public static void main(String[] args) throws Exception{
File fileFI = new File("C:"+File.separator+"JavaStudy"+File.separator+
"JavaIO"+File.separator+"test02.txt");
InputStream iSwitch = new FileInputStream(fileFI);
byte[] bReady = new byte[(int)fileFI.length()];
iSwitch.read(bReady);
iSwitch.close();
System.out.println("The content is "+new String(bReady));
}
}//Demo
output:与上例一样,同时避免了大量的空间浪费。但以上是能够确定数组大小的情况下开展的。如果不知道输入范围多大,
则只能通过判断是否读到文件末尾的方式来读取。本例使用了public int read(byte[])
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
public class TestJava{
}
class Demo{
public static void main(String[] args) throws Exception{
File fileFI = new File("C:"+File.separator+"JavaStudy"+File.separator+
"JavaIO"+File.separator+"test02.txt");
InputStream iSwitch = new FileInputStream(fileFI);
int lengthText = 0;
byte[] bReady = new byte[1024];
int recTemp = 0;
while((recTemp = iSwitch.read())!=-1){
bReady[lengthText] = (byte)recTemp;
lengthText++;
}
iSwitch.close();
System.out.println("The content is "+new String(bReady,0,lengthText));
}
}//Demo
输出一样,只是在不知道文件长度下实现,本例使用了public int read(),一个数字一个数字的读。