内存操作流
内存操作流程是一种只对内存中数据进行读写的输入输出流,不关联任何配置文件。
操作字节数组
输入 | ByteArrayInputStream |
---|---|
输出 | ByteArrayOutputStream |
此流可以直接读取字节数组并将其写入一个字节数组缓冲区,缓冲区会随着数据的不断写入而自动增长,我们可通过toByteArray ( ) 和 toString ()方法来实现对字符串的读写。
public class 操作字节数组 {
public static void main(String[] args) throws IOException {
String s="hello world";
byte[] allbytes = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(allbytes);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len=0;
while ((len=in.read())!=-1){
out.write(len);
}
String str = out.toString();
System.out.println(str);
}
}
操作字符数组
输入 | CharArrayReader |
---|---|
输出 | CharArrayWriter |
此流可以直接读取字符数组并将其写入一个字符数组缓冲区,缓冲区会随着数据的不断写入而自动增长,我们可通过toCharArray ( ) 和 toString ()方法来实现对字符串的读写。
public class 操作字符数组 {
public static void main(String[] args) throws IOException {
String s="hello world";
char[] chars = s.toCharArray();
CharArrayReader reader = new CharArrayReader(chars);
CharArrayWriter writer = new CharArrayWriter();
int len=0;
while ((len=reader.read())!=-1){
writer.write(len);
}
String str = writer.toString();
System.out.println(str);
}
}
操作字符串
输入 | StringReader |
---|---|
输出 | StringWriter |
此流可以直接读取字符串并可以通过append()方法在输出流中直接添加字符串。
public class 操作字符串 {
public static void main(String[] args) throws IOException {
String s="hello world";
StringReader reader = new StringReader(s);
StringWriter writer = new StringWriter();
int len=0;
while ((len=reader.read())!=-1){
writer.write(len);
}
writer.append(s);
String str = writer.toString();
System.out.println(str);
}
}
数据输入输出流
输入 | DataInputStream |
---|---|
输出 | DataOutputStream |
此流可以直接读写基本数据类型。
public class 数据输入输出流 {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(new FileInputStream("配置文件.txt"));
DataOutputStream out = new DataOutputStream(new FileOutputStream("配置文件.txt"));
out.writeChar('A');
out.writeDouble(3.14);
out.writeInt(100);
out.writeUTF("hello world");
out.close();
char c = in.readChar();
double v = in.readDouble();
int i = in.readInt();
String s = in.readUTF();
in.close();
System.out.println(c);
System.out.println(v);
System.out.println(i);
System.out.println(s);
}
}
打印流
打印流的特点:可以直接将,只操作目的地,不关联源文件。可以直接将数据打印至目标文件。
方法 | 功能 |
---|---|
void print() | 打印不换行 |
void println() | 打印换行 |
字节打印流
实现类:PrintStream
public class 字节打印流 {
public static void main(String[] args) throws FileNotFoundException {
PrintStream printStream = new PrintStream(new File("配置文件.txt"));
printStream.println(true);
printStream.println(100);
printStream.println(3.14);
printStream.println("hello world");
printStream.flush();
printStream.close();
}
}
字符打印流
实现类:PrintWriter
public class 字符打印流 {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter writer = new PrintWriter(new FileOutputStream("配置文件.txt"),true);//字符打印流可以自动刷新
writer.println(true);
writer.println(100);
writer.println(3.14);
writer.println("hello world");
writer.close();
}
}
随机访问流
此流能读能写,具有一个 文件指针,能够记录文件读写的位置。此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针。(实现断点下载)
实现类:RandomAccessFile
- 构造方法:
RandomAccessFile(File file, String mode)
参数2为控制文件的可读写("rw"
可读可写) - 获取文件指针位置:
getFilePointer()
- 设置指针位置:
seek()
模拟断点复制文件,先将读写过程停在10kb处并记录指针位置,再开启新的随机访问流从指针处继续复制:
public class 随机访问流 {
public static void main(String[] args) throws IOException {
RandomAccessFile reader1 = new RandomAccessFile("配置文件.txt","rw");
RandomAccessFile reader2 = new RandomAccessFile("配置文件.txt", "rw");
RandomAccessFile writer1 = new RandomAccessFile("副本文件.txt","rw");
RandomAccessFile writer2 = new RandomAccessFile("副本文件.txt", "rw");
int len=0;
while ((len=reader1.read())<=10){
writer1.write(len);
}
long pointer = reader1.getFilePointer();
reader1.close();
writer1.close();
reader2.seek(pointer);
while ((len=reader2.read())!=-1){
writer2.write(len);
}
reader2.close();
writer2.close();
}
}
Properties
Properties属于一个双列集合,规定键值只能是String类型。
特点是可以将配置文件中规定格式的字符串直接读取到Properties集合中储存,并能将集合中的数据保存到配置文件中。
方法 | 功能 |
---|---|
Object setProperty(String key, String value) | 调用 Hashtable 的方法 put |
String getProperty(String key ) | 用指定的键在此属性列表中搜索属性 |
void load(Reader reader) | 按简单的面向行的格式从输入字符流中读取属性列表 |
void store(Writer writer, String comments) | 以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表写入输出字符 |
- 我们定义一个Properties集合并存入数据,再将集合中的数据输出至配置文件:
public class Properties集合 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("中国","北京");
properties.setProperty("美国","华盛顿");
properties.setProperty("英国","伦敦");
properties.setProperty("加拿大","渥太华");
properties.setProperty("澳大利亚","堪培拉");
properties.store(new FileWriter("配置文件.txt"),null);
}
}
会发现输出的格式为“键=值”,所以我们如果通过Properties读取文件中的数据,那么文件中的数据必须保持此格式。
- 读取配置文件中的数据:
- 遍历集合:
public class Properties集合读取 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load(new FileReader("配置文件.txt"));
Set<Map.Entry<Object, Object>> set = properties.entrySet();
for (Map.Entry<Object, Object> entry : set) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
}
}
- 得到结果: