流的一点用法
2013-12-12 11:40
190 查看
一、在new到某文件输出流(不管是字符输出流还是字节输出流)的时候,如果没有没有添加追加属性,那么此输出流就会将文件中
的数据冲掉。
案例:
文件中的内容为:
输出结果为:
1、不管红色代码1和红色代码2是否换行,结果都是一样的。
2、当红色代码2和红色代码3换次序的时候,输出结果是:
其原因是:load的时候,相当于将文件中的数据存储到内存中了,有点BufferedReader中缓冲区的意思,用Properties对象
getProperties的时候,是用内存中的数据,而并非用文件中的数据,所以load方法的功能是将数据进行缓存到内存中。
的数据冲掉。
案例:
public class FileWriteDemon { public static void main(String[] args) { fileToList(); } public static void fileToList() { Properties p = new Properties(); File countFile = new File("connt.ini"); if (!countFile.exists()) { try { countFile.createNewFile();// 判断如果文件为空就创建 } catch (IOException e) { e.printStackTrace(); } } FileOutputStream pw = null; FileReader br = null; try { 1、br = new FileReader(countFile); 2、pw = new FileOutputStream(countFile);// 3、System.out.println(br.read()); System.out.println(br.read()); System.out.println(br.read()); } catch (IOException e) { e.printStackTrace(); } finally { if (pw != null) { // pw.close(); } if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } connt.ini文件中的内容是: aaaaaaaaaaaaaa 输出结果是: 文件中什么都没有了。 -1 -1 -1 1、这时候不管红色代码1和红色代码2是否交换次序,输出结果都是一样的,文件中什么都没有了。 2、如果给输出流添加上追加属性,那么就不会冲掉文件中的数据了。 3、即使将红色代码3System.out.println(br.read());放在了1和2两行红代码之间,其他的两个System.out.println(br.read());还是为-1,因 为读出一个字节后,一new输出流,就会将文件中数据冲掉了,后面的连个System.out.println(br.read());就读取不到了。 二、当输入流换成缓冲流的时候,情况有不一样了,因为他的缓冲功能导致结果与上述不同。 public class FileWriteDemon2 { public static void main(String[] args) { fileToList(); } public static void fileToList() { Properties p = new Properties(); File countFile = new File("connt.ini"); if (!countFile.exists()) { try { countFile.createNewFile();// 判断如果文件为空就创建 } catch (IOException e) { e.printStackTrace(); } } PrintWriter pw = null; BufferedReader br = null; try { 1、br = new BufferedReader(new FileReader(countFile)); 2、pw = new PrintWriter(countFile);// 3、System.out.println(br.readLine()); System.out.println(br.readLine()); System.out.println(br.readLine()); } catch (IOException e) { e.printStackTrace(); } finally { if (pw != null) { // pw.close(); } if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } connt.ini文件中的内容是: a aa aaa 输出结果是: null null null 1、不管将红色代码1和红色代码2是否换次序,输出结果都是一样的:null,文件中什么都没有了。 2、将红色代码2和红色代码3换次序,输出结果是 a aa aaa 而文件中什么都没有了。 原因何在:因为缓冲流是在第一次读取数据的时候才将数据缓冲到其内置的缓冲区中(缓冲区在BufferedReader的构造方法中只是 一个参数,通过它可以设置缓冲区的大小),缓冲区本质是内存的一部分而已。以后再创建PrinterWriter的时候,只是冲掉了硬盘上 的文件而已,所以文件中的数据什么都没有了,并没有将内存中的数据冲掉;而以后用readLine方法读取的时候,是从缓冲区里面 读取数据,与文件没有关系了。 三、Properties.load方法 public class FileWriteDemon2 { public static void main(String[] args) { fileToList(); } public static void fileToList() { Properties p = new Properties(); File countFile = new File("connt.ini"); if (!countFile.exists()) { try { countFile.createNewFile();// 判断如果文件为空就创建 } catch (IOException e) { e.printStackTrace(); } } PrintWriter pw = null; BufferedReader br = null; try { 1、br = new BufferedReader(new FileReader(countFile)); 2、pw = new PrintWriter(countFile);// 3、p.load(br);// 这里,因为countFile是同一个对象,这里没有线 int count = 0; String time = p.getProperty("countkey"); System.out.println(time); if (time != null) { count = Integer.parseInt(time); if (count >= 3) { System.out.println("你不能再使用此程序"); return; } } count++; p.setProperty("countkey", count + ""); p.store(pw, "111");// // 将properties写入输出流中(保存的文件为properties文件格式) // 后面的字符串是记录的文件中进行标记 } catch (IOException e) { e.printStackTrace(); } finally { if (pw != null) { // pw.close(); } if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
文件中的内容为:
#111 #Thu Dec 12 11:36:36 CST 2013 countkey=1
输出结果为:
null
1、不管红色代码1和红色代码2是否换行,结果都是一样的。
2、当红色代码2和红色代码3换次序的时候,输出结果是:
1
其原因是:load的时候,相当于将文件中的数据存储到内存中了,有点BufferedReader中缓冲区的意思,用Properties对象
getProperties的时候,是用内存中的数据,而并非用文件中的数据,所以load方法的功能是将数据进行缓存到内存中。
相关文章推荐
- Android关于selector.xml用法的一点小结
- js 的一点用法
- String.split()用法的一点经验
- 临近毕业了才搞明白pcap的一点用法
- 关于flexigrid一点用法
- 关于锚的一点用法
- D3d10和D3d11中Map()函数用法一点心得总结
- RequestMapping用法的一点理解
- history、less和.bash_profile配置文件用法一点
- mock中的spec的一点用法
- response.sendRedirect的一点实际用法
- 关于VC中enum { IDD = IDD_ABOUTBOX }用法一点看法_lizhon...(转载)
- String.split()用法的一点经验
- D3d10和D3d11中Map()函数用法一点心得总结
- String.split()用法的一点经验
- UIImagePickerController在iPhone和iPad中用法的一点不同
- String.split()用法的一点经验
- Mysql ORDER BY用法的一点理解
- UIImagePickerController在iPhone和iPad中用法的一点不同
- find 命令-exec,xargs用法的一点总结