在整个IO包中,流的操作就分为两种:
字节流:字节输出流OutputStream,字节输入流InputStream
字符流(一个字符等于两个字节):字符输出流Writer,字符输入流是Reader
IO操作的基本步骤
1,使用File找到一个文件
2,使用字节流和字符流的子类为OutputStream,InputStream,Writer,Reader进行实例化操作
3,进行读或写操作
4,关闭:close(),在流的操作中最终必须进行关闭
在java中"\r\n"表示换行
字节输出输入
import java.io.*;
public class OutputStreamDemo {
public static void main(String args[]) throws IOException
{
//写入数据
File file=new File("d:"+File.separator+"demo.txt");
OutputStream out=new FileOutputStream(file,true);//在文件后追加
String str="hello world";
byte b[]=str.getBytes();
out.write(b);
out.close();
//读取数据
File file1=new File("d:"+File.separator+"demo.txt");
InputStream in=new FileInputStream(file1);
byte[] by=new byte[(int)file.length()];//根据文件大小开辟字节空间
in.read(by);
System.out.println(by.toString());
}
}