java 关于io流的知识点_java中IO流相关知识点 悠悠 2022-11-08 11:26 166阅读 0赞 (一) 下边使用outputStream字节输出流进行写操作 package zdbIO; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class OutputStreamDemo1 \{ /\*\* \* @throws IOException \* @throws IOException \* \*/ public static void main(String\[\] args) throws IOException\{ /\*\* \* 使用IO流的具体步骤: \* 1.使用file找到要操作的文件 \* 2.(使用字节流或字符流的子类来实例化inputStream、outStream、reader、writer) \* 3.进行读写操作 \* 4.关闭流,除BufferedReader例外 \*/ File file = new File("f:"+File.separator+"zdb1.txt");//使用file找到要操作的文件 OutputStream out = null; out = new FileOutputStream(file,true);//使用OutputStream的子类进行实例化 String str = "XXX的十年人生规划,一定要有个计划这样你的人生才会有明确的方向 不至于迷失。";//要输出的信息 byte b\[\] = str.getBytes();//将str变为byte数组 out.write(b);//写入数据 out.close();//关闭流 \} \} (二)下边使用inputStream字节流进行读操作的第一种方法: package zdbIO; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; /\*\* \* 通过inputStream字节流来进行读操作 \* \*/ public class InputStreamDemo \{ public static void main(String\[\] args) throws Exception \{ //注意此文件必须存在否则会发生java.io.FileNotFoundException异常 //File.separator表示分隔符,其中在Windows中表示\\,在unix表示/,这样可以跨平台 File file = new File("f:"+File.separator+"zdb1.txt"); InputStream input = null; input = new FileInputStream(file); byte b\[\] = new byte\[1024\];//开辟一块内存用来存储读取的内容 int len = input.read(b);//将文件读到字符数组中 //其中new String(byte\[\]bytes,int offset,int length), //表示将创建一个字符串,从offset为开始,长度为length System.out.println(new String(b,0,len)); input.close(); \} \} /\*\* \* 这种方法中开辟空间的大小受到限制,因此可以根据文件的大小来开辟空间的大小, \* 即可以使用byte b\[\] = new byte\[(int)file.length()\],来创建开辟的空间, \* 然后可以通过read()方法来一个个读取。 \* \*/ (二)下边使用inputStream字节流进行读操作的第二种方法 package zdbIO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo1 \{ public static void main(String\[\] args) throws Exception \{ File file = new File("f:"+File.separator+"zdb1.txt"); InputStream input = null; input = new FileInputStream(file); byte b\[\] = new byte\[(int)file.length()\]; for(int i=0;i b\[i\] = (byte)input.read(); \} System.out.println(new String(b)); input.close(); \} \} (三)下边使用Reader字节流进行读操作的第一种方法: package zdbIO; import java.io.File; import java.io.FileReader; import java.io.Reader; public class ReaderDemo \{ public static void main(String\[\] args) throws Exception \{ File file = new File("f:"+File.separator+"zdb1.txt"); Reader in = null; in = new FileReader(file); char c\[\] = new char\[1024\]; int len = in.read(c); System.out.println(new String(c,0,len)); in.close(); \} \} (三)下边使用Reader字节流进行读操作的第二种方法: package zdbIO; import java.io.File; import java.io.FileReader; import java.io.Reader; public class ReaderDemo \{ public static void main(String\[\] args) throws Exception \{ File file = new File("f:"+File.separator+"zdb1.txt"); Reader in = null; in = new FileReader(file); char c\[\] = new char\[1024\]; int len = in.read(c); System.out.println(new String(c,0,len)); in.close(); \} \} (四)下边使用Writer字节流进行写操作的: package zdbIO; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class WriterDemo \{ /\*\* \* 可以通过字符流writer来进行写操作 \* @throws IOException \* \*/ public static void main(String\[\] args) throws IOException \{ File file = new File("f:"+File.separator+"zdb1.txt"); Writer writer = null; writer = new FileWriter(file, true); String str = "你今天好好学习了吗?你今天进步了吗学会每天一番思"; writer.write(str); writer.close(); \} \} (五) 字符流和字节流的区别: 1.字符流中写操作Writer中,操作完成后需要关闭流或者刷新,否则写入不成功。 2.字节流直接与文件进行交互的,不需要使用缓存;而字符流是通过缓存与文件进行交互的; 3.在传输和硬盘上保存的内容都是字节形式,所以字节形式操作较多;而操作文件的时候使用字符流较多;
还没有评论,来说两句吧...