文件IO

Java IO
    IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中
    按操作数据分为 字节流和字符流
        字符流的由来:
        其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。
        再对这个文字进行操作。简单说:字节流+编码表
    按流向分为 输入流和输出流
        输入流和输出流相对于内存设备而言
        将外设中的数据读取到内存中:输入
        将内存的数写入到外设中:输出。

字节流的两个顶层父类:
1,InputStream  2,OutputStream

字符流的两个顶层父类:
1,Reader 2,Writer

Demo1 将一些文字存储到硬盘一个文件中

  1. public class FileWriterDemo {
  2.  
  3. private static final String LINE_SEPARATOR = System.getProperty("line.separator");
  4.  
  5. public static void main(String[] args) throws IOException {
  6.  
  7. // 创建一个可以往文件中写入字符数据的字符输出流对象。
  8. /*
  9. * 既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地)。
  10. * 如果文件不存在,则会自动创建。 如果文件存在,则会被覆盖。
  11. * 如果构造函数中加入true,可以实现对文件进行续写!
  12. */
  13. FileWriter fw = new FileWriter("demo.txt", true);
  14.  
  15. /*
  16. * 调用Writer对象中的write(string)方法,写入数据。
  17. * 其实数据写入到临时存储缓冲区中。
  18. */
  19. fw.write("abcde" + LINE_SEPARATOR + "hahaha");
  20. // fw.write("xixi");
  21.  
  22. // fw.flush(); 进行刷新,将数据直接写到目的地中。
  23.  
  24. fw.close(); //关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。
  25.  
  26. // fw.write("haha");// java.io.IOException: Stream closed
  27.  
  28. }
  29.  
  30. }

Demo2 读取一个文本文件。将读取到的字符打印到控制台

  1. public class FileReaderDemo {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. // 1,创建读取字符数据的流对象。
  5. //在创建读取流对象时,必须要明确被读取的文件。一定要确定该文件是存在的。
  6. //用一个读取流关联一个已存在文件。
  7. FileReader fr = new FileReader("F:\\demo.txt");
  8.  
  9. int ch = 0;
  10.  
  11. while ((ch = fr.read()) != -1) {
  12. System.out.println((char) ch);
  13. }
  14.  
  15. /*
  16. * //用Reader中的read方法读取字符。 int ch = fr.read(); System.out.println((char)ch);
  17. * int ch1 = fr.read(); System.out.println(ch1); int ch2 = fr.read();
  18. * System.out.println(ch2);
  19. */
  20.  
  21. fr.close();
  22. }
  23. }

Demo3 读取一个文本文件 采用字符数组的方法

  1. public class FileReaderDemo2 {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. FileReader fr = new FileReader("F:\\demo.txt");
  5.  
  6. //使用read(char[])读取文本文件数据。先创建字符数组。
  7. char[] buf = new char[1024];
  8.  
  9. int len = 0;
  10.  
  11. while((len=fr.read(buf))!=-1){
  12. System.out.println(new String(buf,0,len));
  13. }
  14.  
  15. /*
  16. int num = fr.read(buf);//将读取到的字符存储到数组中。
  17. System.out.println(num+":"+new String(buf,0,num));
  18. int num1 = fr.read(buf);//将读取到的字符存储到数组中。
  19. System.out.println(num1+":"+new String(buf,0,num1));
  20. int num2 = fr.read(buf);//将读取到的字符存储到数组中。
  21. System.out.println(num2+":"+new String(buf));
  22. */
  23.  
  24. fr.close();
  25. }
  26. }

Demo4 文本文件复制

  1. public class CopyTextTest {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. // 1,读取一个已有的文本文件,使用字符读取流和文件相关联。
  5. FileReader fr = new FileReader("F:\\demo.txt");
  6. // 2,创建一个目的,用于存储读到数据。
  7. FileWriter fw = new FileWriter("F:\\copydemo.txt");
  8. // 3,频繁的读写操作。
  9. int ch = 0;
  10. while ((ch = fr.read()) != -1) {
  11. fw.write(ch);
  12. }
  13. // 4,关闭流资源。
  14.  
  15. fw.close();
  16. fr.close();
  17. }
  18. }

