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

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

  • 相关阅读:
    AcWing每日一题--最大的和
    使用 *args 和 **kwargs 的含义
    Python的八大基本数据类型之 元组、列表、字典
    条件判断与if嵌套
    数据拼接与转换
    print()函数与转义字符
    BEGIN-2 序列求和
    BEGIN-1 A+B问题
    并发编程——进程——生产者消费者模型
    并发编程——进程——进程的同步与数据共享
  • 原文地址:https://www.cnblogs.com/sunchuanzhen/p/readLine.html
Copyright © 2011-2022 走看看