03.Test:从硬盘中写入文件到内存中(字符)
public void testFileReader(){
FileReader fr = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt");//相对路径
//2.提供具体的流
fr = new FileReader(file);
//3.数据的读入
//read():返回读入的一个字符。如果达到文件末尾,返回-1
//方式一:
int data = fr.read();
while(data != -1){
System.out.print((char)data);
data = fr.read();
}
//方式二:语法上针对于方式一的修改
int data;
while((data = fr.read()) != -1){
System.out.print((char)data);//因为要循环取出字符所以要放入while循环中
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流的关闭操作
// try {
// if(fr != null)
// fr.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
//或
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
说明:
- read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
- 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
- 读入的文件一定要存在,否则就会报FileNotFoundException。
@Test 对read()
操作升级:使用read的重载方法
public void testFileReader1() {
FileReader fr = null;
try {
//1.File类的实例化
File file = new File("hello.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:
//错误的写法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正确的写法
for(int i = 0;i < len;i++){
System.out.print(cbuf[i]);
}
//方式二:
//错误的写法,对应着方式一的错误的写法
// String str = new String(cbuf);
// System.out.print(str);
//正确的写法
String str = new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr != null){
//4.资源的关闭
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
04.Test:从内存中写出文件到硬盘中(字符)
public void testFileWriter() {
FileWriter fw = null;
try {
//1.提供File类的对象,指明写出到的文件
File file = new File("hello1.txt");
//2.提供FileWriter的对象,用于数据的写出
fw = new FileWriter(file,false);
//3.写出的操作
fw.write("I have a dream!\n");
fw.write("you need to have a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流资源的关闭
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
05.Test:完成一个文件的复制(字符)
//注意:此处的异常都以抛出的方式处理,在开发中要以try-catch-finally的形式处理
public void testFileReaderWriter() throws IOException {
//创建读取字符流 与 写出字符流
FileReader fr1 = new FileReader(new File("b.txt"));
FileWriter fw1 = new FileWriter(new File("c.txt"));
//读取和写入操作
char[] cbuf = new char[10];
int len;//记录读到cbuf数组中字符的个数
while ((len = fr1.read(cbuf)) != -1){//读取操作
//写出操作 每次写出len个字符
fw1.write(cbuf,0,len);
}
fr1.close();
fw1.close();
}
06.Test:从硬盘中写入文件到内存中(字节)
//注意:此处的异常都以抛出的方式处理,在开发中要以try-catch-finally的形式处理
public void TestStreamInput() throws IOException {
//造流
FileInputStream fis = new FileInputStream(new File("b.txt"));
//读入方式一:直接读取一行数据
byte[] cbuf = new byte[10];
int len;
while ((len = fis.read(cbuf)) != -1){
String s = new String(cbuf,0,len);
System.out.print(s);
}
//读取方式二:使用转换流将字节转换成字符,在读取一行数据
InputStreamReader isr = new InputStreamReader(fis);//字节转字符
BufferedReader br = new BufferedReader(isr);//缓冲流
String str = null;
while((str = br.readLine()) != null){
System.out.println(str);
}
//闭流
fis.close();
}
07.Test:实现对图片的复制操作
//注意:此处的异常都以抛出的方式处理,在开发中要以try-catch-finally的形式处理
public void TestStreamInputOutput() throws IOException {
//造流
FileInputStream fis = new FileInputStream(new File("img.jpg"));
FileOutputStream fos = new FileOutputStream(new File("img1.jpg"));
//读取
byte[] cbuf = new byte[10];
int len;
while ((len = fis.read(cbuf)) != -1){
fos.write(cbuf,0,len);//写入
}
//闭流
fis.close();
fos.close();
}
08Test:指定路径下文件的复制(封装成一个方法)
//注意:此处的异常都以抛出的方式处理,在开发中要以try-catch-finally的形式处理
public void TestStreamInputOutput(String srcFile,String destFile) throws IOException {
//造流
FileInputStream fis = new FileInputStream(new File(srcFile));
FileOutputStream fos = new FileOutputStream(new File(destFile));
//复制的过程
byte[] cbuf = new byte[10];
int len;
while ((len = fis.read(cbuf)) != -1){//读入内存
fos.write(cbuf,0,len);//写出内存
}
//闭流
fis.close();
fos.close();
}