Java 对文件进行读写操作的例子很多让初学者感到十分困惑我觉得有必要将各种方法进行
一次分析归类理清不同方法之间的异同点
一.在 JDK 中通常是用 InputStream & OutputStream 这两个基类来进行读写操作的
InputStream 中的 FileInputStream 类似一个文件句柄通过它来对文件进行操作类似的在
OutputStream 中我们有 FileOutputStream 这个对象
用FileInputStream 来读取数据的常用方法是
FileInputStream fstream = new FileInputStream(args[]);
DataInputStream in = new DataInputStream(fstream);
用 inreadLine() 来得到数据然后用 inclose() 关闭输入流
完整代码见 Example
用FileOutputStream 来写入数据的常用方法是
FileOutputStream out out = new FileOutputStream(myfiletxt);
PrintStream p = new PrintStream( out );
用 pprintln() 来写入数据然后用 pclose() 关闭输入
完整代码见 Example
二.在 JDK 中支持两个新的对象 Reader & Writer 它们只能用来对文本文件进行操作而
JDK中的 InputStream & OutputStream 可以对文本文件或二进制文件进行操作
用FileReader 来读取文件的常用方法是
FileReader fr = new FileReader(mydatatxt);
BufferedReader br = new BufferedReader(fr);
用 brreadLing() 来读出数据然后用brclose() 关闭缓存用frclose() 关闭文件
完整代码见 Example
用 FileWriter 来写入文件的常用方法是
FileWriter fw = new FileWriter(mydatatxt);
PrintWriter out = new PrintWriter(fw);
在用outprint 或 outprintln 来往文件中写入数据outprint 和 outprintln的唯一区别是后者写
入数据或会自动开一新行写完后要记得 用outclose() 关闭输出用fwclose() 关闭文件
完整代码见 Example
Example :
// FileInputDemo
// Demonstrates FileInputStream and DataInputStream
import javaio*;
class FileInputDemo {
public static void main(String args[]) {
// argslength is equivalent to argc in C
if (argslength == ) {
try {
// Open the file that is the first command line parameter
FileInputStream fstream = new FileInputStream(args[]);
// Convert our input stream to a DataInputStream
DataInputStream in = new DataInputStream(fstream);
// Continue to read lines while there are still some left to read
while (inavailable() !=) {
// Print file line to screen
Systemoutprintln (inreadLine());
}
inclose();
} catch (Exception e) {
Systemerrprintln(File input error);
}
}
else
Systemoutprintln(Invalid parameters);
}
}
Example :
// FileOutputDemo
// Demonstration of FileOutputStream and PrintStream classes
import javaio*;
class FileOutputDemo
{
public static void main(String args[]) {
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try {
// connected to myfiletxt
out = new FileOutputStream(myfiletxt);
// Connect print stream to the output stream
p = new PrintStream( out );
pprintln (This is written to a file);
pclose();
} catch (Exception e) {
Systemerrprintln (Error writing to file);
}
}
}
Example :
// FileReadTestjava
// User FileReader in JDK to read a file
import javaio*;
class FileReadTest {
public static void main (String[] args) {
FileReadTest t = new FileReadTest();
treadMyFile();
}
void readMyFile() {
String record = null;
int recCount = ;
try {
FileReader fr = new FileReader(mydatatxt);
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = brreadLine()) != null) {
recCount++;
Systemoutprintln(recCount + : + record);
}
brclose();
frclose();
} catch (IOException e) {
Systemoutprintln(Uh oh got an IOException error!);
eprintStackTrace();
}
}
}
Example :
// FileWriteTestjava
// User FileWriter in JDK to writer a file
import javaio*;
class FileWriteTest {
public static void main (String[] args) {
FileWriteTest t = new FileWriteTest();
tWriteMyFile();
}
void WriteMyFile() {
try {
FileWriter fw = new FileWriter(mydatatxt);
PrintWriter out = new PrintWriter(fw);
outprint(hithis will be wirte into the file!);
outclose();
fwclose();
} catch (IOException e) {
Systemoutprintln(Uh oh got an IOException error!);
eprintStackTrace();
}
}
}