输入流

  1. package teststream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. public class TestFileInputStream {
  7. public static void main(String[] args){
  8. // try {//理解为 文件是一个仓库 fis对象搬运工 推一个平板车
  9. // //创建一个字节型的文件输入流 读取一个文件中的内容
  10. // File file = new File("D://test//Test.txt");
  11. // FileInputStream fis = new FileInputStream(file);//真实去读文件
  12. // int code = fis.read();//读取一个字节 -1
  13. // while(code!=-1) {
  14. // System.out.print((char)code);//读取的字节对应的Unicode码 0---65535
  15. // code = fis.read();//读取一个字节 -1
  16. // }
  17. // } catch (FileNotFoundException e) {//编译时异常
  18. // e.printStackTrace();
  19. // } catch (IOException e) {
  20. // e.printStackTrace();
  21. // }
  22. // try {//流---管道
  23. // FileInputStream fis = new FileInputStream("D://test//Test.txt");
  24. // int v = fis.available();//流管道中有多少缓存的字节 读取网络数据的数据可能会有问题
  25. // System.out.println(v);
  26. // //创建一个空的数组--->小推车
  27. // byte[] b = new byte[5];
  28. // int count = fis.read(b);//去文件里读东西 装入数组内 读取到的有效字节个数
  29. // while(count!=-1){
  30. // String value = new String(b,0,count);//第一次 a b c d e
  31. // System.out.print(value); //第二次 f g \r\n h
  32. // count = fis.read(b); //第三次 i j k l m
  33. // } //第四次 n \r \n o p
  34. // } catch (IOException e) { //第五次 q \r \n o p count--->1 此时的数组 5个元素
  35. // e.printStackTrace();
  36. // }
  37. FileInputStream fis = null;
  38. try {
  39. fis = new FileInputStream(new File("D://test//Test.txt"));
  40. fis.skip(5);
  41. int code = fis.read();
  42. System.out.println((char)code);
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. } finally {
  46. try {
  47. if(fis!=null) {
  48. fis.close();//关闭的是流通道 不是file对象 关闭这件事情必须要做
  49. }
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }

输出流

  1. package teststream;
  2. import java.io.*;
  3. public class TestFileOutputStream {
  4. public static void main(String[] args){
  5. // try {
  6. // //创建一个字节型文件输出流
  7. // File file = new File("D://test//aaa.txt");
  8. // //创建的是文件输入流 若文件路径有问题 则抛出异常 FileNotFoundException
  9. // //创建的是文件输出流 若文件路径有问题 则直接帮我们创建一个新的文件
  10. // //将内容写入文件
  11. // FileOutputStream fos = new FileOutputStream(file,true);
  12. // fos.write(49);//a a是写入在哪里了? 1+1=2
  13. // fos.write('+');
  14. // fos.write(49);
  15. // fos.write('=');
  16. // fos.write(50);
  17. // fos.flush();//刷新 将管道中的字节 推入文件中
  18. // System.out.println("写入完毕");
  19. // } catch (IOException e) {
  20. // e.printStackTrace();
  21. // }
  22. FileOutputStream fos = null;
  23. try {
  24. fos = new FileOutputStream("D://test//bbb.txt",true);
  25. //创建一个数组 带着信息
  26. //byte[] b = new byte[]{97,98,99};
  27. String str = "1+1=2";//String-->byte[]
  28. byte[] b = str.getBytes();
  29. fos.write(b);
  30. fos.flush();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. } finally {
  34. try {
  35. if(fos!=null) {
  36. fos.close();
  37. }
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. }