今天接着昨天的IO流讲,内容可能会比较多。
DataInputStream与DataOutputStream它们是建立在已有的IO的基础上的两个特殊的过滤流。规律:它们只是增加了一些特定的方法读取特定的数据。
举例说明1:
public class Test {
public static void main(String[] args) {
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("F:/price.txt"));
dos.writeDouble(243.21);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
举例说明2:
public class Test {
public static void main(String[] args) {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("F:/price.txt"));
System.out.println(dis.readDouble());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dis.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
节点流(System.in是读取键盘输入,可以换成new FileInputStream("f:/1.txt")读文件,也可以换成读网络(Socket)--以后会讲)包装成过滤流编程:
public class Test {
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java如何启动其他进程:Runtime实例.exec(String command)
举例说明:
public class Test {
public static void main(String[] args) {
Process process = null;
BufferedReader br = null;
try {
process = Runtime.getRuntime().exec("javac.exe");
br = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String line = null;
System.out.println("编译出错,错误信息如下~~~~~");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
Random --想访问文件的哪个点,就访问文件的哪个点(任意)。
特征:既可读、又可写、还可追加,不会覆盖原有文件内容。但它只能访问文件。
举例说明1:
public class Test {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("f:/1.txt", "rw");
byte[] buff = new byte[1024];
int hasRead = -1;
while ((hasRead = raf.read(buff)) > 0) {
System.out.println(new String(buff, 0, hasRead));
}
raf.close();
}
}
举例说明2:
/**
* @author lhy
* @description 向文件中的指定位置插入内容
*/
public class Test {
public static void main(String[] args) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile("f:/1.txt", "rw");
insertContent(raf, 100, "Java面向对象之I/O流之RandomAccessFile的使用");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void insertContent(RandomAccessFile raf, int pos,
String content) {
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
raf.seek(pos);// 把记录指针移到要插入的地方
byte[] buff = new byte[1024];
int hasRead = -1;
// 将raf从pos位置开始、直到结尾所有的内容
while ((hasRead = raf.read(buff)) > 0) {
bos.write(buff, 0, hasRead);// 将读取的数据(从pos位置开始)放入bos中
}
raf.seek(pos);// 再次将记录指针移到要插入的地方
raf.write(content.getBytes()); // 写入要插入的内容
raf.write(bos.toByteArray()); // 写入之前保存的内容
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结束语
有关Java中的IO流类比较多,而且方法大同小异,大家有空可以多查查API文档。今天就讲到这,明天开始讲Java面向对象之序列化机制及版本。