关于IO流的常用类
1、InputStream is = System.in;
OutputStream os = System.out;
2、InputStreamReader --》字节流转换为字符流
OutputStreamWriter --》字符流转换为字节流
3、PrintStream ps = System.out
4、ByteArrayOutputStream --》 一次把字节写完存入内存
ByteArrayInputStream --》一次把字节读完
----------------------------
//接收键盘输入的字符 但不能使用Scanner
public class SystemIn {
public static void main(String[] args) {
//用InputStream 来读取
InputStream is = System.in;
OutputStream os = null;
int n = 0;
try {
os = new FileOutputStream("d:\\aa.txt");
while((n = is.read()) != 't'){
os.write(n);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
---------------------
//接收键盘输入的字符 但不能使用Scanner
public class System2 {
public static void main(String[] args) {
InputStream is = System.in;
//字节流转换成为字符流
InputStreamReader ir = new InputStreamReader(is);
//缓冲
BufferedReader br = new BufferedReader(ir);
Writer w = null;
String str;
try {
w = new FileWriter("d:\\dd.txt");
while(true){
str = br.readLine();
if("886".equals(str)){break;}
w.write(str);
w.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
w.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
-----------------------------
//把某个文件的内容读取 然后打印到控制台 但是不能用println或print
public class PrintStream1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintStream ps = System.out;
//字符流转换成为字节流
OutputStreamWriter ow = new OutputStreamWriter(ps);
BufferedWriter bw = new BufferedWriter(ow);
int s = 0;
Reader r;
try {
r = new FileReader("d:\\dd.txt");
while((s = r.read())!= -1 ){
bw.write(s);
bw.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
--------------------------------
ByteArrayOutputStream --> 把东西一次写入内存
public class ByteArrayOutputStream1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream is = null;
ByteArrayOutputStream bos = null;
OutputStream os = null;
byte[] b = new byte[1024];
int l = 0;
try {
bos = new ByteArrayOutputStream();
is = new FileInputStream("d:\\a.txt");
os = new FileOutputStream("d:\\dd.txt");
while((l = is.read(b)) != -1){
//写入内存
bos.write(b,0,l);
//从内存取数据 存到数组
byte[] byte1 = bos.toByteArray();
os.write(byte1);
//bos.close(); 关闭无效
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------------
重写LineNumberReader 函数
1
public class RewriteLineNumberRead extends BufferedReader {
public RewriteLineNumberRead(Reader r) {
super(r);
// TODO Auto-generated constructor stub
}
public RewriteLineNumberRead(Reader in, int sz) {
super(in, sz);
// TODO Auto-generated constructor stub
}
private int number = 0; //行号
public int getNumber(){
return number;
}
@Override
public String readLine() throws IOException {
// TODO Auto-generated method stub
number++;
return super.readLine();
}
}
----------
2
import java.io.*;
public class Test1 {
//调用自己重写的LineNumberReader函数 打印出行数
public static void main(String[] args) {
// PrintStream ps = System.out;
//字符流转换成字节流
// OutputStreamWriter osw = new OutputStreamWriter(ps);
//缓冲
// BufferedWriter bw = new BufferedWriter(osw);
Reader r = null;
RewriteLineNumberRead re = null;
String str;
try {
r = new FileReader("d:\\dd.txt");
re = new RewriteLineNumberRead(r);
while((str = re.readLine()) != null){
//bw.write(str);
System.out.println(re.getNumber() + " " + str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
re.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
------------------------------------------------
重写Buffered 函数
1
public class RewriterBuffered extends Reader {
public RewriterBuffered(Reader in) {
// super(in);
this.reader = in;
// TODO Auto-generated constructor stub
}
private Reader reader;
public String readLine(){
StringBuilder sb = new StringBuilder();
while(true){
try {
int ch = reader.read();
if(ch == '\r'){
continue;
}else if(ch == '\n' || ch == -1){
break;
}
sb.append((char)ch);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
}
}
----------------
2
public class TestBuff {
public static void main(String[] args) throws FileNotFoundException {
Reader r = new FileReader("d:\\a.txt");
RewriterBuffered rb = new RewriterBuffered(r);
//rb.readLine();
System.out.println(rb.readLine());
}
}