zoukankan      html  css  js  c++  java
  • 关于java IO 过程当中同时读写的问题

    今天在写一个linux的java守护进程的时候,无意间就用到了java同时读写的功能。

    看错误代码:

    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+"
    ");
    			}
    			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();
    			}
    		}
    		
    	}
    
    }
    

      这时候就工作了,操作完了读操作后,然后把读操作关闭,之后再进行写操作,这个样子就不会有错了。

  • 相关阅读:
    (转)Android系统自带样式(@android:style/)
    Android:安卓资源引用符号的含义
    Android:再按一次退出程序
    Android:实现退出确认对话框
    Android:仿微信开场切换界面
    Android:ViewPager适配器PagerAdapter的使用
    Android:使用ViewPager实现左右滑动切换图片(图上有点点)
    Android:使用ViewPager实现左右滑动切换图片 (简单版)
    inflate方法与findViewById的区别
    Android:自定义标题栏
  • 原文地址:https://www.cnblogs.com/sunchuanzhen/p/readLine.html
Copyright © 2011-2022 走看看