字符流
FileReader
FileWriter
BufferedReader
BufferedWriter
字节流
FileInputStream
FileOutputStream
BufferedInputStream
BufferedOutputStream
读取键盘录入
System.out:对应的是标准输出设备:控制台。
System.in:对应的标准输入设备:键盘。
需求:
通过键盘录入数据。
当录入一行数据后,就将该行数据进行打印。
如果录入的数据是over,那么停止录入。
import java.io.IOException;
import java.io.InputStream;
public class ReadIn {
public static void main(String[] args) throws IOException{
InputStream in = System.in;
int by = in.read();
int by1 = in.read();
int by2 = in.read();
System.out.println(by);
System.out.println(by1);
System.out.println(by2);
}
}
//输出
abc
97
98
99
import java.io.IOException;
import java.io.InputStream;
public class ReadIn {
public static void main(String[] args) throws IOException{
InputStream in = System.in;
System.out.println('\r'+0);
System.out.println('\n'+0);
}
}
//输出
13
10
0.gif
通过刚才的键盘录入一行数据并打印其大写,发现其实就是读一行数据的原理。也就是readLine方法。能不能直接使用readLine方法来完成键盘录入的一行数据的读取呢?
readLine方法是字符流BufferedReader类中的方法。而键盘录入的read方法是字节流InputStream的方法,那么能不能将字节流转成字符流在使用字符流缓冲区的readLine方法呢?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TransStreamDemo {
public static void main(String[] args) throws IOException
{
//获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,使用转换流。InputStreamReader
InputStreamReader isr = new InputStreamReader(in);
//为了提高效率,将字符创进行缓冲区技术高效操作。使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
String line = null;
while ((line = bufr.readLine())!=null)
{
if ("over".equals(line))
break;
System.out.println(line.toUpperCase());
}
bufr.close();
}
}
0.gif
import java.io.*;
public class TransStreamDemo0 {
public static void main(String[] args) throws IOException
{
//获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,使用转换流。InputStreamReader
InputStreamReader isr = new InputStreamReader(in);
//为了提高效率,将字符创进行缓冲区技术高效操作。使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
OutputStream out = System.out;
OutputStreamWriter osw = new OutputStreamWriter(out);
String line = null;
while ((line = bufr.readLine())!=null)
{
if ("over".equals(line))
break;
osw.write(line.toUpperCase()+"\r\n");
osw.flush();
}
bufr.close();
}
}
0.gif
import java.io.*;
public class TransStreamDemo0 {
public static void main(String[] args) throws IOException
{
//获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,使用转换流。InputStreamReader
InputStreamReader isr = new InputStreamReader(in);
//为了提高效率,将字符创进行缓冲区技术高效操作。使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
OutputStream out = System.out;
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bufw = new BufferedWriter(osw);
String line = null;
while ((line = bufr.readLine())!=null)
{
if ("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
}
0.gif
以上代码可以简写为
import java.io.*;
public class TransStreamDemo0 {
public static void main(String[] args) throws IOException
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = null;
while ((line = bufr.readLine())!=null)
{
if ("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
}
网友评论