a、 System:
类中的方法与属性都是静态的。用于键值对形式的配置文件
在 System
类提供的设施中,有:
—— 标准输入、标准输出和错误输出流;
——对外部定义的属性和环境变量的访问;加载文件和库的方法;
——还有快速复制数组的一部分的实用方法。
b、 Runtime:
没有构造函数,通过单例设计模式,将构建对象封装在类中,通过一个方法向外提供本类对象,以保证对象的唯一性。
方法摘要:
static Runtime | getRuntime() |
void | |
void | halt(int status) |
Process【进程】: 该
类提供了执行从进程输入、执行、输出到进程等待、进程完成、检查进程的退出状态以及销毁(杀掉)进程的方法。
Process p = newRuntime().exec();
p.destroy();
c、 打印流PrintStream
一、概述
1、打印流包括:PrintStream和PrintWriter
2、该流提供了打印方法,可将各种类型的数据都原样打印。
二、字节打印流:PrintStream
构造方法中可接收的参数类型:
1、File对象。File
2、字符串路径:String
3、字符输出流:OutputStream
三、字符串打印流:PrintWriter
构造方法中可接受的参数类型
1、File对象:File
2、字符串路径:String
3、字节输出流:OutputStream
4、字符输出流:Writer
d、 SequenceInputStream
一、概述
1、SequenceInputStream对多个流进行合并。也被称为合并流。
2、常用构造函数
SequenceInputStream(Enumeration<?extends FileInputStream> e)
二、常见合并多个流文件步骤
1、创建集合,并将流对象添加进集合
2、创建Enumeration对象,将集合元素加入。
3、创建SequenceInputStream对象,合并流对象
4、创建写入流对象,FileOutputStream关联写入文件
5、利用SequenceInputStream对象和FileOutputStream对象读数据进行反复读写操作。
/*
SequenceInputStream
合并流
需求:将三个文本文件中的数据合并到一个文本文件中
思路:1、创建一个Vector集合,将三个文本文件字节流添加到集合中
2、创建Enumeration对象,创建SequnceInputStream对象关联Enumeration
3、输出流关联新文本文件
4、反复读写操作
*/
Vector<InputStream> ve=new Vector<InputStream>(); //创建vector集合,并添加相关流对象
ve.add(newFileInputStream("1.txt"));
ve.add(newFileInputStream("2.txt"));
ve.add(newFileInputStream("3.txt"));
Enumeration<InputStream> en=ve.elements(); //创建枚举对象
SequenceInputStreamsis=new SequenceInputStream(en); //合并流
FileOutputStreamfos=new FileOutputStream("4.txt"); //关联写入文件
f、 切割与合并文件
class sequenceIO {
public static void split() throws IOException {
FileInputStream fis = new FileInputStream("c:\\test.bmp");
FileOutputStream fos = null;
byte[] by = new byte[1024 * 1024];
int len = 0;
int count = 0;
while ((len = fis.read(by)) != -1) {
fos = new FileOutputStream("c:\\" + count++ + ".part");
fos.write(by, 0, len);
fos.flush();
fos.close();
}
fis.close();
}
public static void merge() throws IOException {
// 读取流存入Arraylist集合
ArrayList<InputStream> in = new ArrayList<InputStream>();
File f = new File("c:\\");
String[] fs = f.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".bmp");
}
});
for (String Fr : fs) {
in.add(new FileInputStream(Fr));
}
// 遍历集合加载到枚举类型
final Iterator<InputStream> it = in.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {
@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return it.hasNext();
}
@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return (FileInputStream) it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(en);
}
g、 序列化
概念 序列化:把Java对象转换为字节序列的过程。
反序列化:把字节序列恢复为Java对象的过程。
用途
对象的序列化主要有两种用途:
1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;
2) 在网络上传送对象的字节序列。
序列化API
java.io.ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中。只有实现了Serializable和Externalizable接口的类的对象才能被序列化。
java.io.ObjectInputStream代表对象输入流,它的readObject()方法从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。
.说明
读取对象的顺序与写入时的顺序要一致。
对象的默认序列化机制写入的内容是:对象的类,类签名,以及非瞬态和非静态字段的值。流可以处理数据文件, ObjectOutputStream可以用于处理对象,将堆内存中的对象数据【成员变量】存入文件(以二进制存储,以记事本等默认GBK的打不开)
ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。通过在流中使用文件可以实现对象的持久存储。如果流是网络套接字流,则可以在另一台主机上或另一个进程中重构对象。
class ObjectStream {
public static void objRead() throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois= new ObjectInputStream(new FileInputStream("demo.txt"));
Person p = (Person) ois.readObject();
System.out.print(p);
ois.close();
}
public static void objWrite() throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("demo.txt"));
oos.writeObject(new Person("guzuxin", 58));
oos.close();
}
}
class Person implements Serializable {
public static final long SerialVersionUID = 42L;
private String name;
transient int age;
static String country = "cn";
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return this.name +this.age;
}
}
h、 管道流 PipedInputStream
概念:管道输入流应该连接到管道输出流;
管道输入流提供要写入管道输出流的所有数据字节。
通常,数据由某个线程从 PipedInputStream
对象读取,并由其他线程将其写入到相应的 PipedOutputStream
。不建议对这两个对象尝试使用单个线程,因为这样可能死锁线程。
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
class PipStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pis.connect(pos);
Read ri = new Read(pis);
Write wo = new Write(pos);
new Thread(ri).start();
new Thread(wo).start();
;
}
}
// 读出
class Read implements Runnable {
private PipedInputStream piStream;
public Read(PipedInputStream pis) {
this.piStream = pis;
}
@Override
public void run() {
// TODO Auto-generated method stub
byte[] by = new byte[1024];
int len = 0;
String lineString = null;
try {
while ((len = piStream.read(by)) != -1) {
lineString = new String(by);
System.out.print(lineString);
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("读 出失败");
}
}
}
// 写入
class Write implements Runnable {
private PipedOutputStream pOutputStream;
public Write(PipedOutputStream pos) {
this.pOutputStream = pos;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
pOutputStream.write("Hello World".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("写入失败");
}
}
}
i、 随机读写访问 RandomAccessFile
public class RandomAccessFile
extends Object
implements DataOutput, DataInput, Closeable
此类的实例支持对随机访问文件的读取和写入。包含数组、指针,支持读取与写入操作。直接继承自Object类,非IO类。可以通过getFilePointer获取指针位置。Seek改 变指针位置。
构造函数【只能操作文件】
RandomAccessFile(File file, String mode) throws FileNotFoundException
创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定。
RandomAccessFile(String name, String mode) throws FileNotFoundException
创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。
Mode: "r"、"rw"、"rws" 或 "rwd"
方法摘要
void writeInt(int v)
按四个字节将 int 写入该文件,先写高字节。
void write(byte[] b, int off, int len)
此类的特点是:封装了数组,使得对流的操作可以带角标读写,而流的读写必须是按顺序的,这样提高了运行效率。
RandomAccessFile raFile =new RandomAccessFile("demo.txt", "r");
//调整对象中的指针
raFile.seek(8*10);
//跳过指定的字节数
raFile.skipBytes(8);
byte[] by = new byte[4];
raFile.read(by);
raFile.write(by);
j、 操纵基本数据类型DataInputStream
public class DataInputStream
extends FilterInputStreamimplements DataInput
DataInputStream dis = new DateInputStream (new FileInputStream("demo.txt"));
int num = dis.readInt();
boolean b = dis.readBoolean();
double d = dis.readDouble();
k、 操作字节数组ByteArrayInputStream
public class ByteArrayInputStream
extends InputStream
不涉及底层调用,close()无效,没有异常。用流的思想来操作数组
ByteArrayInputStream
包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪
read
方法要提供的下一个字节。
l、Propreties
主要用于读取以项目的配置文件(以.properties结尾的文件和xml文件)。Propreties保存在流中或从流中加载. 属性列表中每个键及其对应值都是一个字符串
#测试环境配置:平台路径配置
jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/
dbPort =localhost
databaseName= mydb
dbUserName= root
dbPassword= root
# 以下为数据库表信息
dbTable =mytable
# 以下为服务器信息
ip =192.168.0.9
读取test.properties的方法如下:
class ReadProperties {
publicstaticvoid main(String[] args) {
FilepFile = newFile("e:\test.properties"); // properties文件放在e盘下(windows)
FileInputStreampInStream = null;
try {
pInStream= newFileInputStream(pFile);
}catch(FileNotFoundException e) {
e.printStackTrace();// To change body of catch statement use File |
// Settings | File Templates.
}
Propertiesp = newProperties();
try {
p.load(pInStream);// Properties 对象已生成,包括文件中的数据
}catch(IOException e) {
e.printStackTrace();// To change body of catch statement use File |
// Settings | File Templates.
}
Enumerationenu = p.propertyNames(); // 取出所有的key
// 输出--1
p.list(System.out); // System.out可以改为其他的输出流(包括可以输出到文件)
// 输出--2
while (enu.hasMoreElements()){
System.out.print("key=" +enu.nextElement());
System.out.print("value="
+p.getProperty((String) enu.nextElement()));
}
}
}
读取xml格式的配置文件
test.xml文件ruxi
<?xmlversion="1.0" encoding="UTF-8"?>
<!DOCTYPEproperties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entrykey="koo">bar</entry>
<entrykey="fu">baz</entry>
class Test {
publicstaticvoid main(String[] args) {
FilepFile = newFile("e:\test.xml"); // properties文件放在e盘下(windows)
FileInputStreampInStream = null;
try {
pInStream= newFileInputStream(pFile);
Properties p = new Properties();
p.loadFromXML(pInStream);
p.list(System.out);
}catch(IOException e) {
e.printStackTrace();
}
}
}
通过list 方法将Properties写入Properties文件
Properties p = new Properties();
p.setProperty("id","dean");
p.setProperty("password","123456");
PrintStream fW = new PrintStream(newFile("e:\test1.properties"));
p.list(fW);