数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流。数据写入程序可以是一段、一段地向数据流管道中写入数据,这些数据段会按先后顺序形成一个长的数据流。对数据读取程序来说,看不到数据流在写入时的分段情况,每次可以读取其中的任意长度的数据,但只能先读取前面的数据后,再读取后面的数据。不管写入时是将数据分多次写入,还是作为一个整体一次写入,读取时的效果都是完全一样的。
在电脑上的数据有三种存储方式,一种是外存,一种是内存,一种是缓存。比如电脑上的硬盘,磁盘,U盘等都是外存,在电脑上有内存条,缓存是在CPU里面的。外存的存储量最大,其次是内存,最后是缓存,但是外存的数据的读取最慢,其次是内存,缓存最快。
java中将输入输出对象称为流,就好像水管,将两个容器连接起来。将数据冲外存中读取到内存中的称为输入流(Input Stream),将数据从内存写入外存中的称为输出流(Output Stream)。显然,从上述描述可知,Input Stream不关心数据源来自何种设备;而Output Stream不关心数据的目的是何种设备。
流序列中的数据既可以是未经加工的原始二进制数据,也可以是经一定编码处理后符合某种格式规定的特定数据。因此Java中的流分为两种:
1) 字节流:数据流中最小的数据单元是字节
2) 字符流:数据流中最小的数据单元是字符, Java中的字符是Unicode编码,一个字符占用两个字节。
在Java中,对于文件的储存于读写有与之对应的类, FileInputStream类是以文件作为数据输入源的数据流,或者说是打开文件,从文件读数据到内存的类。该类可以使用read()方法一次读入一个字节,并以int类型返回,或者是使用read()方法时读入至一个byte数组,byte数组的元素有多少个,就读入多少个字节。在将整个文件读取完成或写入完毕的过程中,这么一个byte数组通常被当作缓冲区,因为这么一个byte数组通常扮演承接数据的中间角色。FileOutputStream类用来处理以文件作为数据输出目的数据流,或者说是从内存区读数据入文件。包装类DataOutputStream、DataInputStream为我们提供了多种对文件的写入和读取方法,如writeBoolean(),writeUTF(),writeChar,writeByte(),writeDouble()等和对应的read方法,
这些方法极大的方便了我们的写入和读取操作。
另外,我们需要注意的是:(1)文件中写数据时,若文件已经存在,则覆盖存在的文件;(2)的读/写操作结束时,应调用close方法关闭流。
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileIO {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args){
// //创建文件
// try {
// file.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// file = new File("F:/a/a/test"); //创建目录
file.mkdir(); //只能创建一个目录
// file.mkdirs(); //创建多个目录
//实例化对象
FileIO f = new FileIO();
try {
//读取文件
// f.readFile("F:\\test.txt");
//一次性读取
f.readFile2("F:\\test.txt");
//分段读取
// f.readFile3("F:\\test.txt");
//写入文件
// f.writeFile("F:\\test.txt");
//写入中文(带字符集)文件
// f.writeFile2("F:\\test.txt","呵呵呵呵呵呵呵呵!");
//复制文件
// f.copyFile("F:\\test.txt", "F:\\test_copy.txt");
//
// f.writeFile3();
// f.readFile4();
} catch (IOException e) {
e.printStackTrace();
}
}
public void printFile(String fileName){
File file = new File(fileName);
//列出子目录和文件
File[] files = file.listFiles();
}
public void readFile(String filename) throws IOException{
//创建文件输入流:读取
try {
FileInputStream fis = new FileInputStream(filename);
/*byte存在负数:-128 ---> +127
* */
//读取文件
//读取一个字节
int n = fis.read();
while(n != -1){
System.out.println((char)n);
//读取下一个字节
n = fis.read();
}
//用完要关闭输入流
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//一次全部读取
public void readFile2(String filename) throws IOException{
//创建文件输入流:读取
try {
FileInputStream fis = new FileInputStream(filename);
//输入流后面可读取的字节数
int length = fis.available();
//一次全部读取
byte[] bytes = new byte[length];
//readNum真实读取的数量
int readNum = fis.read(bytes);
//还原成字符串
// String content = new String(bytes);
String content = new String(bytes,"GB2312");//指定字符集
System.out.println(content);
//用完要关闭输入流
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//分段读取
public void readFile3(String filename) throws IOException{
//创建文件输入流:读取
try {
FileInputStream fis = new FileInputStream(filename);
//建立一个字节队列
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//首先按一个字节读取
int n = fis.read();
while(n != -1){
//把字节存入队列
bos.write(n);
if(n == '\n'){
//把队列中的数据读取出来
byte[] bytes = bos.toByteArray();
//还原字符串
String str = new String(bytes,"GB2312");
System.out.print(str);
//清空队列
bos.reset();
}
//读取下一个字节
n = fis.read();
}
//用完要关闭输入流
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//写入文件
public void writeFile(String filename) throws IOException{
try {
//每次写入均从最开始位置写入,覆盖
// FileOutputStream fos = new FileOutputStream(filename);
//每次写入从最后位置写入,不覆盖
FileOutputStream fos = new FileOutputStream(filename, true);
//写数据
//写入一个字节
fos.write(1111);
// fos.write("A");
//关闭
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//写入文件
public void writeFile2(String filename, String content) throws IOException{
try {
FileOutputStream fos = new FileOutputStream(filename, true);
//将字符串转化成byte
byte[] bytes = content.getBytes("GB2312");
//写入数据
fos.write(bytes);
//关闭
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//复制文件
public void copyFile(String fromFile, String toFile) throws IOException{
try {
//创建输出流
FileInputStream fis = new FileInputStream(fromFile);
//创建写入流
FileOutputStream fos = new FileOutputStream(toFile, true);
//读取一个字节
int n = fis.read();
while(n != -1){
// n = n+1;//简单加密
// n = n-1;//解密
//写入数据
fos.write(n);
//读取下一个字节
n = fis.read();
}
//用完要关闭输入流
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//对于int数据而言,按一个字节读取或写入会存在丢失
public void readFile4() throws IOException{
try {
//如果文件不存在会自动创建
FileInputStream fis = new FileInputStream("F:/save.dat");
//创建数据输入流,对文件输入流进行封装
DataInputStream dis = new DataInputStream(fis);
//向输入流写入一个int
int score = dis.readInt();
System.out.println(score);
//关闭
dis.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void writeFile3() throws IOException{
try {
//如果文件不存在会自动创建
FileOutputStream fos = new FileOutputStream("F:/save.dat");
//创建数据输出流,对文件输出流进行封装
DataOutputStream dos = new DataOutputStream(fos);
int score = 12345;
int score1 = 12222;
//向输出流写入一个int
dos.writeInt(score);
dos.writeInt(score1);
//关闭
dos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
<!--EndFragment--><!--EndFragment-->