//读文件
String encoding="utf-8"; //字符编码
File adfile = new File("D:\\tesp.jsp");
if (adfile.isFile() && adfile.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(adfile), encoding);
BufferedReader in = new BufferedReader(read);
String line1;
while ((line1 = in.readLine()) != null) {
out.println(line1);
}
read.close();
}
//写文件
String ad_urljsp = "/javagame/test.jsp" ;
File file = new File(ad_urljsp);
System.out.println("ad_urljsp is " + ad_urljsp);
StringBuffer jspbuff = new StringBuffer();
jspbuff.append(content);
OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(file),ENCODING);
out.write(jspbuff.toString());
out.flush();
out.close();
/**
* 追加文件:使用FileOutputStream,在构造FileOutputStream时,把第二个参数设为true
*
* @param fileName
* @param content
*/
public static void method1(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
out.write(conent);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 追加文件:使用FileWriter
*
* @param fileName
* @param content
*/
public static void method2(String fileName, String content) {
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 追加文件:使用RandomAccessFile
*
* @param fileName
* 文件名
* @param content
* 追加的内容
*/
public static void method3(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//列目录下面的文件
String adfilepath = "/java/page";
System.out.println("##adfilepath=" + adfilepath );
File dirs = new File(adfilepath);
File[] filesDir = dirs.listFiles();
int len = filesDir.length ;