节点流:
可以从或向一个特定的地方(节点)读写数据。如FileOutputStream、FileReader等
处理流:
是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写。如BufferedReader。处理流的构造方法总是要带一个其他的流对象做参数。一个流对象经过其他流的多次包装,称为流的链接。
FileOutputStream
FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter。
常用构造方法
FileOutputStream(File file) //创建一个向file对象表示的文件中写入数据的文件输出流
FileOutputStream(File file,boolean append) //同上,如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
FileOutputStream(String name)
FileOutputStream(String name,boolean append)
常用方法
void close() //关闭流
write(byte[] b) //将b.length个字节从指定byte数组写入此文件输出流中
write(byte[] b,int off,int len) //将指定byte数组中从偏移量off开始的len各字节写入此文件输出流
write(int b) //将指定字节写入此文件输出流
示例
将”Hello World”写入到指定文件中
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write("Hello World".getBytes());
fos.close();
}
}
将”java IO流”追加到”Hello World”后
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt");
FileOutputStream fos = new FileOutputStream(file,true);
fos.write(" java IO流".getBytes());
fos.close();
}
}
FileInputStream
FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
常用构造方法
FileInputStream(File file) //通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定
FileInputStream(String name) //通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
常用方法
void close() //关闭流
read() //从此输入流中读取一个数据字节
read(byte[] b) //从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
read(byte[] b,int off,int len) // 从此输入流中将最多 len 个字节的数据读入一个 byte数组从off位置开始
示例
以字节形式读取
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt");
FileInputStream fis = new FileInputStream(file);
int len ;
while((len = fis.read())!= -1){
System.out.print((char) len);
}
fis.close();
}
}
以字节数组形式读取
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt");
FileInputStream fis = new FileInputStream(file);
int len ;
byte[] b = new byte[10];
while((len = fis.read(b))!= -1){
String s = new String(b,0,b.length);
System.out.print(s);
}
fis.close();
}
}
实现文件的复制
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("D:/io/acopy.txt");
int len ;
byte[] b = new byte[1024];
while((len = fis.read(b))!= -1){
fos.write(b);
}
fis.close();
fos.close();
}
}
FileWriter
public class FileWriterextends OutputStreamWriter
FileWriter 用于写入字符流。要写入原始字节流,请考虑使用 FileOutputStream。
常用构造方法
FileWriter(File file) //根据给定的File对象构造一个FileWriter对象
FileWriter(File file,boolean append) //根据给定的File对象构造一个FileWriter对象
FileWriter(String fileName,boolean append) //据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。
常用方法
继承自Writer的方法
void writer()
Writer append(char c)
void flush() //刷新该流的缓冲
close() //关闭流
写入文件
public class Demo {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt ");
Writer writer = new FileWriter(file);
writer.write("哈哈哈");
writer.close();
}
}
FileReader
基本操作和FileInputStream差不多,只不过FileReader是面向字符的。

635

被折叠的 条评论
为什么被折叠?



