求助高手!!!急急急!!!读入一个文本文件,请输出该文件中的最短一行和最长一行(文件行数不定) 如题(求代码详解)!!! 跪谢!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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保存结果,把这个两个方法合并。 子类怎么访问父类的属性. java web打包 ResultSet的getObject方法为什么不能提取sysname类型字段的值 Java有没有能操作.tar文件的API?(急,在线等) 请教一个关于JCheckBOX的问题!!! mina万人同时在线问题 我的JBuilder8的Tab键不好使 大家来看呀! java编译 关于解析HTML的元素问题,请各位高手指点一下,分不够再加也没问题 java.security.AccessControlException: access denied (java.io.FilePermission <<AL 跨语言使用memcache问题
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保存结果,把这个两个方法合并。