package IOliu.ByteIO;
import java.io.*;
/**
* 标准的输入输出流
* public static final InputStream in:字节输入流,用来读取键盘录入的数据
* InputStream is=System.in;
* Scanner sc=new Scanner(System.in);
*
* public static final PrintStream out:字节输入流,将数据输出到命令行
* OutputStream is=System.out;
* System.out.println();
*
* 案例1 :
* 需求:读取java文件 并输入到命令行
* 数据源:java文件 BufferedReader
* 目的地:命令行 System.out
*
*
*
* */
public class InputOnput {
public static void main(String[] args) throws IOException {
// 创建输入流对象
// method1();
//由于OutputStream 只能输入输出字节或者字节数组需要转换,
//而我们读取到的数据是字符串,如果还需要输出需要字节数组
// 我们要通过标准输出输入流输入字符串,把标准输出流转换成一种字符输出流即可,
// OutputStreamWriter()
//OutputStreamWriter(OutputStream out) 转换流 将字节流转换成字符流
// method2();
//我们在这想用高效输出流
method3();
}
public static void method3() throws IOException {
BufferedReader br=new BufferedReader
(new FileReader("F:\\untitledxuexi\\src\\IOliu\\ByteIO\\InputOnput.java"));
// OutputStream os=System.out;
// Writer osw=new OutputStreamWriter(os);
// BufferedWriter bw=new BufferedWriter(osw);
//还可以使用匿名对象来创建高效输入流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
// int len;
// char[] chs=new char[1024];
String chs;
while ((chs=br.readLine())!=null){
bw.write(chs);
bw.newLine();
}
br.close();
bw.close();
}
//方法1 可以通过文件输入到命令行 不足的是OutputStream 只能输入输出字节需要转换
public static void method2() throws IOException {
BufferedReader br=new BufferedReader
(new FileReader("F:\\untitledxuexi\\src\\IOliu\\ByteIO\\InputOnput.java"));
OutputStream os=System.out;
Writer osw=new OutputStreamWriter(os);
// int len;
// char[] chs=new char[1024];
String chs;
while ((chs=br.readLine())!=null){
osw.write(chs);
osw.write("\r\n");
}
br.close();
osw.close();
}
public static void method1() throws IOException {
BufferedReader br=new BufferedReader
(new FileReader("F:\\untitledxuexi\\src\\IOliu\\ByteIO\\InputOnput.java"));
OutputStream os=System.out;
// int len;
// char[] chs=new char[1024];
String chs;
while ((chs=br.readLine())!=null){
os.write(chs.getBytes());
os.write("\r\n".getBytes());
}
br.close();
os.close();
}
}
删除线格式
package IOliu.ByteIO;
import java.io.*;
// * 案例1 :
// * 需求:读取键盘录入的东西 并输入到fr.txt
// * 数据源:键盘 System.in
// * 目的地:fr.txt BufferedWriter
public class OnputInput {
public static void main(String[] args) throws IOException {
InputStream is=System.in;
// InputStreamReader isr=new InputStreamReader(is);
Reader r=new InputStreamReader(is);
// BufferedReader br=new BufferedReader(r);
FileWriter fw=new FileWriter("F:\\untitledxuexi\\fr.txt");
char[] bys=new char[1024];
int len; //用于存储读取到的字符个数
while ((len=r.read(bys))!=-1){
fw.write(new String(bys,0,len));
fw.write("\r\n");
fw.flush();
}
is.close();
r.close();
fw.close();
// method1();
}
public static void method1() throws IOException {
InputStream is=System.in;
FileWriter bw=new FileWriter("F:/untitledxuexi/fr.txt");
byte[] bys=new byte[1024];
int len; //用于存储读取到的字节个数
while ((len=is.read(bys))!=-1){
bw.write(new String(bys,0,len));
bw.flush();
}
is.close();
bw.close();
}
}