常用工具與JAVA常用小函數實現


import java.io.File;
import java.util.*;

public class ReadJpg {
	public static void main(String args[]) throws Exception{
		Vector<String> Path=new Vector<String>();
		
		 File root = new File("E:/PROJECT/HAND/midea");
		 getJpg(root,Path);
		  try {
		 //  Path=	(Vector<String>) getJpg(root).clone();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 // System.out.println(Path.size());
		  for(int i=0;i<Path.size();i++)
		  {
			  System.out.println(Path.get(i));
		  }
		  
	}
	
	
	/*usage
	 * read all JPG in the dir
	 * intput: File  dir 
	 * output:Vector<String> Path;
	 * 
	 * 
	 */

	 final static Vector<String> getJpg(File dir, Vector<String> Path) throws Exception{
		 // Vector<String> vecstr = new Vector<String>();
		  
		  File[] fs = dir.listFiles();
		  for(int i=0; i<fs.length; i++){
			  String name=fs[i].getName();
		   if(name.endsWith("jpg"))
		   {
			   Path.add(fs[i].getAbsolutePath().toString());
			//   System.out.println(fs[i].getAbsolutePath().toString());
		   }
	     if(fs[i].isDirectory()){
		    try{
		    
		    	getJpg(fs[i],Path);
		     
		    }catch(Exception e){}
		   }
		
		 
		  }
		  
		// System.out.println(vecstr.size());
		return Path;
	 }
}

public class readLine {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int lineNumber=2;
		File sourceFile=new File("E:\\forread.txt");
           //    System.out.println("hello wolrd");
               try
               {
            	   readAppointedLineNumber(sourceFile, lineNumber);  
            	   System.out.println(getTotalLines(sourceFile));  
            	   
               }catch(IOException  e){
            	   e.printStackTrace();
               }
	}

	

	   static void readAppointedLineNumber(File sourceFile, int lineNumber)  
	            throws IOException {  
	        FileReader in = new FileReader(sourceFile);  
	        LineNumberReader reader = new LineNumberReader(in);  
	        String s = "";  
	        if (lineNumber <= 0 || lineNumber > getTotalLines(sourceFile)) {  
	            System.out.println("不在文件的行數範圍(1至總行數)之內。");  
	            System.exit(0);  
	        }  
	        int lines = 0;  
	        while (s != null) {  
	            lines++;  
	            s = reader.readLine();  
	            if((lines - lineNumber) == 0) {  
	                System.out.println(s);  
	                System.exit(0);  
	            }  
	        }  
	        reader.close();  
	        in.close();  
	    }  
	    // 文件內容的總行數。  
	    static int getTotalLines(File file) throws IOException {  
	        FileReader in = new FileReader(file);  
	        LineNumberReader reader = new LineNumberReader(in);  
	        String s = reader.readLine();  
	        int lines = 0;  
	        while (s != null) {  
	            lines++;  
	            s = reader.readLine();  
	            if(lines>=2){  
	                if(s!=null){  
	                    System.out.println(s+"$");  
	                }  
	            }  
	        }  
	        reader.close();  
	        in.close();  
	        return lines;  
	    }  
}

import java.io.*;


public class ReadName {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		int lineNumber=2;
		String FilePath="E:\\Bedroom\\Richard_Gere\\1.2\\1\\9\\9.618.jpg";
		
		System.out.println(GetWorkID(FilePath));
		System.out.println(GetWorkIDAndSequene(FilePath));
		System.out.println(MatchName("E:\\forread.txt","3.456"));
           //    System.out.println("hello wolrd");
      
	}

	
       
	    /*  FilePath format for  E:\\Bedroom\\Richard_Gere\\1.2\\1\\9\\9.618.jpg
	     *   get WorkID from given FilePath
	     *   return WorkID
	     */ 
	    
	    static String  GetWorkID(String FilePath){
	    	String WorkID=null;
	    	String []s=FilePath.split("\\\\");
	    	int temp=s.length-2;
	        WorkID=s[temp];
	    	
			return WorkID;
	    	
	    }
	    
	    /*  FilePath format for  E:\\Bedroom\\Richard_Gere\\1.2\\1\\9\\9.618.jpg
	     *  
	     *  get WorkID and Sequence from given FilePath 
	     *  
	     *  return format for WorkID。Sequence
	     */
	    static String GetWorkIDAndSequene(String filePath) {
	    	String WorkIDAndSequene=null;
	    	String []s1=filePath.split("\\\\");
	    	int temp=s1.length-1;
	    	
	    	String []s2=s1[temp].split("\\.");
	    	WorkIDAndSequene=s2[0]+"."+s2[1];
			return WorkIDAndSequene;
	    }
	    
	    
	    /*
	     * split WorkID and Sequence First;
	     * WorkID format for WorkID.Sequence;
	     * query WorkID and result From txtfile filePath;
	     */
	    
	    static String MatchName(String filePath,String WorkIDAndSequence) throws IOException{
	    	String Name=null;  
	    	String []s1=WorkIDAndSequence.split("\\.");
	    	String WorkID=s1[0];
	    	System.out.println("wordkid="+s1[0]);
	    	FileReader in =new FileReader(filePath);
	    	LineNumberReader reader = new LineNumberReader(in); 
	    	String str = reader.readLine();  
	    	while(str!=null)
	    	{
	    		String []temp=str.split("\\\\");
			
	    		if(temp[4].equals(WorkID))
	    
	    			Name=temp[1];
	         break;
	    	}
	    	reader.close();
	    	in.close();
	    	return Name;
	    }
}

import java.io.FileOutputStream;
pulic class SaveStringToTxt{
	public void main(String []args){
		String str="hello wolrd";
		FileOutputStream out =null;
    try{
    out=new FileOutputStream("d:\\iotest\dest.txt",true); // 指定文件夾命以及存儲路徑
     out.write(str);
	 out.close();
	}
	
}


public static void Save2Txt(String str;String FilePath){
	  FileOutputStream out=new FileOutputStream(FilePath,true);
	  out.write(str);
	  out.close();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章