怎样将TXT文件格式转换成JAVA格式?

来源:百度知道 编辑:UC知道 时间:2024/09/25 07:21:25
如题

调用时,类名.readFile("d:/1.txt");即可:
public static String[] readFile(String sourceFileName) throws IOException
{
String[] array = null;
Set<String> result = new HashSet<String>();

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);
}