直接改后缀名,把 .txt后缀改为 .java 。
调用时,类名.readFile("d:/1.txt");即可:
123456789101112131415161718192021222324252627282930313233343536373839404142
public static String[] readFile(String sourceFileName)
throws IOException { String[] array =
null; Set
result = new HashSet(); String lineText; BufferedReader reader = null; try { File sourceFile = new File(sourceFileName); reader = new BufferedReader(new FileReader(sourceFile)); while ((lineText = reader.readLine()) != null) { //验证是否合法 if(!"".equals(lineText.trim())) { result.add(lineText.trim()); } } } catch (FileNotFoundException e) { //这里由于文件基本不太可能找不到,将此类异常忽略 throw new RuntimeException(e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } } return result.toArray();}