天天看点

处理流_java

1.标准输入输出流

2.打印流

3.数据流

文章目录

  • ​​1.标准输入输出流​​
  • ​​2.打印流​​
  • ​​2.1 打印流_格式化输出​​
  • ​​3.数据流​​

1.标准输入输出流

public class demo06 {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            br = new BufferedReader(isr);
            while (true){
                String data = br.readLine();
                if("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
                    System.out.println("结束");
                    break;
                }
                String s = data.toUpperCase();
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br !=null){

                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
      
处理流_java

标准输入输出流:

1.可以用scanner

2.可以用System.in方法

System.in - 转换流 - BufferedReader的ReadLine

2.打印流

打印流只可以输出没有输入

处理流_java
import java.io.* ;
public class PrintDemo01{
    public static void main(String arg[]) throws Exception{
        PrintStream ps = null ;        // 声明打印流对象
        // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
        ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
        ps.print("hello ") ;
        ps.println("world!!!") ;
        ps.print("1 + 1 = " + 2) ;
        ps.close() ;
    }
};
      

执行结果:

处理流_java
2.1 打印流_格式化输出
处理流_java
package 类集;
import java.io.* ;
public class PrintDemo01{
    public static void main(String arg[]) throws Exception{
        PrintStream ps = null ;        // 声明打印流对象
        // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
        ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
        String name = "李兴华" ;    // 定义字符串
        int age = 30 ;                // 定义整数
        float score = 990.356f ;    // 定义小数
        char sex = 'M' ;            // 定义字符
        ps.printf("姓名:%s;年龄:%d;成绩:%f;性别:%c",name,age,score,sex) ;
        ps.close() ;
    }
};
      
import java.io.* ;
public class PrintDemo03{
    public static void main(String arg[]) throws Exception{
        PrintStream ps = null ;        // 声明打印流对象
        // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
        ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
        String name = "李兴华" ;    // 定义字符串
        int age = 30 ;                // 定义整数
        float score = 990.356f ;    // 定义小数
        char sex = 'M' ;            // 定义字符
        ps.printf("姓名:%s;年龄:%s;成绩:%s;性别:%s",name,age,score,sex) ;
        ps.close() ;
    }
};
      

3.数据流

import java.io.*;

public class demo08 {
    @Test
    public void test1() throws IOException {
        //打印流 输出保存在一个文件中
        //数据流
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("11.txt"));
        dos.writeUTF("你");
        dos.flush();
        dos.writeInt(12);
        dos.flush();
        dos.writeBoolean(true);
        dos.flush();
        dos.close();
    }
    @Test
    //读的顺序和写的顺序一致

    public void test2() throws IOException {
        DataInputStream dis = new DataInputStream(new FileInputStream("11.txt"));
        String name = dis.readUTF();
        int i1 = dis.readInt();
        boolean b1 = dis.readBoolean();
        System.out.println(name);
        System.out.println(i1);
        System.out.println(b1);
        dis.close();
    }
}