⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutil.java

📁 webwork study w ebwork study
💻 JAVA
字号:
package jaction.utility;

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

/**
 * 文件处理工具类<br>
 * @author yanger
 * @version $Revision: 1.4.2.4 $ $Date: 2003-4-24 11:31 $
 * <pre>
 *日志察看器:
 *		java -classpath=%classpath%;..; jaction.utility.FileUtil 
 * </pre>
 */
 
public class FileUtil{
	

	//---------------------------------------------------------------------------->static methods
	/**
	 * 打印message到标准输出
	 * @param message 消息
	 */
	 public static void log(String message){
		System.out.print("[jaction.log]: ");
        System.out.println(message);
        System.out.flush();
	 }
	/**
	 * 读取文件路径,打印文件到标准输出<br>
	 * 模仿unix tail command,在main函数中使用,无需参数
	 */
	private static void readAndPrintln() throws IOException{
		BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
	    String line=null;
		boolean quitNow = false;
		File theFile = null;
		do {
		  System.out.print("Enter a file path of the server (\"quit\" to quit): ");
		  line = msgStream.readLine();

		  if (line != null && line.trim().length() != 0) {
			//退出
			if(line.equalsIgnoreCase("quit")){
				System.out.println("Bye-Bye,thank you\n");
				return;
			}
			//验证文件合法性
			theFile = new File(line);
			if(!theFile.isFile()){
				System.out.println(""+line+"is not a file,please check it\n");
				return;
			}else{
				quitNow = true;
			}
		  }
	    } while (! quitNow);//回车继续
		//文件读取处理
		System.out.println("***********************************************************************");
		System.out.println("* the file path   is "+theFile.getPath());
		System.out.println("* the file name   is "+theFile.getName());
		System.out.println("* the file length is "+theFile.length());
		System.out.println("***********************************************************************");
		
		
		String tempStr = "";
		String oldStr = "";
		boolean flag = false;
		long fileLength = theFile.length();
		//循环读出文件
		while (true) {
			StringBuffer buffer = new StringBuffer();
		    BufferedReader in =  new BufferedReader(new FileReader(line));
		    flag = false;
		    fileLength = fileLength + oldStr.length();
            in.skip(fileLength);
			while(((tempStr = in.readLine()) != null)) {
				buffer.append(tempStr);
				buffer.append("\n");
				flag = true;
				fileLength = fileLength + 1;
			}
			in.close();
		    oldStr = buffer.toString();
			if (flag)
				System.out.print(buffer.toString());
		}


	}
/////////////////////////////////////////////////////////////////////
//			
/////////////////////////////////////////////////////////////////////
  /**
   * 执行函数<br>
   * 执行readAndPrintln() method
   * @param args 参数
   */
  public static void main(String[] args){

    try{
		readAndPrintln();
	}catch(IOException e){
		System.err.println("readAndPrintln has Error:"+e);
	}
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -