跟随狂神学Java
作者:joker2yue
链接:https://github.com/Joker2Yue/Joker2Yue-Blog
来源:Github
著作权归原作者所有。商业转载请联系原作者获得授权,非商业转载请注明出处。
第二十五天:IO流
生活本就沉闷,但跑起来有风
狂神未更新,转动力节点(bilibili.com)
学习内容
IO流的概述
用于读写文件中的数据(可以读写文件,或网络中的数据)
I: input
O: output
IO流的分类
按照流向(以内存作为参照):
按照操作类型(数据读写方式):
- 字节流:可以操作所有类型的文件(这种流一次读写一个字节byte,等同于一次读写8个二进制。可以读写文本、图片、声音、视频等文件)
- 字符流:只能操作纯文本文件(这种流按照字节读取数据,一次读写一个字符,这种流是为了操作文本文件存在的)
IO流的体系结构
IO流类的命名规则
注意:只要类名以Stream结尾的都是字节流,以”Read/Write“结尾的都是字符流
四大家族的首领都是抽象类(java.io.InputStream
,java.io.Output
,java.io.Reader
,java.io.Write
)
所有流都实现了java.io.Closeable接口,都是可关闭的,都有close()方法
流毕竟是一个管道,这个是内存和硬盘之间的通道,用完一定要关闭,不然会占用很多资源
FileOutputStream
操作本地文件的直接输出流,可以把程序中的数据写到本地文件中
书写步骤:
- 创建字节输出流对象
- 写数据
- 释放资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
public class OutputTest01 { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("a.txt"); fos.write(97); fos.close(); } }
|
注意:
- 创建FileOutputStream对象时填入的参数,可以是文件路径或者是File对象
- 如果文件不存在 就会创建一个新的文件,但是前提是父级路径是合理的
- 如果文件已经存在,将会覆盖原文件,除非填入参数
FileOutputStream fos = new FileOutputStream("a.txt",true);
- write方法的参数是整数,但是实际上写到本地文件中的是整数在ASCII上对应的字符
- 每次使用完流都要释放资源,否则会一直占用文件
流的close()和flush()方法
OutputStream和Write都有Flushable接口,这意味着所有的输出流都是可刷新的,都有flush()方法
养成好习惯,输出流在最终输出之后,一定要记得flush()刷新一下,表示在将管道/通道中剩余的未输出的数据强行输出完(清空管道)
注意:如果没有flush(),可能会导致丢失数据
需要掌握的流
- 文件专属
- java.io.FileInputStream
- java.io.FileOutputStream
- java.io.FileReader
- java.io.FileWriter
- 转换流(将字节流转换成字符流)
- java.io.InputStreamReader
- java.io.OutputStreamWriter
- 缓冲流专属
- java.io.BufferedReader
- java.io.BufferedWriter
- java.io.BufferedInputStream
- java.io.BufferedOutputStream
- 数据流专属
- java.io.DatainputStream
- java.io.DataOutputStream
- 标准输出流
- java.io.PrintWrter
- java.io.PrintStream
- 对象专属流
- java.io.ObjectInputStream
- java.io.ObjectOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
public class FileInputStreamTest01 { public static void main(String[] args) {
FileInputStream fis = null;
try { fis = new FileInputStream("E:\\Program\\Idea\\Java\\helloworld\\src\\com\\joker_yue\\javalearn\\IOLearn\\test.txt");
int readData = fis.read(); System.out.println(readData);
readData = fis.read(); System.out.println(readData);
readData = fis.read(); System.out.println(readData);
} catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
|
使用while循环来改进
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
public class FileInputStreamTest02 { public static void main(String[] args) { FileInputStream fis = null; try { fis = new FileInputStream("E:\\Program\\Idea\\Java\\helloworld\\src\\com\\joker_yue\\javalearn\\IOLearn\\test.txt"); while (true) { int readData = fis.read(); if (readData == -1) break; System.out.println(readData); }
int readData = 0; while ((readData = fis.read()) != -1) { System.out.println(readData); }
} catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
|
IDEA中的当前路径
如果我们一次读取一个字节的话,内存硬盘交互的太频繁
int read(byte[] b)
从此输入流中将最多b.length个字节的数据读入一个byte数组中
在IDEA中的默认的当前路径是Project的根
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
public class FileInputStreamTest03 { public static void main(String[] args) { FileInputStream fis = null; try {
fis = new FileInputStream("tempFile"); } catch (FileNotFoundException e) { throw new RuntimeException(e); }finally { if (fis == null) { try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
|
往byte数组中读
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
public class FileInputStreamTest03 { public static void main(String[] args) { FileInputStream fis = null; try {
fis = new FileInputStream("javalearn\\IOLearn\\tempFile.txt");
byte[] bytes = new byte[4]; int readCount = fis.read(bytes); System.out.println(readCount); System.out.println(new String(bytes));
readCount = fis.read(bytes); System.out.println(readCount); System.out.println(new String(bytes));
readCount = fis.read(bytes); System.out.println(readCount); System.out.println(new String(bytes, 0, readCount));
} catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (fis == null) { try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
|
最终版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
public class FileInputStreamTest04 { public static void main(String[] args) { FileInputStream fis = null; try { fis = new FileInputStream("javalearn\\IOLearn\\tempFile.txt"); byte[] bytes = new byte[4];
int readCount = 0; while ((readCount = fis.read(bytes)) != -1) { System.out.println(new String(bytes, 0, readCount)); }
} catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
}
}
|
int available()
,long skip(long n)
返回类型 |
方法名 |
说明 |
int |
available() |
返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。 |
long |
skip(long n) |
从输入流中跳过并丢弃 n 个字节的数据。 |
文件复制
原理:边读边写。
FileReader
FileReader/FileWriter与FileInputStream/FileOutputStream之间的区别是,FileInputStream/FileOutputStream使用的是byte[],而FileReader/FileWriter是char[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;
public class FileReaderTest { public static void main(String[] args) { FileReader reader = null; try { reader = new FileReader("javalearn\\IOLearn\\tempFile.txt"); char[] chars = new char[4];
reader.read(chars); for (char c : chars) { System.out.print(c); }
} catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
} }
|
上述代码的执行结果为
FileWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileWriter; import java.io.IOException;
public class FileWriterTest { public static void main(String[] args) { FileWriter out = null; try { out = new FileWriter("file",true); char[] chars = {'我', '是', '帅', '哥'}; out.write(chars);
out.write("我是一名软件工程师"); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
|
复制普通文本文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;
public class copy02 { public static void main(String[] args) { FileReader in = null; FileWriter out = null; try { in = new FileReader("javalearn/IOLearn/copy02.java"); out = new FileWriter("Copy03.txt");
char[] chars = new char[1024*512]; int readCount = 0; while((readCount = in.read(chars))!=-1){ out.write(chars,0,readCount); } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if(in!=null){ try { in.close(); } catch (IOException e) { throw new RuntimeException(e); } } if(out!=null){ try { out.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
|
带有缓冲的字符流
名字里带Buffer的自带缓冲,比如BufferedReader/BufferedWriter
BufferedReader的构造方法:BufferedReader(Reader in)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.joker_yue.javalearn.IOLearn;
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;
public class BufferedReaderTest { public static void main(String[] args) throws IOException { FileReader reader = new FileReader("Copy03.txt"); BufferedReader br = new BufferedReader(reader);
String s = null; while ((s = br.readLine()) != null) { System.out.println(s); }
br.close(); } }
|
节点流和包装流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.joker_yue.javalearn.IOLearn;
import java.io.*;
public class BufferedReaderTest02 { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream("Copy03.txt");
InputStreamReader reader = new InputStreamReader(in); BufferedReader br = new BufferedReader(reader); String line = null; while ((line = br.readLine()) != null){ System.out.println(line); }
} }
|
合并起来写:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.joker_yue.javalearn.IOLearn;
import java.io.*;
public class BufferedReaderTest02 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("Copy03.txt"))); String line = null; while ((line = br.readLine()) != null){ System.out.println(line); }
} }
|
在转换流构造方法中传一个编码格式可解决乱码,如:new BufferedReader(new InputStreamReader(new FileInputStream(""),"GB18030"))
带有缓冲区的字符输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.joker_yue.javalearn.IOLearn;
import java.io.*;
public class BufferedWriterTest01 { public static void main(String[] args) throws IOException { BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy02",true))); out.write("hello world"); out.write("\n"); out.write("hello kitty"); out.flush(); out.close(); } }
|
数据流
DataOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.joker_yue.javalearn.IOLearn;
import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
public class DataOutputStreamTest { public static void main(String[] args) throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("data")); byte b=100; short s=200; int i=300; long l=400; double d=500.0; boolean bl=false; char c='a'; dos.writeByte(b); dos.writeShort(s); dos.writeInt(i); dos.writeLong(l); dos.writeDouble(d); dos.writeBoolean(bl); dos.writeChar(c); dos.flush();
} }
|
DataInputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.joker_yue.javalearn.IOLearn;
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
public class DataInputStreamTest { public static void main(String[] args) throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("data")); byte b = dis.readByte(); short s = dis.readShort(); int i = dis.readInt(); long l = dis.readLong(); double d = dis.readDouble(); boolean bl = dis.readBoolean(); char c = dis.readChar();
System.out.println(b); System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(d); System.out.println(bl); System.out.println(c); } }
|
标准输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.joker_yue.javalearn.IOLearn;
import java.io.PrintStream;
public class PrintStreamTest { public static void main(String[] args) { System.out.println(""); PrintStream ps = System.out; ps.println("hello zhangsan"); ps.println("hello lisi"); ps.println("hello wangwu");
} }
|
我们手动修改输出位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream;
public class PrintStreamTest { public static void main(String[] args) throws FileNotFoundException { System.out.println("");
PrintStream ps = System.out; ps.println("hello zhangsan"); ps.println("hello lisi"); ps.println("hello wangwu");
PrintStream printStream = new PrintStream(new FileOutputStream("log")); System.setOut(printStream); System.out.println("hello World"); System.out.println("hello Kitty"); System.out.println("hello Zhangsan");
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.joker_yue.javalearn.IOLearn;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.text.SimpleDateFormat; import java.util.Date;
public class Logger {
public static void log(String msg) { try { PrintStream out = new PrintStream(new FileOutputStream("log.txt",true)); System.setOut(out); Date nowTime = new Date(); SimpleDateFormat self = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); String strTime = self.format(nowTime);
System.out.println(strTime + "" + msg); } catch (FileNotFoundException e) { throw new RuntimeException(e); }
} }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.joker_yue.javalearn.IOLearn;
public class loggerTest {
public static void main(String[] args) { Logger.log("调用了System类的gc()方法,建议启动垃圾回收"); Logger.log("调用了UserService的doSome()方法"); Logger.log("用户尝试进行登录,验证失败"); } }
|
File类
注意:
- File类和四大家族没有关系,所以File类不能完成文件读写
- File对象表示
路径和目录名的抽象表示形式
C:\Deskto\ 是一个File对象
C:\readme.txt 也是一个File对象
一个File对象有可能是目录,有可能是文件
File只是一个路径名的抽象表现形式
- File中常用的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com.joker_yue.javalearn.IOLearn;
import java.io.File; import java.io.IOException;
public class FileTest01 { public static void main(String[] args) throws IOException { File f1 = new File("E:\\TempFiles\\File"); System.out.println(f1.exists());
File f3 = new File("E:\\TempFiles\\File"); String parentPath = f3.getParent(); System.out.println(parentPath); File parentFile = f3.getParentFile(); System.out.println("获取绝对路径:"+parentFile.getAbsoluteFile());
File f4 = new File("copy"); System.out.println("绝对路径"+f4.getAbsoluteFile());
}
}
|
File常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.joker_yue.javalearn.IOLearn;
import java.io.File; import java.text.SimpleDateFormat; import java.util.Date;
public class FileTest02 { public static void main(String[] args) { File f1 = new File("E:\\TempFiles\\File"); System.out.println("文件名" + f1.getName());
System.out.println(f1.isDirectory()); System.out.println(f1.isFile());
long haoMiao = f1.lastModified(); Date time = new Date(haoMiao); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSS"); String strTime = sdf.format(haoMiao); System.out.println(strTime);
System.out.println(f1.length());
} }
|
获取所有的子文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.joker_yue.javalearn.IOLearn;
import java.io.File;
public class FileTest03 { public static void main(String[] args) { File f = new File("E:\\Downloads"); File[] files = f.listFiles(); for(File file:files){ System.out.println(file.getAbsoluteFile()); } } }
|
对象的序列化和反序列化
将内存中的对象存储在硬盘中的过程叫做序列化 Serialize
同理,将硬盘中松散的对象组装回内存中的过程叫做反序列化 Deserialize
可以理解为拆分和组装的过程
序列化的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.joker_yue.javalearn.IOLearn.ObjectOutputLearn;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream;
public class ObjectOutputTest01 { public static void main(String[] args) throws IOException { Student s = new Student(111, "zhangsan"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Students"));
oos.writeObject(s); oos.flush(); oos.close();
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.joker_yue.javalearn.IOLearn.ObjectOutputLearn;
import java.io.Serializable;
public class Student implements Serializable { int no; String name;
public Student(int no, String name) { this.no = no; this.name = name; }
public int getNo() { return no; }
public void setNo(int no) { this.no = no; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public String toString() { return "Student{" + "no=" + no + ", name='" + name + '\'' + '}'; } }
|
通过源代码Serializable只是一个标志性接口,里面什么代码都没有,给Java虚拟机看的
反序列化的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.joker_yue.javalearn.IOLearn.ObjectOutputLearn;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream;
public class ObjectInputStreamTest01 { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Students")); Object obj = ois.readObject(); System.out.println(obj);
ois.close(); } }
|
Users类的序列化和反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.joker_yue.javalearn.IOLearn.ObjectOutputLearn;
import java.io.*; import java.util.ArrayList; import java.util.List;
public class ObjectOutputStreamTest02 { public static void main(String[] args) throws IOException { List<User> userList = new ArrayList<>(); userList.add(new User(1,"zhangsan")); userList.add(new User(2,"lisi")); userList.add(new User(3,"wangwu"));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users"));
oos.writeObject(userList);
oos.flush(); oos.close();
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.joker_yue.javalearn.IOLearn.ObjectOutputLearn;
import java.io.Serializable;
public class User implements Serializable { private int no; private String name;
public User(int no, String name) { this.no = no; this.name = name; }
public User() { }
public int getNo() { return no; }
public void setNo(int no) { this.no = no; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public String toString() { return "User{" + "no=" + no + ", name='" + name + '\'' + '}'; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.joker_yue.javalearn.IOLearn.ObjectOutputLearn;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.util.List;
public class ObjectInputStreamTest02 { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users"));
List<User> userList = (List<User>) ois.readObject();
for (User user : userList) { System.out.println(user);
}
ois.close(); } }
|
上述程序的输出结果为
1 2 3
| User{no=1, name='zhangsan'} User{no=2, name='lisi'} User{no=3, name='wangwu'}
|
transient关键字
当我们不希望某个属性被序列化的时候,我么可以加关键字transient(adj.游离的)关键字
1
| private transient String name;
|
IO和Properties联合使用
IO流:文件的读和写
Properties:是一个Map集合,key和value都是String类型
在后面讲反射机制的时候会讲
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.joker_yue.javalearn.IOLearn.IOProperties;
import java.io.FileReader; import java.io.IOException; import java.util.Properties;
public class IOPropertiesTest01 { public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("userinfo.properties");
Properties pro = new Properties();
pro.load(reader);
String username = pro.getProperty("username"); System.out.println(username);
String password = pro.getProperty("password"); System.out.println(password); } }
|
属性配置文件
1 2 3 4 5 6 7 8 9 10
| username=admin
password=123123
data = 100
admintoken:1213
|
上述代码的输出结果为