调用时,类名.readFile("d:/1.txt");即可:
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();
}
取消
评论