Demo5 文本文件复制加缓存

  1. public class CopyTextTest_2 {
  2. private static final int BUFFER_SIZE = 1024;
  3.  
  4. public static void main(String[] args) {
  5.  
  6. FileReader fr = null;
  7. FileWriter fw = null;
  8. try {
  9. fr = new FileReader("F:\\demo.txt");
  10. fw = new FileWriter("F:\\democopy2.txt");
  11.  
  12. // 创建一个临时容器,用于缓存读取到的字符。
  13. char[] buf = new char[BUFFER_SIZE];// 这就是缓冲区。
  14.  
  15. // 定义一个变量记录读取到的字符数,(其实就是往数组里装的字符个数 )
  16. int len = 0;
  17.  
  18. while ((len = fr.read(buf)) != -1) {
  19. fw.write(buf, 0, len);
  20. }
  21.  
  22. } catch (Exception e) {
  23. // System.out.println("读写失败");
  24. throw new RuntimeException("读写失败");
  25. } finally {
  26. if (fw != null)
  27. try {
  28. fw.close();
  29. } catch (IOException e) {
  30.  
  31. e.printStackTrace();
  32. }
  33. if (fr != null)
  34. try {
  35. fr.close();
  36. } catch (IOException e) {
  37.  
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. }

字符流的缓存区
    缓存区的出现提高了对数据的读写效率
    对应类 BufferedReader BufferedWriter
    缓存区要结合流才可以使用 在流的基础上对流的功能进行了增强

Demo6 缓存写

  1. public class BufferedWriterDemo {
  2. private static final String LINE_SEPARATOR = System.getProperty("line.separator");
  3.  
  4. public static void main(String[] args) throws IOException {
  5.  
  6. FileWriter fw = new FileWriter("F:\\buf.txt");
  7.  
  8. // 为了提高写入的效率。使用了字符流的缓冲区。
  9. // 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联
  10. BufferedWriter bufw = new BufferedWriter(fw);
  11.  
  12. // 使用缓冲区的写入方法将数据先写入到缓冲区中。
  13. // bufw.write("abcdefq"+LINE_SEPARATOR+"hahahha");
  14. // bufw.write("xixiixii");
  15. // bufw.newLine();
  16. // bufw.write("heheheheh");
  17.  
  18. for (int x = 1; x <= 4; x++) {
  19. bufw.write("abcdef" + x);
  20. bufw.newLine();
  21. bufw.flush();
  22. }
  23.  
  24. // 使用缓冲区的刷新方法将数据刷目的地中。
  25. // bufw.flush();
  26.  
  27. // 关闭缓冲区。其实关闭的就是被缓冲的流对象。
  28. bufw.close();
  29.  
  30. // fw.write("hehe");
  31.  
  32. // fw.close();
  33. }
  34. }

Demo7 缓存读

  1. public class BufferedReaderDemo {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. FileReader fr = new FileReader("F:\\buf.txt");
  5.  
  6. BufferedReader bufr = new BufferedReader(fr);
  7.  
  8. String line = null;
  9.  
  10. while ((line = bufr.readLine()) != null) {
  11. System.out.println(line);
  12. }
  13. /*
  14. * String line1 = bufr.readLine(); System.out.println(line1); String line2 =
  15. * bufr.readLine(); System.out.println(line2); String line3 =
  16. * bufr.readLine(); System.out.println(line3); String line4 =
  17. * bufr.readLine(); System.out.println(line4); String line5 =
  18. * bufr.readLine(); System.out.println(line5);
  19. */
  20.  
  21. bufr.close();
  22.  
  23. }
  24.  
  25. }

Demo8 缓存复制

  1. public class CopyTextByBufTest {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. FileReader fr = new FileReader("F:\\buf.txt");
  5. BufferedReader bufr = new BufferedReader(fr);
  6.  
  7. FileWriter fw = new FileWriter("F:\\buf_copy.txt");
  8. BufferedWriter bufw = new BufferedWriter(fw);
  9.  
  10. String line = null;
  11. while ((line = bufr.readLine()) != null) {
  12. bufw.write(line);
  13. bufw.newLine();
  14. bufw.flush();
  15. }
  16.  
  17. /*
  18. * int ch = 0;
  19. *
  20. * while((ch=bufr.read())!=-1){
  21. *
  22. * bufw.write(ch); }
  23. */
  24. bufw.close();
  25. bufr.close();
  26. }
  27.  
  28. }

Demo9 自定义缓存区

  1. public class MyBufferedReader extends Reader {
  2. private Reader r;
  3.  
  4. // 定义一个数组作为缓冲区。
  5. private char[] buf = new char[1024];
  6. // 定义一个指针用于操作这个数组中的元素。当操作到最后一个元素后,指针应该归零。
  7. private int pos = 0;
  8. // 定义一个计数器用于记录缓冲区中的数据个数。 当该数据减到0,就从源中继续获取数据到缓冲区中。
  9. private int count = 0;
  10.  
  11. MyBufferedReader(Reader r) {
  12. this.r = r;
  13. }
  14.  
  15. //该方法从缓冲区中一次取一个字符。
  16. public int myRead() throws IOException {
  17.  
  18. if (count == 0) {
  19. count = r.read(buf);
  20. pos = 0;
  21. }
  22. if (count < 0)
  23. return -1;
  24.  
  25. char ch = buf[pos++];
  26.  
  27. count--;
  28.  
  29. return ch;
  30.  
  31. }
  32.  
  33. public String myReadLine() throws IOException {
  34.  
  35. StringBuilder sb = new StringBuilder();
  36.  
  37. int ch = 0;
  38. while ((ch = myRead()) != -1) {
  39.  
  40. if (ch == '\r')
  41. continue;
  42. if (ch == '\n')
  43. return sb.toString();
  44. // 将从缓冲区中读到的字符,存储到缓存行数据的缓冲区中。
  45. sb.append((char) ch);
  46.  
  47. }
  48.  
  49. if (sb.length() != 0)
  50. return sb.toString();
  51. return null;
  52. }
  53.  
  54. public void myClose() throws IOException {
  55.  
  56. r.close();
  57. }
  58.  
  59. @Override
  60. public int read(char[] cbuf, int off, int len) throws IOException {
  61.  
  62. return 0;
  63. }
  64.  
  65. @Override
  66. public void close() throws IOException {
  67. }
  68. }
  1. public class MyBufferedReaderDemo {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. FileReader fr = new FileReader("F:\\buf.txt");
  5.  
  6. MyBufferedReader bufr = new MyBufferedReader(fr);
  7.  
  8. String line = null;
  9.  
  10. while ((line = bufr.myReadLine()) != null) {
  11. System.out.println(line);
  12. }
  13.  
  14. bufr.myClose();
  15. }
  16. }

