I/O实操之扩展
打印流分为PrintStream 和PrintWriter
PrintStream :针对字节 调用println 内容中有换行符等 自动调用flush方法(自动发送缓冲区的内容到文件中)
PrintWriter:针对字符 只有在调用println 才会 自动调用flush方法(自动发送缓冲区的内容到文件中)
//System.out.println() 打印到屏幕(标准输出设备) 建立在 PrintStream基础上
File file=new File("d:/aa/1.txt");
try {
PrintWriter printWriter=new PrintWriter(file);
printWriter.println("hello");
printWriter.println("world");
printWriter.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
properties
Properties是一个属性类,也许它可能在一些初级项目中很少运用,但是Properties文件,在许多项目中会经常运用,它使得我们的配置属性的独立化,方便项目后期的配置属性的更改。
Properties properties=new Properties();
try {
//默认的路径是当前项目根目录
FileReader fileReader=new FileReader("db.properties");
properties.load(fileReader);
System.out.println(properties.getProperty("uid"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

浙公网安备 33010602011771号