zoukankan      html  css  js  c++  java
  • java线程读取文件,可以同时读写 202006031002

    package org.jimmy.monitor.test;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    
    public class FileTest {
    
        public static String path = "E:/Test/test.txt";
        
        public static void main(String[] args){
            test();
        }
        
        public static void test(){
            ReadThread readThread = new ReadThread();  
            Thread thread1 = new Thread(readThread, "readThread");  
            thread1.start();  
        }
        
        static class WriteThread implements Runnable {  
              
            @Override  
            public void run() {  
                try {  
                    File file = new File(path);  
                    FileOutputStream fos = new FileOutputStream(file);  
                    byte[] b = "hello".getBytes(); 
                    boolean flag = true;
                    while (flag) {  
                        fos.write(b);  
                        fos.flush();  
                        Thread.sleep(1000);  
                    }  
                    fos.flush();
                    fos.close();
                } catch (Exception e) {  
                    e.printStackTrace();  
                }
            }  
      
        }  
        
        static class ReadThread implements Runnable {  
              
            @Override  
            public void run() {  
                try {    
                    int cacheLength = 1024;
                    boolean flag = true;
                    while(flag){
                        FileInputStream fis = new FileInputStream(path);
                        BufferedReader br = new BufferedReader(new InputStreamReader(fis, "utf-8"));
                        int fileLength = fis.available();
                        if(fileLength < cacheLength) {
                            cacheLength = fileLength;
                        }
                        int len = cacheLength;
                        char[] chars = new char[cacheLength];
                        while((len = br.read(chars, 0, len)) != -1) {
                            String text = new String(chars);
                            System.out.println(text);
                            Thread.sleep(1000);
    //                        sb.append(text);
                        }
                        br.close();
                        fis.close();
                    }
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
            
        }  
        
    }
  • 相关阅读:
    块元素&行内元素
    semantic ui要装什么才能使用
    float属性
    CSS 选择器
    px,em和rem
    CSS各类布局
    一个 / 引起想骂他事件
    使用fastjson 获取json字符串中的数组,再转化为java集合对象
    计算面试题
    Dubbo(二) 一次惨痛的流血事故
  • 原文地址:https://www.cnblogs.com/JimmySeraph/p/13035676.html
Copyright © 2011-2022 走看看