如题(求代码详解)!!!   跪谢!!!!

解决方案 »

  1.   


        public static void main(String[] args) {
            String path = "test.txt";
            
            System.out.println(shortestLine(path));
            System.out.println(longestLine(path));
        }
        
        public static String shortestLine(final String path) {
            String result = "";
            
            try {
             BufferedReader reader = null;
            
             try {
             reader = new BufferedReader(new FileReader(new File(path)));             String line;
                
                while (null != (line = reader.readLine())) {
                
                 if(line.length() < result.length()) {
                 result = line;
                 }
                }
             }finally {
             if(reader != null) {
             reader.close();
             }
             }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
        
        public static String longestLine(final String path) {
            String result = "";
            
            try {
             BufferedReader reader = null;
            
             try {
             reader = new BufferedReader(new FileReader(new File(path)));             String line;
                
                while (null != (line = reader.readLine())) {
                
                 if(line.length() > result.length()) {
                 result = line;
                 }
                }
             }finally {
             if(reader != null) {
             reader.close();
             }
             }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }闲,直接给你代码,最好是用两个方法,分别找最长和最短行,各司其职,单一职责。
    你若一定要用一个方法的话,那么就用个Map保存结果,把这个两个方法合并。