FileReader
FileReader读取数据
注意点:一个流打开,在我们使用完之后一定要关闭流。
在新建流、读取流、关闭流都需要抛出异常,这里抛出异常的方法必须是try-catch-finally
- 创建一个File类的对象,将需要读取的文件路径作为参数传入构造器
- 创建一个FileReader类的对象,将上述File类的对象作为参数传入构造器
- 调用FileReader.read(),返回结果是整型,将他转换为字符,如果都不到字符则返回-1
- 调用FileReader.cloce()关闭流
public static void main(String[] args){
FileReader fileReader = null;
try {
//1.新建一个File类的对象
File file = new File("IOStream\\hello.txt");
//2.将需要读取的File类对象作为参数传到FileReader构造器中
fileReader = new FileReader(file);
//3.调用read(),得到一个字符的整形格式,如果没有字符则返回-1
int read = fileReader.read();
while (read != -1){
System.out.print((char) read);
read = fileReader.read();
}
//优化写法
// int data;
// while ((data = fileReader.read()) != -1){
// System.out.print((char) data);
// }
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭字符流
try {
if(fileReader != null)
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileReader读取数据(char[])
@Test
public void test01() {
FileReader fr = null;
try {
//1.创建File类对象
File file = new File("hello.txt");
//2.创建流的对象
fr = new FileReader(file);
/*
3.读入数据操作
fr.read(char[] cbuf)
1.定义一个char数组来读取文本数据,char数组的长度则是一次最多能读取多少个字符
2.定义一个len来读取一次读取了多少个字符
3.当读不到字符时,则返回-1
*/
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
//注意点:每次遍历的长度应该是这次读取到了字符的长度,而不是cbuf数组的长度
//第一种写法:for循环
// for (int i = 0; i < len; i++) {
// System.out.print(cbuf[i]);
// }
//第二种:String构造器,要注意的是每次读取char[]应该是从0-len
String str = new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileWriter
- 创建File类的对象,指定需要写入数据文件的路径
- 创建FileWriter的对象,将File对象作为参数传入
- 这里调用new FileWriter(file)或new FileWriter(file,false):则表示覆盖原来的文件
- 这里调用new FileWriter(file,true):则表示在原来的文件上追加写入
- 调用write方法,写入数据
- 关闭流
@Test
public void test02() {
FileWriter fw = null;
try {
//1.创建File类的对象,指定需要写入数据文件的路径
File file = new File("helloworld.txt");
//2.创建FileWriter的对象,将File对象作为参数传入
//这里调用new FileWriter(file)或new FileWriter(file,false):则表示覆盖原来的文件
//这里调用new FileWriter(file,true):则表示在原来的文件上追加写入
fw = new FileWriter(file,true);
//3.调用write方法,写入数据
fw.write("I have a dream!\t");
fw.write("I have a ball!\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流
try {
if(fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
利用FileReader和FileWriter实现复制文件操作
@Test
public void test03() {
FileReader fr = null;
FileWriter fw = null;
try {
File srcFile = new File("helloworld.txt");
File destFile = new File("hello1.txt");
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
InputStream以及OutputStream
使用InputStream、OutputStream实现图片的复制
@Test
public void test01(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//1.造文件对象
File srcFile = new File("img.png");
File destFile = new File("img1.png");
//2。造流
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
//3.复制图片
byte[] buffer = new byte[5];
int len;//用来记录一次读取了多少条数据
while ((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流
try {
if(fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}