怎么将txt文件转换成java
1个回答
展开全部
调用时,类名.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);
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
return result.toArray();
}
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);
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
return result.toArray();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询