package test2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
public class CommonFile {
private String m_fileName = "";
private File m_File = null;
private BufferedReader m_reader = null;
public CommonFile(String strFileName) {
m_fileName = strFileName;
m_File = new File(m_fileName);
try {
m_reader = new BufferedReader(new FileReader(m_File));
} catch (IOException e) {
}
}
public static boolean writeFile(String strFileName, String content) {
try {
RandomAccessFile randomFile = new RandomAccessFile(strFileName,"rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
return false;
}
return true;
}
public String ReadLine() {
if (!m_File.exists()) {
return null;
}
if (m_reader == null) {
return null;
}
try {
String tempString = m_reader.readLine();
return tempString;
} catch (IOException e) {
}
return "";
}
protected void close() {
if (m_reader != null) {
try {
m_reader.close();
} catch (IOException e) {
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
CommonFile file = new CommonFile("d:\\1.txt");
String line = null;
while ((line = file.ReadLine()) != null) {
System.out.println(line);
}
file.close();
CommonFile.writeFile("d:\\1231.txt", "adsfasdfasdfasdfasdf");
CommonFile.writeFile("d:\\1231.txt", "111111111111111111111");
}
}
Jav读写文件
最新推荐文章于 2024-07-24 12:30:59 发布
本文详细介绍了使用Java进行文件读写操作的方法,包括如何创建文件对象、使用BufferedReader读取文件内容以及使用RandomAccessFile进行高效写入。通过实例演示了如何将字符串内容写入文件,并展示了文件读取和关闭操作的基本步骤。
672

被折叠的 条评论
为什么被折叠?



