JAVA io笔记10 打印流

package FileText;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Scanner;


//打印流(处理流)
//PrintStream 这是一个字节流

//三个常量
//System.in  输入流 键盘输入
//System.out 输出流 控制台输出
//System.err 输出流
//out 和 err 本质上并没有区别,只是常量和错误做区分

//重定向
//setIn() 
//setOut()
//控制台上不显示,直接进入文件了
//FileDescriptor.in //控制台 ,控制台实际上也是一个文件系统 //记住,这不是一个流!
//FileDescriptor.out

public class PritnStream10 {
public static void main(String args[]) throws IOException{
//!输出流!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    PrintStream print = System.out;
    print.println("打印");
    //作用等同于
    System.out.println("打印");
    
    //使用打印流可以直接输出到文件
    File file = new File("D:/text/dd/text2.txt");
    try {
        PrintStream print2 = System.out;
        print2 = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
        print2.flush();
        print2.println("打印我进文件");
        print2.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    
//!输入流!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    InputStream input = System.in; //键盘输入
    Scanner sc  = new Scanner(input);
    System.out.println("请输入信息");
    System.out.println(sc.nextLine());//使用nextline方式进行接收
// nextLine() 和 Line() 区别
    
//  next():
//    1、一定要读取到有效字符后才可以结束输入。
//    2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
//    3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
//    next() 不能得到带有空格的字符串。

//    nextLine():
//    1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
//    2、可以获得空白。
//    
    
    //上面三行等同于
    //Scanner sc2 = new Scanner(System.in);
    //System.out.println("请输入信息");
    //System.out.println(sc.nextLine());//使用nextline方式进行接收
    
//相对应的,我们也可以直接从文件中输入
    try {
        InputStream inputs = new BufferedInputStream(new FileInputStream(file));
        Scanner sc2 = new Scanner(inputs);
        System.out.println(sc2.nextLine());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    
    chongdingxiang();
    IN();
    
}

public static void chongdingxiang() throws FileNotFoundException{
    //重定向
        File file2 = new File("D:/text/dd/chongdingxiang.txt");
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(file2)),true));
        //PrintStream(OutputStream out,boolean autoFlush)
        //注意。true 指的是使用PringStream的自动刷新
        System.out.println("我直接去文件了!"); //控制台-》文件 
        //在这之后,所有的Ststem.out 都直接打印到文件中,而不是控制台

    //重定向回控制台
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
        System.out.println("我又回到控制台了!");
        
}

public static void IN() throws IOException{  //自己封装一个输入
    InputStream in = System.in;
    //使用FileReader是因为其中有新增方法,对一行的处理方便.但是FileReader时字符流,所以会用到转换流InputStreamReader
    BufferedReader read = new BufferedReader(new InputStreamReader(in));
    System.out.println("请输入!");
    String sss = read.readLine();
    System.out.println(sss);
}


}
 

猜你喜欢

转载自blog.csdn.net/qq_40302611/article/details/85211981