装饰设计模式
    对一组对象的功能进行增强时,就可以使用该模式进行问题的解决。
    
装饰和继承都能实现一样的特点:进行功能的扩展增强。

有什么区别呢?

首先有一个继承体系。
Writer
    |--TextWriter:用于操作文本
    |--MediaWriter:用于操作媒体。
    
想要对操作的动作进行效率的提高。
按照面向对象,可以通过继承对具体的进行功能的扩展。
效率提高需要加入缓冲技术。
    
Writer
    |--TextWriter:用于操作文本
        |--BufferTextWriter:加入了缓冲技术的操作文本的对象。
    |--MediaWriter:用于操作媒体。
        |--BufferMediaWriter:

但是这样做好像并不理想 如果这个体系进行功能扩展,又多了流对象。
那么这个流要提高效率,是不是也要产生子类呢?是。这时就会发现只为提高功能,进行的继承,
导致继承体系越来越臃肿。不够灵活。

重新思考这个问题?
既然加入的都是同一种技术--缓冲。
前一种是让缓冲和具体的对象相结合。
可不可以将缓冲进行单独的封装,哪个对象需要缓冲就将哪个对象和缓冲关联。

  1. class Buffer{
  2. Buffer(TextWriter w)
  3. {}
  4.  
  5. Buffer(MediaWirter w)
  6. {
  7.  
  8. }
  9. }
  10. class BufferWriter extends Writer{
  11. BufferWriter(Writer w)
  12. {
  13. }
  14. }

