国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java從零開始學三十八(JAVA IO- 重定向IO)

2019-11-14 23:07:34
字體:
來源:轉載
供稿:網友
java從零開始學三十八(JAVA IO- 重定向IO)一、三個靜態變量

java.lang.System提供了三個靜態變量

  • System.in(默認鍵盤)
  • System.out(默認顯示器)
  • System.err
二、重寫向方法

System提供了三個重定向方法

方法說明
static void setErr(PRintStream errr)重定向標準錯誤輸出流
static void setIn(InputStream in )重定向標準輸入流
static void setOut(PrintStream out)重定向歀輸出流
三、例子3.1、重定向輸入流
package com.pb.io.reio;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;/* * 重定向輸入 * 1.有一個已經初始化的InputStream輸入流 * 2.調用System.setIn()方法,將標淮輸入流重定向到目的輸入流 * 3.讀取System.in中的內容 */public class ReIn {    public static void main(String[] args) throws UnsupportedEncodingException {                        try {            //1.聲明一個輸入流            FileInputStream fis=new FileInputStream("d:/test/s1.txt");            //2.重定向            System.setIn(fis);            //3.讀取System.in標準輸入流中的內容            BufferedReader br=new BufferedReader(new InputStreamReader(System.in,"gb2312")); //設置字符編碼            //4.輸出System.in中的內容            String line=null;            while((line=br.readLine())!=null){                System.out.println(line);            }            //5.關閉流            br.close();            fis.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}
3.2、重定向輸出流
package com.pb.io.reio;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;/* * 重定向標淮輸出流 * 1.初始化PrintStream對象 * 2.調用System.setOut()方法,將標淮輸出流重定向至PrintStream對象 * 3.操作System.out流, */public class ReOut {    public static void main(String[] args) {                try {            //1.聲明一個輸出流PrintStream對象            PrintStream ps=new PrintStream(new FileOutputStream("d:/test/ps.txt",true));   //追加內容            //2.重定向標淮輸出流            System.setOut(ps);            //3.使用PrintStream對象向流中寫信息            System.out.println("測試重定向成功了沒有!");            System.out.println(new ReOut());            ps.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        }            }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东至县| 南雄市| 库车县| 长治市| 大理市| 绥棱县| 嫩江县| 光泽县| 定结县| 九龙城区| 邹平县| 昌都县| 麻栗坡县| 平山县| 麟游县| 桂阳县| 宣化县| 汪清县| 梅河口市| 绵阳市| 鹰潭市| 湾仔区| 濮阳县| 久治县| 龙门县| 綦江县| 囊谦县| 灵台县| 朔州市| 淳安县| 广州市| 金堂县| 霍邱县| 隆德县| 修文县| 富阳市| 青岛市| 隆安县| 广汉市| 微山县| 三亚市|