Faced with the upcoming exam, Some useful methods referred to file operation drew tremenous attention. Now I make a summary to reading file.
-
import java.io.BufferedReader;
-
import java.io.BufferedWriter;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
import java.io.OutputStreamWriter;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
public class FileUtil
-
{
-
-
public static final String SEP = ":";
-
-
public static final String ENCODING = "UTF-8";
-
-
-
-
-
-
-
-
-
-
-
-
public static Map<String, String> readFileToMap(String filePath)
-
throws Exception
-
{
-
System.out.println("读文件[" + filePath + "]开始...");
-
-
Map<String, String> contents = new HashMap<String, String>();
-
-
File file = new File(filePath);
-
BufferedReader reader = null;
-
-
try
-
{
-
reader = new BufferedReader(new InputStreamReader(
-
new FileInputStream(file), ENCODING));
-
String info = null;
-
-
while ((info = reader.readLine()) != null)
-
{
-
contents.put(info.split(SEP)[0], info.split(SEP)[1]);
-
}
-
}
-
catch (FileNotFoundException e)
-
{
-
System.out.println("文件[" + filePath + "]未找到.");
-
throw e;
-
}
-
catch (IOException e)
-
{
-
System.out.println("文件IO操作出错, 异常信息: " + e.getMessage());
-
throw e;
-
}
-
catch (Exception e)
-
{
-
System.out.println("读文件出错, 异常信息: " + e.getMessage());
-
throw e;
-
}
-
finally
-
{
-
if (reader != null)
-
{
-
try
-
{
-
reader.close();
-
}
-
catch (IOException e)
-
{
-
System.out.println("关闭IO流出错, 异常信息: " + e.getMessage());
-
throw e;
-
}
-
}
-
}
-
-
System.out.println("读文件[" + filePath + "]结束.");
-
-
return contents;
-
}
-
-
-
-
-
-
-
-
-
-
public static List<String> readFileToList(String fileName) throws Exception
-
{
-
List<String> infos = new ArrayList<String>();
-
-
BufferedReader reader = null;
-
-
try
-
{
-
reader = new BufferedReader(new InputStreamReader(
-
new FileInputStream(new File(fileName)), "utf-8"));
-
String info = null;
-
while ((info = reader.readLine()) != null)
-
{
-
infos.add(info);
-
}
-
}
-
catch (FileNotFoundException e)
-
{
-
System.out.println("文件[" + fileName + "]未找到.");
-
throw e;
-
}
-
catch (IOException e)
-
{
-
System.out.println("文件IO操作出错, 异常信息: " + e.getMessage());
-
throw e;
-
}
-
finally
-
{
-
try
-
{
-
if (reader != null)
-
{
-
reader.close();
-
}
-
}
-
catch (IOException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
-
return infos;
-
}
-
-
-
-
-
-
-
-
-
-
public static String readFileToString(String filePath) throws Exception
-
{
-
InputStreamReader isReder = null;
-
BufferedReader br = null;
-
StringBuffer sb = new StringBuffer();
-
try
-
{
-
File f = new File(filePath);
-
if (f.isFile() && f.exists())
-
{
-
isReder = new InputStreamReader(new FileInputStream(f), "UTF-8");
-
br = new BufferedReader(isReder);
-
String line;
-
while ((line = br.readLine()) != null)
-
{
-
sb.append(line).append("\r\n");
-
}
-
}
-
}
-
catch (Exception e)
-
{
-
System.out.println("exception when reading the file" + filePath);
-
throw e;
-
}
-
finally
-
{
-
if (isReder != null)
-
{
-
try
-
{
-
isReder.close();
-
}
-
catch (IOException e)
-
{
-
e.getMessage();
-
}
-
}
-
if (br != null)
-
{
-
try
-
{
-
br.close();
-
}
-
catch (IOException e)
-
{
-
e.getMessage();
-
}
-
}
-
}
-
return sb.toString();
-
}
-
-
-
-
-
-
-
-
-
-
public static void writeFile(String result, String fileName)
-
{
-
File f = new File(fileName);
-
OutputStreamWriter osw = null;
-
BufferedWriter bw = null;
-
try
-
{
-
if (!f.exists())
-
{
-
f.createNewFile();
-
}
-
osw = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
-
-
bw = new BufferedWriter(osw);
-
bw.write(result);
-
}
-
catch (IOException e)
-
{
-
System.out.println("IOException when writing result, "
-
+ e.getMessage());
-
}
-
catch (Exception e)
-
{
-
System.out.println("Exception when writing result, "
-
+ e.getMessage());
-
}
-
finally
-
{
-
if (bw != null)
-
{
-
try
-
{
-
bw.close();
-
}
-
catch (IOException e)
-
{
-
System.out.println("IOException when closing FileWriter, "
-
+ e.getMessage());
-
}
-
}
-
-
if (osw != null)
-
{
-
try
-
{
-
osw.close();
-
}
-
catch (IOException e)
-
{
-
System.out.println("IOException when closing bufferwriter, "
-
+ e.getMessage());
-
}
-
}
-
}
-
}
-
-
public static void main(String[] args) throws Exception
-
{
-
Map<String, String> testMap = FileUtil.readFileToMap("input.txt");
-
System.out.println("---------------------");
-
for (Map.Entry<String, String> entry : testMap.entrySet())
-
{
-
System.out.println(entry.getKey() + ":" + entry.getValue());
-
}
-
-
System.out.println("---------------------");
-
-
List<String> testList = FileUtil.readFileToList("input.txt");
-
-
for (String str : testList)
-
{
-
System.out.println(str);
-
}
-
System.out.println("---------------------");
-
String test = FileUtil.readFileToString("input.txt");
-
FileUtil.writeFile(test, "output.txt");
-
}
-
}
【DataStructure】A useful util class for reading and writing files,布布扣,bubuko.com
【DataStructure】A useful util class for reading and writing files
原文:http://blog.csdn.net/sxb0841901116/article/details/38548945