[ Java ] Sử dụng InputStream / OutputStream

Sử dụng luồng vào ra InputStream và OutputStream trong Java

/* ----------------------------------------------------------------------------------------
Ví dụ 1: InputStream - System.in;
- Nhập vào từng ký tự từ bàn phím, In ký tự vừa nhập, kết thúc khi nhập phím 'q'

*/
package inputstream1;
import java.io.*;
public class InputStream1 {
public static void main(String args[])
{
InputStream is = System.in;
while (true)
{
try
{
int ch = is.read(); // doc 1 ky tu tu ban phim
if (ch ==-1 || ch =='q')
break;
System.out.print((char)ch);

} catch (IOException ie)
{
System.out.print("Error: "+ie);
}
}


}
}

/*----------------------------------------------------------------------------------------------
Ví dụ 2: InputStream - System.in
Nhập vào từ bàn phú dãy ký tự lưu vào mảng b, in mảng b ra màn hình

*/


package inputstream2;
import java.io.*;
public class InputStream2
{
public static void main(String args[])
{
InputStream is = System.in; // KeyBoard = System.in

while (true)
{
try
{
int num = 10;//is.available();
if (num > 0)
{
byte[] b = new byte[num];
int result = is.read(b);
if (result == -1) break;
String s = new String(b);
System.out.print(s);
} else
{
System.out.print('.');
}
} catch (IOException ie)
{
System.out.print("Error: "+ie);
}
}
}
}

/* -----------------------------------------------------------------------------------------
Ví dụ 3: OutputStream - System.out
In ra màn hình xâu: "Day la vi du OutputStream voi System.out (man hinh) "

*/

ackage outputstream1;

import java.io.*;
public class OutputStream1
{
public static void main(String args[])
{
OutputStream os = System.out;

// Monitor = System.out
try
{
String str = "Day la vi du OutputStream voi System.out (man hinh) \n";
byte b[] = str.getBytes(); // Đổi chuỗi thành mảng các bytes
os.write(b);
} catch (IOException ie)
{
System.out.print("Error: "+ie);
}
}
}


/*----------------------------------------------------------------------------------------------
Ví dụ 4: - InputStream - InputStreammReader - BufferedReader - readLine()
- Nhập vào 2 số nguyên a, b
- Tính tổng 2 số, in kết quả ra màn hình
*/

package inputstreamreadline;
import java.io.*;
public class InputStreamReadLine{
public static void main(String args[])
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

try{
System.out.print("\n Nhap a:");
String xau1=br.readLine();
int a=Integer.valueOf(xau1);// ép kiểu string -> int

System.out.print("\n Nhap b:");
String xau2=br.readLine();
int b=Integer.valueOf(xau2);

int s=a+b;
String str=String.valueOf(s);// ép kiểu int -> string

// in ra nam hinh
OutputStream os = System.out;
PrintWriter xuat = new PrintWriter(os);

xuat.write("Ket qua= ");
xuat.print(str);
xuat.flush();

// System.out.print("\n Tong= "+str);

}catch(IOException e){
System.out.print("Loi "+e.toString());
}

}
}

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Tài liệu: Lập trình hướng đối tượng JAVA core dành cho người mới bắt đầu học lập trình [Click để xem]

# Khoá học online: Lập trình Java trong 4 tuần [Click để xem]

Tìm kiếm nội dung khác: