1.
//字符流缓冲区 写入 BudderedWriter
import java.io.*;
class BufferedWriterDemo
{
public static void main(String[] args) throws IOException
{
FileWriter fw = new FileWriter("demo.txt");
BufferedWriter bw = new BufferedWriter(fw);
for (int x= 1;x < 5 ;x++ )
{
fw.write("123"+x);
bw.newLine();
bw.flush();
}
bw.close();
}
}
2.
/*
字节读取流缓冲区 BufferedReader
*/
import java.io.*;
class BufferedReaderDemo
{
public static void main(String[] args)
{
FileReader fr = null;
BufferedReader bufr = null;
try
{
fr = new FileReader("demo.txt");
bufr = new BufferedReader(fr);
String line = null;
while ((line=bufr.readLine())!=null)
{
System.out.println(line);
}
}
catch (IOException e)
{
throw new RuntimeException("读取失败");
}
//bufr.close();
finally
{
try
{
if(bufr!=null)
bufr.close();
}
catch (IOException e)
{
throw new RuntimeException("失败");
}
}
}
}
|
|