Writer
    |--TextWriter:用于操作文本
    |--MediaWriter:用于操作媒体。
    |--BufferWriter:用于提高效率。
    
装饰比继承灵活。

特点:装饰类和被装饰类都必须所属同一个接口或者父类。

Demo10 装饰设计模式例子

  1. public class PersonDemo {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. Person p = new Person();
  6. // p.chifan();
  7.  
  8. NewPerson p1 = new NewPerson(p);
  9. p1.chifan();
  10.  
  11. NewPerson2 p2 = new NewPerson2();
  12. p2.chifan();
  13. }
  14.  
  15. }
  16.  
  17. class Person{
  18. void chifan(){
  19. System.out.println("吃饭");
  20. }
  21. }
  22. //这个类的出现是为了增强Person而出现的。
  23. class NewPerson{
  24. private Person p ;
  25. NewPerson(Person p){
  26. this.p = p;
  27. }
  28.  
  29. public void chifan(){
  30. System.out.println("开胃酒");
  31. p.chifan();
  32. System.out.println("甜点");
  33.  
  34. }
  35.  
  36. }
  37.  
  38. class NewPerson2 extends Person{
  39. public void chifan(){
  40. System.out.println("开胃酒");
  41. super.chifan();
  42. System.out.println("甜点");
  43. }
  44. }

Demo11 LineNumberReaderDemo

  1. public class LineNumberReaderDemo {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. FileReader fr = new FileReader("F:\\demo.txt");
  5. LineNumberReader lnr = new LineNumberReader(fr);
  6.  
  7. String line = null;
  8. lnr.setLineNumber(100);
  9. while((line=lnr.readLine())!=null){
  10. System.out.println(lnr.getLineNumber()+":"+line);
  11. }
  12.  
  13. lnr.close();
  14. }
  15. }

字节流
    基本操作与字符流类相同 但它不仅可以操作字符 还可以操作其它媒体文件

Demo12 写文件

  1. public class ByteStreamDemo {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. // 1,创建字节输出流对象。用于操作文件.
  5. FileOutputStream fos = new FileOutputStream("F:\\bytedemo.txt");
  6.  
  7. // 2,写数据。直接写入到了目的地中。
  8. fos.write("abcdefg".getBytes());
  9.  
  10. // fos.flush();
  11. fos.close();// 关闭资源动作要完成。
  12. }
  13. }

