一,将文件读取到字符串(文件不能太大)
package com.util;
import java.io.BufferedReader;import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* @author Robin
* @date 2016年5月17日 上午9:17:28
* @version 1.0
*/
public class ReadTxtFromFile {
public static String readTXT(String fileName) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(new File(
fileName).getAbsoluteFile()));
try {
String s;
while ((s = in.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
public static String readTXT_File(File file) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(
file.getAbsoluteFile()));
try {
String s;
while ((s = in.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
}
二,将字符串写入到文件
package com.util;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author Robin
* @date 2016年5月17日 上午10:36:50
* @version 1.0
*/
public class SaveTxtToFile {
public static void write(String text, String filePath) {
try {
PrintWriter out = new PrintWriter(
new File(filePath).getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
参考《java编程思想》