`
zhangfy068
  • 浏览: 144498 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
读取文件/cmd java
package common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import bean.VideoBean;

public class FileHelp {
	
	//读取文件
	 public static List readFileByLines(File file) {
	        BufferedReader reader = null;
	        List<VideoBean> list=new ArrayList();
	        try {
//	            System.out.println("以行为单位读取文件内容,一次读一整行:");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            // 一次读入一行,直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                // 显示行号
//	                System.out.println("line " + line + ": " + tempString);
	            tempString=tempString.trim();
	            String s[]=tempString.split("[\\s]+");
	            if(s.length>=2){
	            	  VideoBean bean=new VideoBean();
	  	            bean.setGcid(s[0]);
	  	            bean.setCid(s[1]);
	  	            if(s.length>=3){
	  	              bean.setFileSize(s[2]);
	  	            }
	  	          if(s.length>=4){
	  	        	  bean.setTransSpec(s[3]);
	  	          }
	  	            list.add(bean);
	            }else if(s.length==1){
	            	 VideoBean bean=new VideoBean();
		  	            bean.setGcid(s[0]);
		  	          list.add(bean);
	            }
	            }
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
			return list;
	    }
	 
	 //输出文件
	 public static void  WriteFileByLines(File file,List list) throws IOException {
		 PrintWriter pw = new PrintWriter(new FileWriter(file)); 
		 
		 for (Iterator i = list.iterator(); i.hasNext();) {
				pw.println(i.next());
		 }
			 pw.flush();
			 } 

	 
//输出某目录下文件的GCID 等信息

	public  static void recursion(String root){
		File file = new File(root);
		  File[] subFile = file.listFiles();
		  for (int i = 0; i < subFile.length; i++) {
		   if (subFile[i].isDirectory()) {
//		    System.out.println("目录: " + subFile[i].getName());
		    recursion(subFile[i].getAbsolutePath());
		   }else{
		    System.out.println("文件: " + subFile[i].getName());
		    getCMD(subFile[i].getAbsolutePath());
		   }
		  }

	}
	
	
	public static File getFile(String gcid,String spec){
		//在此目录下查找,该GCID的日志
		File file = new File("D:/18");
		 File[] subFile = file.listFiles();
		 for (int i = 0; i < subFile.length; i++) {
			String filename=subFile[i].getName();
			if(filename.indexOf(gcid)>0&&filename.indexOf(spec)>0){//找到该日志后
//				System.out.println(filename);
			    BufferedReader reader = null;
		        try {
//		            System.out.println("以行为单位读取文件内容,一次读一整行:");
		            reader = new BufferedReader(new FileReader(subFile[i]));
		            String tempString = null;
		            // 一次读入一行,直到读入null为文件结束
		            while ((tempString = reader.readLine()) != null) {
		            	 tempString=tempString.trim();
		            	   Pattern p = Pattern.compile("\\[buffer|\\[buffersink|\\[scale\\s@|\\[abuffer|\\[asf|error!|gfsfile open error|log4cplus:ERROR|Invalid data found"); 
		                   Matcher m = p.matcher(tempString); 
		                   if(m.find()){
		                	   System.err.println(tempString);
		                   }
		            }
		        }catch(Exception e){
		        	System.err.println(e.toString());
		        }
			}
		 }
		
		return null;
		
	}
	
	//使用CMD获取相关信息
	public static void getCMD(String path){
		
		try {
			String[] cmd = new String[3];
			cmd[0 ] = "cmd";
			cmd[1] = "/C";
			cmd[2] = "cid "+path;
			Process p = Runtime.getRuntime().exec(cmd);
			InputStreamReader reader =  new InputStreamReader( p.getInputStream() );
			BufferedReader br = new BufferedReader(reader);
			String rs = null;
			while( (rs = br.readLine()) != null){
			System.out.println(rs);
			}
			br.close();
			p.destroy();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//只输出gcid
	public static void getFilterCMD(String path){
		
		try {
			String[] cmd = new String[3];
			cmd[0 ] = "cmd";
			cmd[1] = "/C";
			cmd[2] = "cid "+path;
			Process p = Runtime.getRuntime().exec(cmd);
			InputStreamReader reader =  new InputStreamReader( p.getInputStream() );
			BufferedReader br = new BufferedReader(reader);
			String rs = null;
			while( (rs = br.readLine()) != null){
				if(rs.contains("file gcid: ")){
					String rs2[]=rs.split("file gcid: ");
					System.out.println(rs2[1]);
				}
	
			}
			br.close();
			p.destroy();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		recursion("C:/320");
	}

	public void recursionFilter(String root) {
		File file = new File(root);
		  File[] subFile = file.listFiles();
		  for (int i = 0; i < subFile.length; i++) {
		   if (subFile[i].isDirectory()) {
//		    System.out.println("目录: " + subFile[i].getName());
		    recursion(subFile[i].getAbsolutePath());
		   }else{
		    System.out.println("文件: " + subFile[i].getName());
		    getFilterCMD(subFile[i].getAbsolutePath());
		   }
		  }
		
	}
	
	
	//读取文件
	 public static List readFileByLines2(File file) {
	        BufferedReader reader = null;
	        List<VideoBean> list=new ArrayList();
	        try {
//	            System.out.println("以行为单位读取文件内容,一次读一整行:");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            // 一次读入一行,直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                // 显示行号
//	                System.out.println("line " + line + ": " + tempString);
	            tempString=tempString.trim();
	            String s[]=tempString.split("[\\s]+");
	            if(s.length==5){
	            	 VideoBean bean=new VideoBean();
	            	 bean.setGcid(s[0]);
	            	 bean.setTransSpec(s[2]);
	            	 bean.setCid(s[4]);//失败码
	            	 list.add(bean);
	            	
	            }else if(s.length==4){

	            	 VideoBean bean=new VideoBean();
		  	            bean.setGcid(s[0]);
		  	            bean.setTransSpec(s[1]);
		  	            bean.setFileSize(s[2]);//succ or fail
		  	            bean.setCid(s[3]); // 状态码
		  	          list.add(bean);
	            
	            }
	            }
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
			return list;
	    }
	 
		//读取文件
	
}
设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1.写出程序. java
/**
 * @author WenQiang Wu
 * @version Dec 28, 2009
 */
public class ThreadTest {

    private int j;

    /**
     * add method
     */
    private synchronized void add() {
        j++;
        System.out.println(Thread.currentThread().getName() + "->add : " + j);
    }

    /**
     * inc method
     */
    private synchronized void inc() {
        j--;
        System.out.println(Thread.currentThread().getName() + "->inc : " + j);
    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        ThreadTest tt = new ThreadTest();

        //inner class instance
        Inc inc = tt.new Inc();
        Add add = tt.new Add();

        //start thread
        for (int i = 0; i < 2; i++) {
            //start inc thread
            Thread thread = new Thread(inc);
            thread.start();

            //start add thread
            thread = new Thread(add);
            thread.start();
        }
    }

    /**
     * 
     * @author WenQiang Wu
     * @version Dec 28, 2009
     */
    class Inc implements Runnable {
        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Runnable#run()
         */
        public void run() {
            for (int i = 0; i < 100; i++) {
                inc();
            }
        }
    }

    /**
     * 
     * @author WenQiang Wu
     * @version Dec 28, 2009
     */
    class Add implements Runnable {

        /* (non-Javadoc)
         * @see java.lang.Runnable#run()
         */
        public void run() {
            for (int i = 0; i < 100; i++) {
                add();
            }
        }
    }
}
假如有字符串“6sabcsssfsfs33”,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring)等? java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author WenQiang Wu
 * @version Dec 29, 2009
 */
public class ReplaceString {

    /**
     * use array method
     * 
     * @param s
     * @param match
     */
    static void replace(String s, String match) {
        char[] strs = s.toCharArray();
        char[] matchs = match.toCharArray();

        StringBuilder sb = new StringBuilder();

        for (char c : strs) {
            int index = -1;
            for (char m : matchs) {
                index++;
                if (c == m) {
                    break;
                } else if (index >= matchs.length - 1) {
                    sb.append(c);
                }
            }
        }
        System.out.println(sb.toString());
    }

    /**
     * use regex method
     * 
     * @param s
     * @param p
     */
    static void replace(String s, Pattern p) {
        Matcher m = p.matcher(s);
        StringBuilder sb = new StringBuilder();

        while (m.find()) {
            sb.append(m.group());
        }
        System.out.println(sb.toString());
    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {

        String s = "6sabcsssfsfs33";

        String match = "ab3";

        // method one
        replace(s, match);

        String regx = "[^a|b|3]";
        Pattern p = Pattern.compile(regx);

        // method two
        replace(s, p);
    }
}
Global site tag (gtag.js) - Google Analytics