java 对于文件的操作
/**
* 把指定的内容写到指定路径的文本文件上
*
* @param path指定路径
* @param context 要写的内容
*/
public static void writeFile(String path, String context) {
// 从控制台输入内容写入文件
try {
FileWriter fw = new FileWriter(path, true);
PrintWriter pw = new PrintWriter(fw);
pw.write(context);
pw.println();
pw.flush();
fw.close();
pw.close();
System.out.println("写入文件成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 从指定路径下读取一个文本文件
* @param path指定路径
* @return 这个文本文件中的内容
*/
public static String BufferReadFile(String path)throws Exception {
StringBuilder context = new StringBuilder();
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
char[] c = new char[2048];
int i = br.read(c);
if (i < c.length) {
for (int j = 0; j < i; j++) {
context.append(c[j]);
}
return context.toString();
}
while (i != -1) {
context.append(c);
i = br.read(c);
}
fr.close();
br.close();
return context.toString();
}
相关文档:
Java学习从入门到精通
一、 JDK (Java Development Kit)
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的J ......
package server;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Recta ......
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
public class ThreadLog
{
private final static String _detailLogFile = "log"+File.separator+"detail";
private static boolean _logFlag = true;
priv ......
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class ExceptionDefaultHandler
{
private final static String _relativeExceptionLogPath = "log";
private final static String _defaultExceptionLogFileName = "exception.log";
......
public class StringUtil
{
public static String convertToStringWithTrim(Object object)
{
if(null == object)
{
return null;
}
String returnStr = (String)object;
return Trim(returnStr);
}
public static String Trim(String str)
{
if(IsEmpty(str))
{
r ......