Demo13 拷贝文件

  1. public class CopyMp3Test {
  2.  
  3. public static void main(String[] args) throws IOException {
  4.  
  5. copy_1();
  6. copy_2();
  7. copy_3();
  8. // copy_4();
  9.  
  10. }
  11.  
  12. // 千万不要用,效率没有!
  13. public static void copy_4() throws IOException {
  14. FileInputStream fis = new FileInputStream("F:\\0.zip");
  15. FileOutputStream fos = new FileOutputStream("F:\\4.zip");
  16.  
  17. long start = System.currentTimeMillis();
  18.  
  19. int ch = 0;
  20.  
  21. while ((ch = fis.read()) != -1) {
  22. fos.write(ch);
  23. }
  24.  
  25. long end = System.currentTimeMillis();
  26. System.out.println("拷贝完成4---" + (start - end));
  27.  
  28. fos.close();
  29. fis.close();
  30. }
  31.  
  32. // 不建议。 测试速度还可以
  33. public static void copy_3() throws IOException {
  34. FileInputStream fis = new FileInputStream("F:\\0.zip");
  35. FileOutputStream fos = new FileOutputStream("F:\\3.zip");
  36.  
  37. long start = System.currentTimeMillis();
  38.  
  39. byte[] buf = new byte[fis.available()];
  40. fis.read(buf);
  41. fos.write(buf);
  42.  
  43. long end = System.currentTimeMillis();
  44. System.out.println("拷贝完成3---" + (start - end));
  45.  
  46. fos.close();
  47. fis.close();
  48. }
  49.  
  50. // 速度一般
  51. public static void copy_2() throws IOException {
  52.  
  53. FileInputStream fis = new FileInputStream("F:\\0.zip");
  54. BufferedInputStream bufis = new BufferedInputStream(fis);
  55.  
  56. FileOutputStream fos = new FileOutputStream("F:\\2.zip");
  57. BufferedOutputStream bufos = new BufferedOutputStream(fos);
  58.  
  59. long start = System.currentTimeMillis();
  60.  
  61. int ch = 0;
  62.  
  63. while ((ch = bufis.read()) != -1) {
  64. bufos.write(ch);
  65. }
  66.  
  67. long end = System.currentTimeMillis();
  68. System.out.println("拷贝完成2---" + (start - end));
  69.  
  70. bufos.close();
  71. bufis.close();
  72. }
  73.  
  74. // 速度最快
  75. public static void copy_1() throws IOException {
  76.  
  77. FileInputStream fis = new FileInputStream("F:\\0.zip");
  78. FileOutputStream fos = new FileOutputStream("F:\\1.zip");
  79.  
  80. byte[] buf = new byte[1024];
  81.  
  82. int len = 0;
  83.  
  84. long start = System.currentTimeMillis();
  85.  
  86. while ((len = fis.read(buf)) != -1) {
  87. fos.write(buf, 0, len);
  88. }
  89.  
  90. long end = System.currentTimeMillis();
  91.  
  92. System.out.println("拷贝完成1---" + (start - end));
  93.  
  94. fos.close();
  95. fis.close();
  96. }
  97. }

Demo14 读取键盘输入

  1. public class ReadKey {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. // readKey();
  5. // System.out.println((int)'\r');
  6. // System.out.println((int)'\n');
  7.  
  8. readKey2();
  9.  
  10. }
  11.  
  12. public static void readKey2() throws IOException {
  13.  
  14. /*
  15. * 获取用户键盘录入的数据, 并将数据变成大写显示在控制台上, 如果用户输入的是over,结束键盘录入。
  16. *
  17. * 思路: 1,因为键盘录入只读取一个字节,要判断是否是over,需要将读取到的字节拼成字符串。 2,那就需要一个容器。StringBuilder.
  18. * 3,在用户回车之前将录入的数据变成字符串判断即可。
  19. */
  20.  
  21. // 1,创建容器。
  22. StringBuilder sb = new StringBuilder();
  23.  
  24. // 2,获取键盘读取流。
  25. InputStream in = System.in;
  26.  
  27. // 3,定义变量记录读取到的字节,并循环获取。
  28. int ch = 0;
  29.  
  30. while ((ch = in.read()) != -1) {
  31.  
  32. // 在存储之前需要判断是否是换行标记 ,因为换行标记不存储。
  33. if (ch == '\r')
  34. continue;
  35. if (ch == '\n') {
  36. String temp = sb.toString();
  37. if ("over".equals(temp))
  38. break;
  39. System.out.println(temp.toUpperCase());
  40. sb.delete(0, sb.length());
  41. } else
  42. // 将读取到的字节存储到StringBuilder中。
  43. sb.append((char) ch);
  44.  
  45. // System.out.println(ch);
  46. }
  47.  
  48. }
  49.  
  50. public static void readKey() throws IOException {
  51.  
  52. InputStream in = System.in;
  53.  
  54. int ch = in.read();// 阻塞式方法。
  55. System.out.println(ch);
  56. int ch1 = in.read();// 阻塞式方法。
  57. System.out.println(ch1);
  58. int ch2 = in.read();// 阻塞式方法。
  59. System.out.println(ch2);
  60.  
  61. // in.close();
  62.  
  63. // InputStream in2 = System.in;
  64. // int ch3 = in2.read();
  65.  
  66. }
  67. }

