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

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

  • 相关阅读:
    求概率 Bag of mice CodeForces
    HDU 3853 LOOPS(概率DP)
    HDU 4405 Aeroplane chess (概率DP & 期望)
    求期望 ZOJ 3329 One Person Game
    poj 3774 Scout YYF I (矩阵优化的概率DP)
    codeforces 600D Area of Two Circles' Intersection
    codeforces 600A Extract Numbers
    codeforces 600C Make Palindrome
    POJ
    POJ
  • 原文地址:https://www.cnblogs.com/sunchuanzhen/p/readLine.html
Copyright © 2020-2023  润新知