File, formater

睁着眼做梦 posted @ 2014年1月15日 00:10 in Java , 722 阅读
public class WordCount {

	// EDIT DIR PATH WHEN NEEDED, DEFINED AS A CONSTANT
	private static final String FILE_DIR = "//Users//charles//Documents//workspace//assignment1//";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// list to store file names
		List<String> fileNameList = new ArrayList<String>();
		// total size of files
		int total = 0;

		// input file names
		System.out.println("Enter the files to be calculated(\"q\" to indicate no more files to input): ");

		// read all possible file names
		try{
			BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
		String s = bufferRead.readLine();
			while(!s.equals("q")){
				fileNameList.add(s);
				s = bufferRead.readLine();
			}

		}catch(IOException e){
			e.printStackTrace();
		}

		// attempt to open files 
		for(String fileName : fileNameList){
			File file = new File(FILE_DIR.concat(fileName));

			// if file exists, format print it, and recalculate total size
			// exception if file cannot be accessed
			try{
				if(file.exists()){

					System.out.println(String.format("%7d", file.length()) +" "+ fileName);
					total += file.length();
				}
			}catch(SecurityException e){
				System.out.println("Cannot access file: " + fileName);
			}
		}

		// print total size
		System.out.println(String.format("%7d", total) + " total");

	}
// this is the comment i made from GVim, good !~~~
}

 

fdgdg

  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter