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读写文件
最新推荐文章于 2021-03-04 08:26:58 发布