package cn.io.bytes;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ByteTest {
public static void main(String[] args) {
//写入文件
//创建文件
FileWriter fw =null;
try
{
//在里面定义的东西。外面是访问不到的
fw = new FileWriter("IoTest.java");
//将字符写入字节流中
fw.write("io流1s5af4ef5sa4221 31s2fe3a51f2asd31f5ea1f23ads1f321212 af123s1fwe23f123啊身份213符123符231符123 f");
}
catch (IOException e){
System.out.println("to"+e.toString());
}
finally{
try
{
if(fw!=null)
//colse 刷新后关闭流
fw.close();
}
catch (IOException e)
{
System.out.println("to"+e.toString());
}
}
//读取文件
FileReader fl =null;
try
{
fl = new FileReader("IoTest.java");
//读取字符
char[] chs = new char[1024];
int num = 0;
while((num = fl.read(chs))!=-1){
System.out.println(new String(chs,0,num));
if(num%5==0){
System.out.println();
}
}
}
catch (IOException e)
{
System.out.println(e);
}
finally{
try
{
if(fl != null)
fl.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
}
|
|