流的操作规律:

之所以要弄清楚这个规律,是因为流对象太多,开发时不知道用哪个对象合适。

想要知道开发时用到哪些对象。只要通过四个明确即可。

1,明确源和目的(汇)
    源:InputStream  Reader
    目的:OutputStream  Writer

2,明确数据是否是纯文本数据。
    源:是纯文本:Reader
        否:InputStream
    目的:是纯文本 Writer
        否:OutputStream
    
    到这里,就可以明确需求中具体要使用哪个体系。

3,明确具体的设备。
    源设备:
        硬盘:File
        键盘:System.in
        内存:数组
        网络:Socket流
        
    目的设备:
        硬盘:File
        控制台:System.out
        内存:数组
        网络:Socket流

4,是否需要其他额外功能。
    1,是否需要高效(缓冲区);
        是,就加上buffer.
    2,转换。

需求1:复制一个文本文件。
    1,明确源和目的。
        源:InputStream Reader
        目的:OutputStream  Writer
    2,是否是纯文本?
        是!
        源:Reader
        目的:Writer
        
    3,明确具体设备。
        源:
            硬盘:File
        目的:
            硬盘:File
    
        FileReader fr = new FileReader("a.txt");
        FileWriter fw = new FileWriter("b.txt");
        
    4,需要额外功能吗?
        需要,需要高效。
        BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));

需求2:读取键盘录入信息,并写入到一个文件中。
        
    1,明确源和目的。
        源:InputStream Reader
        目的:OutputStream  Writer
    2,是否是纯文本呢?
        是,
        源:Reader
        目的:Writer
    3,明确设备
        源:
            键盘。System.in
        目的:
            硬盘。File
            
        InputStream in = System.in;
        FileWriter fw = new FileWriter("b.txt");
        这样做可以完成,但是麻烦。将读取的字节数据转成字符串。再由字符流操作。
    4,需要额外功能吗?
        需要。转换。    将字节流转成字符流。因为名确的源是Reader,这样操作文本数据做便捷。
            所以要将已有的字节流转成字符流。使用字节-->字符 。InputStreamReader
        InputStreamReader isr = new InputStreamReader(System.in);
        FileWriter fw = new FileWriter("b.txt");
        
        还需要功能吗?
        需要:想高效。
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));
                
    
需求3:将一个文本文件数据显示在控制台上。
    1,明确源和目的。
        源:InputStream Reader
        目的:OutputStream  Writer
    2,是否是纯文本呢?
        是,
        源:Reader
        目的:Writer
    3,明确具体设备
        源:
            硬盘:File
        目的:
            控制台:System.out
            
        FileReader fr = new FileReader("a.txt");
        OutputStream out = System.out;//PrintStream
    4,需要额外功能吗?
        需要,转换。
        FileReader fr= new FileReader("a.txt");
        OutputStreamWriter osw = new OutputStreamWriter(System.out);
        需要,高效。
        BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));

需求4:读取键盘录入数据,显示在控制台上。
    1,明确源和目的。
        源:InputStream Reader
        目的:OutputStream  Writer
    2,是否是纯文本呢?
        是,
        源:Reader
        目的:Writer
    3,明确设备。
        源:
            键盘:System.in
        目的:
            控制台:System.out
        
        InputStream in = System.in;
        OutputStream out = System.out;
        
    4,明确额外功能?
        需要转换,因为都是字节流,但是操作的却是文本数据。
        所以使用字符流操作起来更为便捷。
        InputStreamReader isr = new InputStreamReader(System.in);
        OutputStreamWriter osw = new OutputStreamWriter(System.out);
        
        为了将其高效。
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));

5,将一个中文字符串数据按照指定的编码表写入到一个文本文件中.
    
    1,目的。OutputStream,Writer
    2,是纯文本,Writer。
    3,设备:硬盘File
    FileWriter fw = new FileWriter("a.txt");
    fw.write("你好");
    
    注意:既然需求中已经明确了指定编码表的动作。
    那就不可以使用FileWriter,因为FileWriter内部是使用默认的本地码表。
    只能使用其父类。OutputStreamWriter.
    OutputStreamWriter接收一个字节输出流对象,既然是操作文件,那么该对象应该是FileOutputStream
    
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"),charsetName);
    
    需要高效吗?
    BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),charsetName));

