1.硬盘和内存中的数据读写,1个字符=2个字节,1个字节=8个二进制位;
输入流 输出流
字节流 InputStream OutputStream
字符流 Reader Writer
2.字节流:可以读取任意文件;
1>字节输出流:<1>OutputStream:表示所有字节输出流的类的父类;
<2>FileOutputStream:把内存中的数据写入到硬盘文件中;
//String name 目的地是文件的路径/ File file 目的地是文件
<3>写入数据的原理:内存->硬盘
java程序-->JVM(java虚拟机)-->OS(操作系统)-->OS调用写数据的方法-->把数据写入到文件
<4>步骤:FileOutputStream fos = new FileOutputStream("balbal\\a.txt");
//创建一个FileOutputStream对象
fos.write(97); //调用FileOutputStream方法,把数据写入文件中;
fos.close(); //释放资源
<5>字节输出流写多个字节:/public void write(byte[] b) //将b.length字节从指定的字节数组写入输出流
若写入的第一个字节是正数0-127,显示的时候会查询ASCII表;
若第一个字节是负数,第一和第二个字节查询GBK表,组成中文显示;
/public void write(byte[] b,int off,int len) //把字节数组的一部分写入文件
int off:数组的开始索引;
int len:写几个字节;
/byte[] bytes2 = "balala".getBytes();
System.out.println(Arrays.toString(bytes2));
//使用String类中的方法把字符串,转换为字节数组;
<6>追加写/续写:boolean append追加写开关;
true创建对象不覆盖源文件,在末尾追加数据,false创建新文件,覆盖原文件;
换行:写换行符号,不同操作系统不同,Mac:/r;//fos.write("/r",getBytes());
2>字节输入流:<1>FileInputStream:把硬盘中的数据,读取到内存中;
<2>原理:硬盘-->内存-->JVM-->OS-->OS读取数据的方法-->读取文件
<3>步骤:一次读取一个字节:
FileInputStream fis = new FileInputStream("balbal\\a.txt");
//创建一个FileInputStream对象
int len = 0; //记录读取到的字节
while(len = fis.read()!=1){
System.out.println(len);
} //读取文件中的一个字节并返回,到文件的末尾返回-1;
fis.close(); //释放资源
一次读取多个字节:用byte[]数组读取多个数组,有缓冲作用,int的返回值是每次读区有效字节个数;
<4>读取多个字节优化:byte[] bytes = new byte[1024];
int len = 0;
while((len =fis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
} fis.close();
<5>练习:文件复制:
3. 字符流:1>Reader类:字符输入流最顶层的父类;抽象类;
2>FileReader类:<1>概念:文件字符输入流;把硬盘文件中的数据以字符的方式读取到内存中;
<2>步骤://输入单个字符:FileReader fr = new FileReader("naa;na\\c.txt");
int len = 0;
while((len = fr.read())!= -1){
System.out.println((cahr)len);
}fr.close();
//输入多个字符:FileReader fr = new FileReader("naa;na\\c.txt");
char[] cs = new char[1024];
int len = 0;
while(len = fr.read(cs))!=-1){
System.out.println(new String(cs , 0 , len));
}fr.close();
3>Writer类:
4>FileWriter类:FileWriter fw = new FileWriter("jklank\\d.txt");
fw.write(97);
fw.close();
5>写出单个字符到文件:
6>flush方法:刷新缓冲区,流对象可以继续使用;
close方法的区别:先刷下缓冲区,再通知系统释放资源,流对象不能再使用;
7>其他方法:write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ; //每次可以写出字符数 组中的数据;
write(String str) 和 write(String str, int off, int len) ; //每次可以写出字符串中的数据;
8>续写和换行:和字节流一样;
4.try...catch...finally处理流中的异常:final FileReader fr = new FileReader("in.txt"); // 创建流对象
FileWriter fw = new FileWriter("out.txt");
try (fr; fw){
int b;
while ((b = fr.read())!=‐1) {
fw.write(b);}
}catch(IOException e){
e.printStackTrace();}
5.Properties集合:1>Properties集合存储数据:是HashMap类的子类;双列集合<k,v> k 和 v 默认是字符串;
<1>public Object setProperty(String key, String value); //保存一对属性;
<2>public String getProperty(String key);
//使用此属性列表中指定的键搜索属性值;
<3>public Set<String> stringPropertyNames(); //所有键的名称的集合;
2>遍历取出Properties集合中的数据:Set<String> strings = properties.stringPropertyNames();
3>Properties集合中的方法store:把临时数据,持久写入硬盘中储存;
void store(OutputStreeam out, String comments); // 字节
void store(Writer writer, String comments); //字符
4>Properties集合中的方法load:从字节输入流中读取键值对;
void load(InputStream inStream); //字节
void load(Reader reader); //字符