大家好,欢迎来到IT知识分享网。
*21-IO流(字节流-操作文件基本演示)
字符流——只能处理文件
字节流——很多
不需要临时存储缓冲了,直接写到目的地。
//1,创建字节输出流对象。用于操作文件.
FileOutputStream fos = new FileOutputStream(“bytedemo.txt”);
//2,写数据。直接写入到了目的地中。
fos.write(“abcdefg”.getBytes());
// fos.flush(); 不需要
fos.close();//关闭资源动作要完成。 它关闭是有用的。
字节流没法读一个中文汉字。
package cn.itcast.p7.io.bytestream.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class ByteStreamDemo { / * @param args * @throws IOException */ public static void main(String[] args) throws IOException { demo_read(); } public static void demo_read() throws IOException { //1,创建一个读取流对象。和指定文件关联。 FileInputStream fis = new FileInputStream("bytedemo.txt"); // System.out.println(fis.available()); // byte[] buf = new byte[fis.available()]; // fis.read(buf); // System.out.println(new String(buf)); //建议使用这种读取数据的方式 // byte[] buf = new byte[1024]; // int len = 0; // // while((len=fis.read(buf))!=-1){ // System.out.println(new String(buf,0,len)); // } // int ch = 0; // while((ch=fis.read())!=-1){ // System.out.println((char)ch); // } //一次读取一个字节。 // int ch = fis.read(); // System.out.println(ch); fis.close(); } public static void demo_write() throws IOException { //1,创建字节输出流对象。用于操作文件. FileOutputStream fos = new FileOutputStream("bytedemo.txt"); //2,写数据。直接写入到了目的地中。 fos.write("abcdefg".getBytes()); // fos.flush(); fos.close();//关闭资源动作要完成。 } }
*22-IO流(字节流-练习-复制MP3)
字节流——就可以操作媒体文件了。
package cn.itcast.p7.io.bytestream.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyMp3Test { / * @param args * @throws IOException */ public static void main(String[] args) throws IOException { copy_4(); } // 千万不要用,效率没有! public static void copy_4() throws IOException { FileInputStream fis = new FileInputStream("c:\\0.mp3"); FileOutputStream fos = new FileOutputStream("c:\\4.mp3"); int ch = 0; while((ch =fis.read())!=-1){ fos.write(ch); } fos.close(); fis.close(); } //不建议。 public static void copy_3() throws IOException { FileInputStream fis = new FileInputStream("c:\\0.mp3"); FileOutputStream fos = new FileOutputStream("c:\\3.mp3"); byte[] buf = new byte[fis.available()]; fis.read(buf); fos.write(buf); fos.close(); fis.close(); } public static void copy_2() throws IOException { FileInputStream fis = new FileInputStream("c:\\0.mp3"); BufferedInputStream bufis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream("c:\\2.mp3"); BufferedOutputStream bufos = new BufferedOutputStream(fos); int ch = 0; while((ch=bufis.read())!=-1){ bufos.write(ch); } bufos.close(); bufis.close(); } public static void copy_1() throws IOException { FileInputStream fis = new FileInputStream("c:\\0.mp3"); FileOutputStream fos = new FileOutputStream("c:\\1.mp3"); byte[] buf = new byte[1024]; int len = 0; while((len=fis.read(buf))!=-1){ fos.write(buf,0,len); } fos.close(); fis.close(); } }
public static void copy_5() throws Exception { FileInputStream fileInputStream = new FileInputStream("I:\\3.zip"); BufferedInputStream bufferedInputStream = new BufferedInputStream( fileInputStream); FileOutputStream fileOutputStream = new FileOutputStream("I:\\5.zip"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( fileOutputStream); int len = 0; byte[] buf = new byte[1024]; while ((len = bufferedInputStream.read(buf)) != -1) { bufferedOutputStream.write(buf); } bufferedInputStream.close(); bufferedOutputStream.close(); }
问;字符流可以复制一个图片么?
有可能,但几率很低。
*23-IO流(演示键盘录入)
为什么是这个结果。
——输入,a一个字节,回车两个字节!
13——->\r
10——->\n
所以默认是输入设备和输出设备不用关流!
*24-IO流(读取键盘录入)
练习:
public static void readKey2() throws IOException { /* * 获取用户键盘录入的数据, * 并将数据变成大写显示在控制台上, * 如果用户输入的是over,结束键盘录入。 * * 思路: * 1,因为键盘录入只读取一个字节,要判断是否是over,需要将读取到的字节拼成字符串。 * 2,那就需要一个容器。StringBuilder. * 3,在用户回车之前将录入的数据变成字符串判断即可。 * */ //1,创建容器。 StringBuilder sb = new StringBuilder(); //2,获取键盘读取流。 InputStream in = System.in; //3,定义变量记录读取到的字节,并循环获取。 int ch = 0; while((ch=in.read())!=-1){ // 在存储之前需要判断是否是换行标记 ,因为换行标记不存储。 if(ch=='\r') continue; if(ch=='\n'){ String temp = sb.toString(); if("over".equals(temp)) break; System.out.println(temp.toUpperCase()); sb.delete(0, sb.length()); } else //将读取到的字节存储到StringBuilder中。 sb.append((char)ch); // System.out.println(ch); } }
*25-IO流(转换流)
注意到上面的代码似曾相识,就是读取一行的时候,。
转换流 InputStreamReader.
字符———>转字节
outputstreamWriter 编码
编解码。
简体中文的操作系统都是gbk
再改进:
Outputstreamwriter是Filewriter‘的父类
简化代码:
背下来!!
*26-IO流(转换流-需求演示)
*27-IO流(流的操作基本规律)
*28-IO流(流的操作基本规律-需求体现-1)
*29-IO流(流的操作基本规律-需求体现-2)
*30-IO流(转换流的编码解码)
新需求:
Unicode–>utf-8
有局限性,
所以。
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(“gbk_3.txt”),”GBK”);
// FileWriter fw = new FileWriter(“gbk_1.txt”);
/*
* 这两句代码的功能是等同的。
* FileWriter:其实就是转换流指定了本机默认码表的体现。而且这个转换流的子类对象,可以方便操作文本文件。
* 简单说:操作文件的字节流+本机默认的编码表。
* 这是按照默认码表来操作文件的便捷类。
*
* 如果操作文本文件需要明确具体的编码。FileWriter就不行了。必须用转换流。
*
*/
‘
public static void writeText_3() throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"),"UTF-8"); osw.write("你好"); osw.close(); }
读出来:
package cn.itcast.io.p1.transstream.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class TransStreamDemo3 { / * @param args * @throws IOException */ public static void main(String[] args) throws IOException { readText_2(); } public static void readText_2() throws IOException, FileNotFoundException { InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk_1.txt"),"utf-8"); char[] buf = new char[10]; int len = isr.read(buf); String str = new String(buf,0,len); System.out.println(str); isr.close(); } public static void readText_1() throws IOException { FileReader fr = new FileReader("gbk_1.txt"); char[] buf = new char[10]; int len = fr.read(buf); String str = new String(buf,0,len); System.out.println(str); fr.close(); } public static void writeText_3() throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"),"UTF-8"); osw.write("你好"); osw.close(); } public static void writeText_2() throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"),"GBK"); // OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"),"GBK"); // FileWriter fw = new FileWriter("gbk_1.txt"); /* * 这两句代码的功能是等同的。 * FileWriter:其实就是转换流指定了本机默认码表的体现。而且这个转换流的子类对象,可以方便操作文本文件。 * 简单说:操作文件的字节流+本机默认的编码表。 * 这是按照默认码表来操作文件的便捷类。 * * 如果操作文本文件需要明确具体的编码。FileWriter就不行了。必须用转换流。 * */ osw.write("你好"); osw.close(); } public static void writeText_1() throws IOException { FileWriter fw = new FileWriter("gbk_1.txt"); fw.write("你好"); fw.close(); } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/104499.html
