文件流:
FileOutputStream output = new FileOutputStream("byte.dat");
定义一个OutPutStream,一个OutputStream可以调用write()把一个数据流从内存读入到文件。
FileInputStream input = new FileInputStream("byte.dat");
定义一个InputStream,一个InputStream可以调用read()把文件的数据流入到内存,并且可以打印到屏幕。
BufferedReader传入一个FileReader,传入了 in使用一个readLine函数读取一行。
FileReader fr = new FileReader("num.txt");
BufferedReader in = new BufferedReader(fr);
可以使用StringTokenizer可以进行StringTokenizer,分隔每个元素。
StringTokenizer st = new StringTokenizer(line);
File一个建立一个文件,然后利用FileWriter建立一个文件读入流。
File f = new File("data.txt");
FileWriter output = new FileWriter(f);
output.write(something);读入文件。
Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
BufferedWriter传入一个FileWriter可以对文件进行读入。
FileWriter fw = new FileWriter("num.txt");
BufferedWriter output = new BufferedWriter(fw);
使用write函数读入把数据读入文件。
用后需要关闭。 output.flush();
output.close();
fw.close();
还可以使用FileOutStream和FileInputStream进行文件读写。用这两个都需要进行关闭。
FileOutputStream output = new FileOutputStream("byte.dat");
for (int i = 1; i <= 100; i++)
output.write(i);
output.close();
FileInputStream input = new FileInputStream("byte.dat");
int value;
while ((value = input.read()) != -1)
System.out.print(value + " ");
input.close();
DataOutStream传入一个OutputStream可以进行读入。
FileOutputStream out = new FileOutputStream("primitive.dat");
DataOutputStream output = new DataOutputStream(out);
output.writeUTF("张三");
output.writeInt(19);
output.writeDouble(58.7);
output.writeUTF("李四");
output.writeInt(21);
output.writeDouble(66.8);
output.close();
DataInputStream input = new DataInputStream(new FileInputStream("primitive.dat"));
String name = input.readUTF();
int age = input.readInt();
double weight = input.readDouble();
System.out.println(name+" "+age+" "+weight);
name = input.readUTF();
age = input.readInt();
weight = input.readDouble();
System.out.println(name+" "+age+" "+weight);
使用后需要进行关闭。
RandomAccessFile类
RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
inout.setLength(0); // 将文件长度设为0,删除文件中内容
for (int i = 1; i <= 10; i++) {
double d = Math.random();
inout.writeDouble(d);
System.out.println(d);
}
inout.seek(0); // 设置指针指向第一个数据
System.out.println("第1个随机数是:" + inout.readDouble());
inout.seek(1 * 8); // 设置指针指向下一个数据,即第2个double值,一个double占用8位。
System.out.println("第10个随机数是:" + inout.readDouble());
Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the
file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes
starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the
getFilePointer
method and set by the seek
method.
It is generally true of all the reading routines in this class that if end-of-file is reached before the desired number of bytes has been read, an
EOFException
(which is a kind of IOException
) is thrown. If any byte cannot be read for any reason other than end-of-file, an
IOException
other than EOFException
is thrown. In particular, an
IOException
may be thrown if the stream has been closed.