知识总结"/>
Java知识总结
Java知识总结-文件流使用详情
文件流
- 字节流:FileIntputStream、FileOutputStream
- 字符流:FileReader、FileWriter
- 对于文本文件(.txt , .java, .c),使用字符流处理
- 对于非文本文件(.jpg , .mp3, .mp4, .avi, .doc, .ppt),使用字节流处理
文件字节流
输入txt文件
package FileInputOutputTest;import java.io.*;
import java.io.IOException;public class FileInputStreamTest {public static void main(String[] args) {FileInputStream fis =null;try {//1.造文件File file = new File("hello.txt");//2.造流fis = new FileInputStream(file);//3.读数据byte[] buffer = new byte[5];int len;//记录每次读取的字节的个数while ((len = fis.read(buffer)) != -1) {String str = new String(buffer, 0, len);System.out.print(str);}} catch (IOException e) {e.printStackTrace();}finally {//4.关闭资源if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
}
复制代码
输出png文件
package FileInputOutputTest;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class FileInputStreamTest2 {public static void main(String[] args) {FileInputStream fis = null;FileOutputStream fos =null;try {File srcfile = new File("怪物万岁.png");File destfile = new File("怪物万岁1.png");fis = new FileInputStream(srcfile);fos = new FileOutputStream(destfile);byte[] buffer = new byte[5];int len;while ((len = fis.read(buffer)) != -1) {fos.write(buffer, 0, len);}System.out.println("复制成功!");} catch (IOException e) {e.printStackTrace();}finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
}
复制代码
文件字符流
- read()的理解:返回读入的一个字符。如果达到问价你的末尾,返回-1
- 异常的处理:为了保证资源一定可以执行关闭操作。需要使用try-catch-finally处理
- 读入的文件一定要存在,否则就会报FileNotFoundException。
输入流
1.方式一
public class FileReaderTest1 {public static void main(String[] args) {FileReader fr =null;try {/*将包下的hello.txt文件内容读入程序,并输出到控制台*///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);}} catch (IOException e) {e.printStackTrace();}finally {//4.流的关闭操作try {if (fr != null) {fr.close();}} catch (IOException e) {e.printStackTrace();}}}
}
复制代码
2.方式二
package FileReaderWriterTest;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;public class FileReaderTest2 {public static void main(String[] args) {//对read()操作的升级:使用read的重载方法FileReader fr=null;try {//1.File类的实例化File file = new File("hello.txt");//2.FileReader流的实例化fr = new FileReader(file);//3.读入的操作//read(char[] cbuf):返回每次读入的cbuf数组中字符的个数。如果达到文件末尾,返回-1char[] cbuf = new char[5];int len;
// while ((len = fr.read(cbuf)) != -1) {
// for (int i = 0; i <len; i++) {
// System.out.print(cbuf[i]);
// }
// }
// System.out.println();//方式二while ((len = fr.read(cbuf)) != -1) {String str = new String(cbuf, 0, len);System.out.print(str);}} catch (IOException e) {e.printStackTrace();}finally {//4.资源的关闭if (fr != null) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}
}复制代码
输出流
-
输出操作,对应的File可以不存在。并不会报异常
-
如果不存在,在输出的过程中,会自动创建文件。
如果存在,
如果流使用的构造器是FileWriter(file,false)/FileWriter(file):对原有的文件覆盖
如果流使用的构造器是FileWriter(file,true):对原有文件的基础上追加内容
package FileReaderWriterTest;import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class FileReaderWriterTest {public static void main(String[] args) {FileWriter fw = null;FileReader fr = null;try {//1.创建File类的对象,指明读入和写出的文件File srcFile = new File("hello.txt");File destFile = new File("hello1.txt");//不能用字符流来处理图片等字节数据
// File srcFile = new File("怪物万岁.png");
// File destFile = new File("怪物万岁1.png");//2.创建输入流和输出流的对象fr = new FileReader(srcFile);fw = new FileWriter(destFile);//3.数据的读入和写出char[] cbuf = new char[5];int len;//记录每次读入到cbuf数组中的字符个数while ((len = fr.read(cbuf)) != -1) {fw.write(cbuf, 0, len);//每次写出len个字符}} catch (IOException e) {e.printStackTrace();}finally {//4.关闭资源try {if (fw != null) {fw.close();}} catch (IOException e) {e.printStackTrace();}finally {if (fr != null) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}}
}
复制代码
从内存写出数据到硬盘文件内
package FileReaderWriterTest;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;public class FileWriterTest {public static void main(String[] args) throws IOException {/*从内存写出数据到硬盘文件内*///1.提供File文件File file = new File("hello.txt");//2.提供FileWriter的对象,用于数据的写出FileWriter fw = new FileWriter(file);//3.写出的操作fw.write("I have a dream!\n");fw.write("you need have a dream ,too.");//4.流资源的关闭fw.close();}
}
最后:
最近我整理了整套**《JAVA核心知识点总结》**,说实话 ,作为一名Java程序员,不论你需不需要面试都应该好好看下这份资料。拿到手总是不亏的~我的不少粉丝也因此拿到腾讯字节快手等公司的Offer
进[Java架构资源交流群] ,找管理员获取哦-!
更多推荐
Java知识总结
发布评论