天天看點

處理流_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();
    }
}