关于java IO 过程当中同时读写的问题
2014-02-15 11:16
399 查看
今天在写一个linux的java守护进程的时候,无意间就用到了java同时读写的功能。
看错误代码:
在上述的代码当中,读写同时进行没有先后顺序。这样导致的结果就是readLine()出来的内容是null,也就是同时读写的一个弊病。
经别人指正后修改如下:
这时候就工作了,操作完了读操作后,然后把读操作关闭,之后再进行写操作,这个样子就不会有错了。
看错误代码:
package cn.sunchuanzhen.main; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class DomainDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File file = new File("C:/ax.txt"); if(!file.exists()){ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("文件创建失败"); } } while (true) { FileWriter fw = new FileWriter(file); BufferedWriter bfw = new BufferedWriter(fw); FileReader fr = new FileReader(file); BufferedReader bfr = new BufferedReader(fr); String str = null; StringBuilder sb = new StringBuilder(); String buf = null; // System.out.println(bfr.readLine()); while((buf = bfr.readLine())!=null) { sb.append(buf); } str = System.currentTimeMillis()+""+sb.toString(); System.out.println(str); bfw.write(str); bfw.newLine(); bfw.flush(); fw.close(); fr.close(); bfw.close(); bfr.close(); try { Thread.sleep(5*1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
在上述的代码当中,读写同时进行没有先后顺序。这样导致的结果就是readLine()出来的内容是null,也就是同时读写的一个弊病。
经别人指正后修改如下:
package cn.sunchuanzhen.main; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class DomainDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File file = new File("C:/ax.txt"); if(!file.exists()){ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("文件创建失败"); } } while (true) { FileReader fr = new FileReader(file); BufferedReader bfr = new BufferedReader(fr); String str = null; StringBuilder sb = new StringBuilder(); String buf = null; // System.out.println(bfr.readLine()); while((buf = bfr.readLine())!=null) { sb.append(buf+"\r\n"); } str = System.currentTimeMillis()+""+sb.toString(); fr.close(); bfr.close(); FileWriter fw = new FileWriter(file); BufferedWriter bfw = new BufferedWriter(fw); bfw.newLine(); bfw.write(str); bfw.newLine(); bfw.flush(); fw.close(); bfw.close(); try { Thread.sleep(5*1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
这时候就工作了,操作完了读操作后,然后把读操作关闭,之后再进行写操作,这个样子就不会有错了。
相关文章推荐
- 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题
- 学习安卓开发过程中遇到关于R.Java文件的问题
- javaweb上传文件过程中关于指针定位的问题
- 关于java.io.EOFException异常的解决问题
- 关于java的io一个需要注意的问题
- 关于Java基础输入输出IO的问题
- Java IO学习【2】一个关于换行、续写的的小问题
- java 关于项目导出功能实现过程中遇到的问题及思路历程
- java 关于初学面向对象的问题二:创建对象过程
- 关于java.io.IOException: open failed: EACCES (Permission denied)问题的解决
- 关于“ java.io.FileNotFoundException: C:\Program%20Files\Apache%20Software%20Foundation\”的类似问题
- java.io.File的基本使用以及关于Windows中大小写问题
- 关于迭代集合同时删除元素报错的问题java.util.ConcurrentModificationException
- 网络访问过程中遇到的java.io.IOException: Target host must not be null的问题
- 关于Mysql数据库longblob格式数据的插入com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V问题分析
- 解决MapReduce编程过程中的 java.io.FileNotFoundException 问题
- android配置第三方jar包过程中关于找不到类 java.lang.noclassdeffounderror的问题解决办法
- 网络访问过程中遇到的java.io.IOException: Target host must not be null的问题
- java中NIO和IO的关于阻塞与非阻塞的问题
- spring mvc使用过程中关于spring:bind和绑定java.util.Date遇到的问题