java的IO流分为字符流和字节流,字符流又分为字符输入流和字符输出流,字节流又分为字节输入流和字节输出流。
(这个图不完整,以后随着学习的进度慢慢补充)
今天我们主要讲FileInputStream和FileOutputStream:
它们叫做文件输入流和文件输出流。
1.FileInputStream能够将文件中的数据读取到内存中。
它有三个构造函数:
①FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
②FileInputStream(FileDescriptor fdObj)
通过使用文件描述符 fdObj 创建一个 FileInputStream,该文件描述符表示到文件系统中某个实际文件的现有连接。
③FileInputStream(String name)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
通常我们使用第三种构造
我们也来列一下它的几个常用的方法:
①int read() :从此输入流中读取一个数据字节。
②int read(byte[] b) :从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
③int read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
④int available() :返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。
⑤long skip(long n):从输入流中跳过并丢弃 n 个字节的数据。
⑥void close() :关闭此文件输入流并释放与此流有关的所有系统资源。
我们看一段简单的代码范例:
package IO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
/*
* test文件是一个文本文件,放在F盘更目录下,里面包含abcdefg。
*/
public class testFileInputStream01 {
public static void main(String[] args) {
try {
FileInputStream fis =null;
String filePath="F:/test";//F:\\test也可以
fis = new FileInputStream(filePath);
int t1 = fis.read();
System.out.println(t1);//输出a的Ascall码值97
fis.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上是一个简单的读取,利用到了read()方法,它每次读一个字节,返回这个字节的ascall码值。
我们再来看看FileInputStream的另一个读取方法
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class testFileInputStream02 {
public static void main(String[] args) {
String path = "F:/test";
try {
FileInputStream fis = new FileInputStream(path);
byte[] bytes = new byte[3];
//t1位读的的字节数
int t1 = fis.read(bytes);
System.out.println(t1);
//将字节转化为字符串,并输出。
System.out.println(new String(bytes));
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样就可以将字节读到字节数组中,并把他们转化为字符串输出。
我们继续改进这段代码:
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputAndOutput {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("F:/1.png");
byte[] b = new byte[1024];
while(true) {
fis.read(b);
}
System.out.println(new String(b));
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样就可以将一个文件不断的读到字节数组中。
接下来说说FileOutputStream:
FileOutputStream和FileInputStream基本雷同,只是一个是读一个是写,特别需要注意一下他的构造函数:
我们经常使用最后两个构造函数,但是倒数第二个构造函数一定要慎用。因为它覆盖原来的文件字节。我们一般如果要为一个文本文件添加字节,一般都使用第二个,可以选择是否是追加方式的写操作(如果第二个参数为false,则效果和倒数第二个一样)
用FileInputStream和FileOutputStream实现复制粘贴的功能:
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputAndOutput {
public static void main(String[] args) {
try {
//创建输入流对象,将在F盘根目录下的一张图片读出。
FileInputStream fis = new FileInputStream("F:/1.png");
//创建输出流对象,将输出流入流中的图片字节写到F盘下,命名为3.png
FileOutputStream fos = new FileOutputStream("F:/3.png");
//创建用来保存字节的字节数组
byte[] b = new byte[1024];
//当文件中的字节读完的时候fis.read(b)=-1
//这样就形成了一边读一边写的顺序
while(fis.read(b)!=-1) {
fos.write(b);
}
fis.close();//关闭流
fos.close();//关闭流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行后F盘下生成3.png的图片,图片内容和1.png相同。
以上就是文件流复制粘贴的具体实现。