字符流的缓冲区
·缓冲区的出现提高了对数据的读写效率。对应类
BufferedWriter
BufferedReader
缓冲区要结合流才可以使用。
在流的基础上对流的功能进行了增强。
缓冲区的出现是为了提高流的操作效率而出现的。
所以在创建缓冲区之前,必须要先有流对象。
该缓冲区中提供了一个跨平台的换行符。
newLine();
class BufferedWriterDemo
{
public static void main(String[] args) throws IOException
{
//创建一个字符写入流对象。
FileWriterfw = new FileWriter ("buf. txt");
//为了提高字符写入流效率。加入了缓冲技术。
//只要将需要被提高效率的流对象作为参数传递给缓冲区的构造函数即可。
BufferedWriter buff = new BufferedWriter (fw);
for (int x = 1; x < 5; x++)
{
bufw.write("abcd" + x);
buffw.newLine();
buff.flush();
}
//记住,只要用到缓冲区,就要记得刷新。
//bufw. flush();
//其实关闭缓冲区,就是在关闭缓冲区中的流对象。buyw.close();
}
}
字符读取流缓冲区:
该缓冲区提供了一个一次读一行的方法readline,方便于对文本数据的获取.当返回null时,表示读到文件末尾。
import java.io.*;
class BufferedReaderDemo
{
public static void main(String[]args) throws IOException
{
//创建一个读取流对象和文件相关联。
FileReader fr = new FileReader ("buf. txt");
//为了提高效率,加入缓冲技术。将字符读取流对象作为参数传递给缓冲对象的构造函数。 BufferedReader buff = new BufferedReader (fr);
String line = null;
while ((line = buff.readLine()) != null)
{
system, out, printin(line);
}
buff, close();
}
}
装饰设计模式:
当想要对己有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能,并提供加强功能。
那么自定义的该类称为装饰类。
装饰类通常会通过构造方法接收被装饰的对象。
并基于被装饰的对象的功能,提供更强的功能。
class Person
{
public void chifan()
{
System.out.println("吃饭");
}
}
class SuperPerson
{
private Person p;
SuperPerson(Person p)
{
this up = pi
}
public void superChifan()
{
system, out, printin("开胃酒");
p.chifan();
system.out.printin("甜点");
system.out.printin("来一根");
}
class PersonDemo
{
public static void main(String[]args)
{
Person p = now Person();
//p. chifan();
SuperPerson sp≡now SuperPerson (p);
sp.superchifan \();
}
}
字符流:
FileReader
FileWriter 。
BufferedReader
BufferedWriter
字节流:
InputStreamOutputStream
需求,想要操作图片数据。这时就要用到字节流。
import Java.io.*;
class FileStream
{
public static void main(String[] args) throws IOException
{
readFile_2();
}
public static void read File _3() throws IOException
{
FileInputStream fis = new FileInputStream ("fos. txt");
//int num = fis.available(); bytel]buf = new byte[fis.available()];//定义一个刚刚好的缓冲区。不用在循环了。fis. read(buff);
system.out.println(new String(buf));
fis.close();
}
public static void readFile_2() throws IOException
{
FileInputStream fis = new FileInputStream ("fos. txt");
byte[]buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1)
{
System.out.println(new String(buff, 0, len));
}
fis.close();
}
}
读取键盘录入。
system. out:对应的是标准输出设备,控制台。
System. in:对应的标准输入设备:键盘。
import java.io.*;
class ReadIn
{
public static void main(String[] args) throws IOException
{
InputStream in = System.in;
System.out.println('\=' + 0;
system.out.println('\n' + 0);
//int by = in.read();
//intby1 = in.read();
//intby3≡in.read();
//System.out.println(by);
//System.out.println(by1);
//System.out, println(by3);*/
}
}
需求:
通过键盘录入数据。
当录入一行数据后,就将该行数据进行打印。
如果录入的数据是over,那么停止录入,
import java.io.*;
class ReadIn
{
public static void main(String[]args) throws IOException
{
Inputstream in = system.in;
StringBuilder sb = new StringBuilder ();
while(true)
{
int ch = in.read();
if (ch == '\r')
continue;
if (ch == '\n')
{
String s = sb.toString();
if ("over".equals(s))
break;
System.out.println(s.toUpperCase ());
sb& delete(0, sb.length());
}
else
sb.append((char)ch);
}
}
}