1.
字符缓冲流里面有特殊的功能(重点)
字符缓冲输出流的特殊方法public void newLine()throws IOException :写入一个行的分隔符号!
字符缓冲输入流的特殊方法:
public String readLine() throws IOException:一次读取一行:
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
public static void main(String[] args) throws IOException {
//字符缓冲输入流读数据
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String line = null;
while((line=br.readLine())!=null){
System.out.println(line);
}
//字符缓冲输出流写数据
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
for(int x =0;x<10;x++){
bw.write("hello"+x);
bw.newLine();
}
bw.close();
}
}
字符缓冲输入流:
public BufferedReader(Reader in)创建一个使用默认大小输入缓冲区的缓冲字符输入流。
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw.txt")) ;
//读数据:
//一次读取一个字符/一次读取一个字符数组
// int ch = 0 ;
// while((ch=br.read())!=-1){
// System.out.print((char)ch);
// }
char[] chs = new char[1024] ;
int len = 0 ;
while((len=br.read(chs)) !=-1){
System.out.print(new String(chs, 0, len));
}
//关闭资源
br.close();
}
}
复制文本文件:
将当前项目下的a.txt------>b.txt文件中
第一种:读写操作:
字符流一次读取一个字符数组进行读写操作
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt"));
char ch[] = new char[1024];
int len = 0;
while((len=br.read(ch))!=-1){
bw.write(ch, 0, len);
}
bw.close();
br.close();
}
}
第二种:
读写操作:
字符流一次读取一行的特有方法读写操作
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("d.txt"));
String line = null;
while((line=br.readLine())!=null){
bw.write(line);//读一行 写一行
bw.newLine();//换行
}
bw.close();
br.close();
}
2.
操作基本数据类型的流:
DataInputStream :数据输入流DataOutputStream:数据输出流
public class Demo1 {
public static void main(String[] args) throws IOException {
//创建数据输出流对象
DataOutputStream dos = new DataOutputStream(new FileOutputStream("dos.txt"));
dos.writeByte(10);//写数据
dos.writeInt(100);
dos.close();
//创建数据输入流对象
DataInputStream dis = new DataInputStream(new FileInputStream("dos.txt"));
byte b = dis.readByte();//读数据
System.out.println(b);
}
3.
打印流:
字节打印流:PrintStream字符打印流:PrintWriter
该流:不能操作数据源,只能操作目的的数据:该流只能写数据,不能读数据
该流可以自动刷新
该流还可以针对文本数据进行操作
学习的流哪些流可以针对文本数据操作
(只要构造方法中有File对象或者String类型的路径都是可以对文本进行操作的)
FileOutPutStream
FileInputStream
FilerWriter
FileReader
BufferedReader
BufferedWriter
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("pw.txt");
pw.write("hello");
pw.write("world");
pw.close();
4.
SequenceInputStream:合并流,将一些输入流合并起来,对输出流没有作用SequenceInputStream 表示其他输入流的逻辑串联
合并流:
两个文件
a.txt+b.txt---->c.txt中构造方法
public SequenceInputStream(InputStream s1,InputStream s2)
public static void main(String[] args) throws IOException {
//需求: 将当前项目下的a.txt和b.txt合并到e.txt中
//将数据源封装成合并流
InputStream is1 = new FileInputStream("a.txt");
InputStream is2 = new FileInputStream("b.txt");
//创建合并流对象
SequenceInputStream sis = new SequenceInputStream(is1, is2);
//创建字节缓冲输出流对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e.txt"));
byte bys[] = new byte[1024];
int len = 0;
while((len = sis.read(bys))!=-1){
bos.write(bys, 0, len);
}
bos.close();
sis.close();
}
}
合并流的另一种构造方式:
//public SequenceInputStream(Enumeration e)
public class Demo2 {
public static void main(String[] args) throws IOException {
//Enumeration从哪里来 是从Vector集合的一种方法返回类型 v.elements
Vector<InputStream> v = new Vector<InputStream>();
//将文件封装好加入到Vector集合中
InputStream fis1 = new FileInputStream("a.txt");
InputStream fis2 = new FileInputStream("b.txt");
InputStream fis3 = new FileInputStream("c.txt");
v.add(fis1);
v.add(fis2);
v.add(fis3);
//使用方法 返回Enumeration类型
Enumeration<InputStream> en = v.elements();
//创建合并流对象
SequenceInputStream sis = new SequenceInputStream(en);
//创建字节缓冲输出流对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f.txt"));
byte[] ch = new byte[1024];
int len = 0;
while((len=sis.read(ch))!=-1){
bos.write(ch);
}
sis.close();
bos.close();
}
}
5.
序列化流:将对象按照流的形式(在网络进行传输等等)封装成流数据:对象--->流数据:ObjectOutputStream
反序列化流:将网络传输中的流数据又封装成了一个对象: 流数据-->对象 ObjectInputStreampublic class ObjectStreamDemo {
public static void main(String[] args) throws Exception {
// write();
read();
}
//反序列化
private static void read() throws Exception {
//创建反序列化流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oot.txt")) ;
//public final Object readObject()
Object obj = ois.readObject() ;
ois.close();
System.out.println(obj);
}
//序列化
private static void write() throws Exception {
//创建序列化流对象
ObjectOutputStream oot = new ObjectOutputStream(
new FileOutputStream("oot.txt")) ;
//写数据:
//序列化流:是对对象进行操作
Person p = new Person("高圆圆", 27) ;
oot.writeObject(p);
oot.close();
}
}
6.
Properties :属性集合类(用作txt格式的配置文件)
这个类是Hashtbale的子类,而Hashtable也Map下面的双列集合
put()方法,添加元素
注意:Properties:不带泛型!
Properties里面的特殊功能:
public Object setProperty(String key,String value):和添加相关的:
public Set<String> stringPropertyNames():获取所有的键的集合
public String getProperty(String key):获取指定的属性集合中的键的值
public class PropertiesDemo2 {
public static void main(String[] args) {
//创建属性集合类对象
Properties prop = new Properties() ;
//添加元素
prop.setProperty("张三", "27") ;
prop.setProperty("李四", "25") ;
prop.setProperty("王五", "22") ;
//public Set<String> stringPropertyNames():获取所有的键的集合
Set<String> set = prop.stringPropertyNames() ;
for(String key : set){
// public String getProperty(String key):获取指定的属性集合中的键的值
String value = prop.getProperty(key) ;
System.out.println(key+"---"+value);
}
}
}
public void load(Reader reader):将文本文件的数据读取集合中
public void store(Writer writer, String comments):将集合中的数据保存到文本文件中
comments:属性列表的描述
//将集合中的数据保存到文本文件中
public static void main(String[] args) throws IOException {
//创建属性集合类对象
Properties p =new Properties();
p.setProperty("1", "11");
p.setProperty("2","22");
FileWriter w = new FileWriter("name.txt");
p.store(w, "names");//public void store(Writer writer, String comments):将集合中的数据保存到文本文件中
w.close();
//将文本文件中的数据读取到集合中
Properties pr = new Properties();
// public void load(Reader reader):将文本文件的数据读取集合中
pr.load(new FileReader("name.txt"));
System.out.println(pr);
}
}
例子
知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100”
1)先将文件中的数据加载到集合中
2)获取键的集合,遍历键的集合
判断:如果"lisi"就是里面的key,
是的话就修改
3)重新将集合中的数据保持到文件中
public class PropertiesTest {
public static void main(String[] args) throws IOException {
//将文件中的数据加载到集合中
Properties prop = new Properties() ;
Reader r = new FileReader("user.txt") ;
prop.load(r);
//释放资源
r.close();
//获取所有的键的集合
Set<String> set = prop.stringPropertyNames() ;
for(String key: set){
//获取到每一个键:判断
if("lisi".equals(key)){
//修改值
prop.setProperty(key, "100") ;
break ;
}
}
//重新保存
Writer w = new FileWriter("user.txt") ;
prop.store(w, null);
//释放资源
w.close();
}
}