Day20_IO第二天

1、IO体系总图





2、IO体系——字节流



记忆路线:输入输出流前面加File和Buffered,这样就全记住了

3、表达式解释

表达式:由变量和常量通过运算符连接起来的式子,单个的常量和变量也是表达式

4、标准代码JDK1.6之前(掌握

1、通过标准输入输出流,一个字节一个字节拷贝文件
  1. /**
  2. * 将source拷贝到dest中 source:源文件 dest:目标文件
  3. */
  4. public static void copy1(String source, String dest) {
  5. // 输入流
  6. FileInputStream fis = null;
  7. // 输出流
  8. FileOutputStream fos = null;
  9. try {
  10. fis = new FileInputStream(source);
  11. fos = new FileOutputStream(dest);
  12. // 读取到的数据
  13. int data;
  14. while ((data = fis.read()) != -1) {
  15. fos.write(data);
  16. }
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. } finally{
  20. if(fis != null){
  21. try {
  22. fis.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. if(fos != null){
  28. try {
  29. fos.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. }

2、通过标准输入输出流,采用小数组的方式拷贝文件

  1. public static void copy2(String source, String dest) {
  2. // 输入流
  3. FileInputStream fis = null;
  4. // 输出流
  5. FileOutputStream fos = null;
  6. try {
  7. fis = new FileInputStream(source);
  8. fos = new FileOutputStream(dest);
  9. //bys里存的是读取到的数据
  10. byte[] bys= new byte[1024];
  11. //读取了几个数据
  12. int len;
  13. while((len=fis.read(bys)) != -1){
  14. fos.write(bys, 0, len);
  15. }
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }finally{
  19. if(fis != null){
  20. try {
  21. fis.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. if(fos != null){
  27. try {
  28. fos.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }


3、通过高效流和字节数组的方式拷贝文件(这个必须掌握
  1. public void copy3(String source, String dest) throws Exception {
  2. BufferedInputStream bis = null;
  3. BufferedOutputStream bos = null;
  4. try {
  5. bis = new BufferedInputStream(new FileInputStream(source));
  6. bos = new BufferedOutputStream(new FileOutputStream(dest));
  7. byte[] bys = new byte[1024*20];
  8. int len;
  9. while((len=bis.read(bys)) != -1){
  10. bos.write(bys, 0, len);
  11. }
  12. } catch (Exception e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }finally{
  16. if(bis != null){
  17. try {
  18. bis.close();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. if(bos != null){
  24. try {
  25. bos.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. }


5、标准代码JDK1.7(掌握


  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. public class CopyDemo {
  6. public static void main(String[] args) {
  7. /*
  8. 格式:
  9. try(
  10. //声明输入输出流
  11. ){
  12. //拷贝文件
  13. }catch(Exception e){
  14. //异常处理
  15. }
  16. */
  17. try(
  18. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(".project"));
  19. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));
  20. ){
  21. //存储读取到的数据
  22. byte[] data = new byte[1024*8];
  23. //存储读取到的数据的长度
  24. int len = -1;
  25. //通过read方法将数据读取到data数组中,读取了几个数据呢?读取了len个数据
  26. while((len=bis.read(data)) !=-1){
  27. bos.write(data,0,len);
  28. }
  29. }catch(Exception e){
  30. e.printStackTrace();
  31. }
  32. }
  33. }


5、案例(掌握)

1、将写出的字节异或上一个数,这个数就是密钥,解密的时候再次异或就可以了
2、在控制台录入文件的路径,将文件拷贝到当前项目下
3、将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出


6、案例代码(掌握)

1、将写出的字节异或上一个数,这个数就是密钥,解密的时候再次异或就可以了
  1. package com.heima.test;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. public class Test1 {
  9. /**
  10. * @param args
  11. * @throws IOException
  12. * 将写出的字节异或上一个数,这个数就是密钥,解密的时候再次异或就可以了
  13. */
  14. public static void main(String[] args) throws IOException {
  15. BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
  16. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
  17. int b;
  18. while((b = bis.read()) != -1) {
  19. bos.write(b ^ 123);
  20. }
  21. bis.close();
  22. bos.close();
  23. }
  24. }

2、在控制台录入文件的路径,将文件拷贝到当前项目下
  1. package com.heima.test;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.Scanner;
  10. public class Test2 {
  11. /**
  12. * 在控制台录入文件的路径,将文件拷贝到当前项目下
  13. *
  14. * 分析:
  15. *
  16. * 1,定义方法对键盘录入的路径进行判断,如果是文件就返回
  17. * 2,在主方法中接收该文件
  18. * 3,读和写该文件
  19. * @throws IOException
  20. */
  21. public static void main(String[] args) throws IOException {
  22. File file = getFile(); //获取文件
  23. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
  24. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
  25. int b;
  26. while((b = bis.read()) != -1) {
  27. bos.write(b);
  28. }
  29. bis.close();
  30. bos.close();
  31. }
  32. /*
  33. * 定义一个方法获取键盘录入的文件路径,并封装成File对象返回
  34. * 1,返回值类型File
  35. * 2,参数列表无
  36. */
  37. public static File getFile() {
  38. Scanner sc = new Scanner(System.in); //创建键盘录入对象
  39. System.out.println("请输入一个文件的路径:");
  40. while(true) {
  41. String line = sc.nextLine(); //接收键盘录入的路径
  42. File file = new File(line); //封装成File对象,并对其进行判断
  43. if(!file.exists()) {
  44. System.out.println("您录入的文件路径不存在,请重新录入:");
  45. }else if(file.isDirectory()) {
  46. System.out.println("您录入的是文件夹路径,请重新录入:");
  47. }else {
  48. return file;
  49. }
  50. }
  51. }
  52. }
3、将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
  1. package com.heima.test;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. public class Test3 {
  7. /**
  8. * 将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
  9. *
  10. * 分析:
  11. * 1,创建键盘录入对象
  12. * 2,创建输出流对象,关联text.txt文件
  13. * 3,定义无限循环
  14. * 4,遇到quit退出循环
  15. * 5,如果不quit,就将内容写出
  16. * 6,关闭流
  17. * @throws IOException
  18. */
  19. public static void main(String[] args) throws IOException {
  20. //1,创建键盘录入对象
  21. Scanner sc = new Scanner(System.in);
  22. //2,创建输出流对象,关联text.txt文件
  23. FileOutputStream fos = new FileOutputStream("text.txt");
  24. System.out.println("请输入数据:");
  25. //3,定义无限循环
  26. while(true) {
  27. String line = sc.nextLine(); //将键盘录入的数据存储在line中
  28. //4,遇到quit退出循环
  29. if("quit".equals(line)) {
  30. break;
  31. }
  32. //5,如果不quit,就将内容写出
  33. fos.write(line.getBytes()); //字符串写出必须转换成字节数组
  34. fos.write("\r\n".getBytes());
  35. }
  36. //6,关闭流
  37. fos.close();
  38. }

  39. }
方式2
  1. public class Demo2 {
  2. public static void main(String[] args)throws Exception {
  3. Scanner sc = new Scanner(System.in);
  4. String line;
  5. FileOutputStream fos = new FileOutputStream("record.txt");
  6. //如果输入的不是quite就将用户输入的信息写入到文本文件里面
  7. while(!(line=sc.nextLine()) .equals("quite")){
  8. fos.write(line.getBytes());
  9. //System.getProperty("line.separator")获取换行符
  10. fos.write(System.getProperty("line.separator").getBytes());
  11. }
  12. fos.close();
  13. }
  14. }

7、今天必须掌握的内容,面试题,笔试题。

1、说说字节流体系
2、说说以下代码怎么念
int data;
 
while ((data = fis.read()) != -1) {
fos.write(data);
}

3、说说以下段代码怎么念
byte[] data = new byte[1024*8];
int len = -1;
while((len=bis.read(data)) !=-1){
    bos.write(data,0,len);
}

3、flush和close区别 
flush用来刷新缓冲区的,刷新后可以再次写出,即flush后流仍然可以使用
close用来关闭流释放资源的的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出,即不能再使用该流
4、说说字节流文件拷贝:定义小数组和不定义小数组的原理
5、高效字节流结合小数组拷贝文件的两种标准代码(JDK1.7以前和JDK1.7以后)
6、练习:从控制台录入数据将数据写入到a.txt中,直到输入quite程序停止。当程序下次启动时可以继续向a.txt中追加数据

7、有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中


























posted on 2016-12-23 09:54  虫虫爬啊爬  阅读(235)  评论(0编辑  收藏  举报

导航