什么时候使用转换流呢?

转换流:
InputStreamReader :字节到字符的桥梁。解码。
OutputStreamWriter:字符到字节的桥梁。编码。

1,源或者目的对应的设备是字节流,但是操作的却是文本数据,可以使用转换作为桥梁。
        提高对文本操作的便捷。
    2,一旦操作文本涉及到具体的指定编码表时,必须使用转换流 。

转换Demo1

  1. public class TransStreamDemo {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. // 字节流。
  5. InputStream in = System.in;
  6.  
  7. // 将字节转成字符的桥梁。装换流。
  8. InputStreamReader isr = new InputStreamReader(in);
  9.  
  10. // int ch = isr.read();
  11. // System.out.println((char)ch);
  12.  
  13. // 字符流。
  14. BufferedReader bufr = new BufferedReader(isr);
  15.  
  16. OutputStream out = System.out;
  17.  
  18. OutputStreamWriter osw = new OutputStreamWriter(out);
  19.  
  20. BufferedWriter bufw = new BufferedWriter(osw);
  21.  
  22. String line = null;
  23.  
  24. while ((line = bufr.readLine()) != null) {
  25. if ("over".equals(line))
  26. break;
  27. // System.out.println(line.toUpperCase());
  28. // osw.write(line.toUpperCase()+"\r\n");
  29. // osw.flush();
  30.  
  31. bufw.write(line.toUpperCase());
  32. bufw.newLine();
  33. bufw.flush();
  34. }
  35.  
  36. }
  37.  
  38. }

转换Demo2

  1. public class TransStreamDemo2 {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
  5. BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F:\\b.txt")));
  6.  
  7. String line = null;
  8.  
  9. while((line=bufr.readLine())!=null){
  10. if("over".equals(line))
  11. break;
  12.  
  13. bufw.write(line.toUpperCase());
  14. bufw.newLine();
  15. bufw.flush();
  16. }
  17.  
  18. }
  19. }

转换Demo3

  1. public class TransStreamDemo3 {
  2. public static void main(String[] args) throws IOException {
  3.  
  4. readText_2();
  5. }
  6.  
  7. public static void readText_2() throws IOException, FileNotFoundException {
  8.  
  9. InputStreamReader isr = new InputStreamReader(new FileInputStream("F:\\demo.txt"), "utf-8");
  10. char[] buf = new char[10];
  11. int len = isr.read(buf);
  12. String str = new String(buf, 0, len);
  13. System.out.println(str);
  14.  
  15. isr.close();
  16. }
  17.  
  18. public static void readText_1() throws IOException {
  19.  
  20. FileReader fr = new FileReader("gbk_1.txt");
  21.  
  22. char[] buf = new char[10];
  23. int len = fr.read(buf);
  24. String str = new String(buf, 0, len);
  25. System.out.println(str);
  26.  
  27. fr.close();
  28.  
  29. }
  30.  
  31. public static void writeText_3() throws IOException {
  32.  
  33. OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"), "UTF-8");
  34.  
  35. osw.write("你好");
  36. osw.close();
  37.  
  38. }
  39.  
  40. public static void writeText_2() throws IOException {
  41.  
  42. OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"), "GBK");
  43.  
  44. // OutputStreamWriter osw = new OutputStreamWriter(new
  45. // FileOutputStream("gbk_3.txt"),"GBK");
  46. // FileWriter fw = new FileWriter("gbk_1.txt");
  47.  
  48. /*
  49. * 这两句代码的功能是等同的。 FileWriter:其实就是转换流指定了本机默认码表的体现。而且这个转换流的子类对象,可以方便操作文本文件。
  50. * 简单说:操作文件的字节流+本机默认的编码表。 这是按照默认码表来操作文件的便捷类。
  51. *
  52. * 如果操作文本文件需要明确具体的编码。FileWriter就不行了。必须用转换流。
  53. */
  54.  
  55. osw.write("你好");
  56.  
  57. osw.close();
  58.  
  59. }
  60.  
  61. public static void writeText_1() throws IOException {
  62.  
  63. FileWriter fw = new FileWriter("gbk_1.txt");
  64.  
  65. fw.write("你好");
  66.  
  67. fw.close();
  68. }
  69. }

