zoukankan      html  css  js  c++  java
  • IO流标-准输出流PrintWriter 和 InputStream字节输入流 , 字符高级流OutputStreamWriter 和 InputStreamReade

    1. 标准输入流

    1)InputStream字节输入流 ,源数据源是标准输入设备(键盘、鼠标、触摸屏)等输入设备,InputStream和Reader是所有输入流的抽象基类。

     1 import java.io.IOException;
     2 import java.io.InputStream;
     3 
     4 public class Test1 {
     5     public static void main(String[] args) throws IOException {
     6         // 输入一句话,然后原样输出
     7 
     8         InputStream in = System.in;
     9 
    10         System.out.println("请输入一句话:");
    11 
    12         byte[] buf = new byte[1024];// 一次读取1024个字节
    13         int len;
    14         // buf中包含回车和换行
    15         len = in.read(buf);
    16         String str = new String(buf, 0, len);
    17 
    18         System.out.println("你输入的是:" + str);
    19 
    20     }
    21 
    22 }

    2)根据上一篇文章:IO流  的那首诗。从控制台输入这首诗,然后把这首诗写入文件里。

     1 import java.io.BufferedReader;
     2 import java.io.BufferedWriter;
     3 import java.io.File;
     4 import java.io.FileWriter;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.InputStreamReader;
     8 import java.io.UnsupportedEncodingException;
     9 
    10 public class Test2 {
    11     public static void main(String[] args) throws IOException, UnsupportedEncodingException {
    12 
    13         InputStream in = System.in;
    14         InputStreamReader reader = new InputStreamReader(in, "GBK");
    15         BufferedReader br = new BufferedReader(reader);
    16 
    17         File file = new File("D:" + File.separator + "javatest" + File.separator + "f.txt");
    18 
    19         FileWriter writer = new FileWriter(file);
    20         BufferedWriter beWriter = new BufferedWriter(writer);
    21         String endString = "bye";// 定义一个输入这首诗结束时的标志;
    22         System.out.println("请输入一首诗:");
    23         while (true) {
    24             String line = br.readLine();
    25             if (line.equals(endString)) {
    26                 break;
    27             }
    28             
    29             beWriter.write(line);//输入一行
    30             beWriter.newLine();//换行和回车
    31         }
    32         
    33         beWriter.flush();//刷新缓冲区
    34         //关闭通道
    35         beWriter.close();
    36         writer.close();
    37     
    38     }
    39 }

    2. 标准输出流(PrintStream)

    1)读取以上目录文件,显示到标准输出设备;

     1 import java.io.BufferedReader;
     2 import java.io.File;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileReader;
     5 import java.io.IOException;
     6 import java.io.PrintStream;
     7 
     8 public class Test1 {
     9     public static void main(String[] args) throws FileNotFoundException, IOException {
    10         
    11         File file = new File("D:"+File.separator+"javatest"+File.separator+"f.txt");
    12         //创建管道
    13         FileReader fReader = new FileReader(file);
    14         BufferedReader bReader = new BufferedReader(fReader);
    15         
    16         PrintStream pStream = System.out;
    17         
    18         String line;//定义一个字符串类型
    19         
    20         while ((line = bReader.readLine()) != null) {//循环读取文件中每一行,直到为空,就结束。
    21             pStream.println(line);
    22         }
    23         //关闭通道
    24         bReader.close();
    25         fReader.close();
    26     }
    27 
    28 }

     3.字符打印流 PrinWriter

    用PrintWriter类对象的write()、print()、append()方法实现写入字符到文件,看看结果是否相同?

     1 import java.io.BufferedReader;
     2 import java.io.File;
     3 import java.io.FileInputStream;
     4 import java.io.FileNotFoundException;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.InputStreamReader;
     8 import java.io.PrintWriter;
     9 
    10 
    11 public class Test1 {
    12     public static void main(String[] args) throws FileNotFoundException, IOException {
    13         File file = new File("D:"+File.separator+"javatest"+File.separator+"g.txt");
    14         
    15         PrintWriter pWriter = new PrintWriter(file);
    16         
    17         pWriter.append("Hello");
    18         pWriter.write("这个");
    19         pWriter.println("你好");
    20         
    21         pWriter.close();
    22         
    23         //将PrintlWriter字符打印流 写文件的信息,输出到控制 台
    24         InputStream iStream = new FileInputStream("D:"+File.separator+"javatest"+File.separator+"g.txt");
    25         InputStreamReader iStreamReader = new InputStreamReader(iStream);
    26         BufferedReader bReader = new BufferedReader(iStreamReader);
    27         
    28         String line;
    29         /*line = bReader.readLine();*/ //写到while循环里
    30         while ((line = bReader.readLine()) != null) {
    31             
    32             System.out.println(line);
    33             //line = bReader.readLine();
    34         }
    35         
    36         bReader.close();
    37         iStream.close();
    38         iStreamReader.close();
    39     }
    40 
    41 }

    输出

    Hello这个你好

    返回结果是相同的,PrinWriter的更多用法,可通过查询JDK API文档

    4.Scanner(一般不用,了解即可)

     通过Scanner扫描文件、字节流等等。

     1 import java.io.File;
     2 import java.io.FileNotFoundException;
     3 import java.util.Scanner;
     4 
     5 public class Test1 {
     6     public static void main(String[] args) throws FileNotFoundException {
     7         //通过Scanner扫描文件、字节流等
     8         File file = new File("d:"+File.separator+"javatest"+File.separator+"c.txt");
     9         
    10         Scanner sc = new Scanner(file);
    11         
    12         String line;
    13         while (sc.hasNextLine()) {
    14             line = sc.nextLine();
    15             System.out.println(line);
    16         }
    17         
    18         sc.close();
    19     }
    20 }

     或者通过扫描指定编码文件

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.util.Scanner;
     5 
     6 public class Test1 {
     7     public static void main(String[] args) throws FileNotFoundException {
     8         //通过Scanner扫描文件、字节流等
     9         /*File file = new File("d:"+File.separator+"javatest"+File.separator+"c.txt");
    10         
    11         Scanner sc = new Scanner(file);*/
    12         Scanner sc = new Scanner(new FileInputStream(new File("d:"+File.separator+"javatest"+File.separator+"c.txt")));
    13         String line;
    14         while (sc.hasNextLine()) {
    15             line = sc.nextLine();
    16             System.out.println(line);
    17         }
    18         
    19         sc.close();
    20     }
    21 }
  • 相关阅读:
    HTML初识
    使用python操作Memcache、Redis、RabbitMQ、
    使用salt-cloud创建虚拟机
    运维堡垒机----Gateone
    ELK日志分析系统
    Python MySQL API
    浅谈Java中static作用--转
    oracle如何设置最大连接数
    转--oracle查看允许的最大连接数和当前连接数等信息
    oracle 查看未关闭连接
  • 原文地址:https://www.cnblogs.com/abcdjava/p/10828424.html
Copyright © 2011-2022 走看看