Java 文件IO的更多相关文章

  1. Java - 文件(IO流)

    Java - 文件 (IO)   流的分类:     > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter     ...

  2. Java文件IO操作应该抛弃File拥抱Paths和Files

    Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream;import java.nio.file.FileSystem; ...

  3. Java 文件IO续

    文件IO续 File类    用来将文件和文件夹封装成对象 方便对文件和文件夹的属性信息进行操作    File对象可以作为参数传递给流的构造函数 Demo1 File的构造方法 public cla ...

  4. Java文件IO操作应该抛弃File拥抱Path和Files

    Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream;import java.nio.file.FileSystem; ...

  5. Java文件IO流的操作总结

    Java中的IO操作涉及到的概念及相关类很多,很容易弄混,今天特来整理总结一下,并附上一份完整的文件操作的代码. 概念解析 读和写 流就是管道,向管道里面写数据用输出流:write 从管道里面读数据, ...

  6. Java 文件 IO 操作

    window 路径分割符: \ 表示 windows 系统文件目录分割符 java 代码在 windows 下写某个文件的话需要下面的方式 D:\\soft\\sdclass.txt  其中一个单斜杠 ...

  7. 简易记事本(演示java文件io)

      演示效果:  打开txt文件 输入文字,保存 选择保存地址 生成文件 源代码: package io; import java.io.*; import java.awt.*; import ja ...

  8. java文件IO操作

    package com.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...

  9. java文件io过滤器

    package cn.stat.p1.file; import java.io.File; public class newfilelist { /** * @param args */ public ...

随机推荐

  1. android中文件操作的四种枚举

    1.MODE_PRIVATE:默认操作模式,代表该文件是私有数据,只能被应用自身访问,在该模式下,写入的的内容会覆盖原文件中的内容. 2.MODE_APPEND:该模式会检查文件是否存在,存在就往文件 ...

  2. java 用socket制作一个简易多人聊天室

    代码: 服务器端Server import java.io.*; import java.net.*; import java.util.ArrayList; public class Server{ ...

  3. matlab mat文件读取和调用

    13.1 数据基本操作 本节介绍基本的数据操作,包括工作区的保存.导入和文件打开.13.1.1 文件的存储 MATLAB支持工作区的保存.用户可以将工作区或工作区中的变量以文件的形式保存,以备在需要时 ...

  4. LA 3704 Cellular Automaton

    题意概述: 等价地,本题可以转化为下面的问题: 考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$.求$ ...

  5. Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏

    Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21767   Accepted: 13641 Desc ...

  6. Speed Limit 分类: POJ 2015-06-09 17:47 9人阅读 评论(0) 收藏

    Speed Limit Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17967   Accepted: 12596 Des ...

  7. JqueryEasyUI 解决IE下datagrid无法刷新的问题

    问题描述: 在使用JqueryEasyUI 时,发现在IE下$('#table').datagrid('reload');无效,数据并没有被刷新,究其原因,是因为刷新时,datagrid请求的url没 ...

  8. 实现的时钟(time)

    <!doctype html><html lang="en"><head> <script type="text/javascr ...

  9. 测可用!ecshop立即购买和加入购物车按钮共存的方法

    网上方法很多,但都不能用的,有的是老版本的,有的方法本身就不完整. 应大多数客户要求,我们重新整理下教程,希望对大家有用. 亲测可用!ecshop立即购买和加入购物车按钮共存的方法 第一步:修改ecs ...

  10. OS开发(Objective-C)常用库索引

    code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 cocoacontrols.com/ 英文版本的lib收集 objclibs.com/ 精品lib的收集网站 h ...