打印字节流
public class printDemo1 {
public static void main(String[] args) throws Exception {
PrintStream pst = new PrintStream("CertExam\\src\\pstTest01.txt");
/**
* this(false, new FileOutputStream(fileName));
* this内部如下
* super(out);
* this.autoFlush = autoFlush;
* this.charset = out instanceof PrintStream ps ? ps.charset() : Charset.defaultCharset();
* this.charOut = new OutputStreamWriter(this, charset);
* this.textOut = new BufferedWriter(charOut);
*/
pst.println(100);
pst.println('a');
pst.println('@');
pst.println(false);
pst.println(3333.3);
pst.println("在文件中我可以输出中文");
pst.close();
}
}
打印字符流
public class printDemo2 {
public static void main(String[] args) throws Exception {
PrintWriter pwr = new PrintWriter("CertExam\\src\\pstTest01.txt");
/**
* this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),false);
* 结构明了,缓冲->转换->原始字节流
* FileWriter fw = new FileWriter("CertExam\\src\\pstTest01.txt");
* 其实字符流内部也是用转换流包了一条字节流
* 看来字节打印流PrintStream和字符打印流PrintWriter几乎没区别,打出去的东西完全一样
*/
pwr.println(99);
pwr.println('b');
pwr.println("#");
pwr.println(true);
pwr.println(111.1);
pwr.println("文件里中文输出没问题");
pwr.close();
}
}
properties的set和store
public class PropertiesDemo1 {
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
/**
* Map集合,在属性集合这里,put和 setProperties的差别一
* put的key,和value可以是任何object,而setProperty的键值都必须是String
* 差别二
* hashmap的value参数可以设置空值null,而当setProperty的value参数为null时,会抛出空指针异常NullPointerException
*/
pro.setProperty("leon","12345");
pro.setProperty("levis","23456");
pro.setProperty("louis","34567");
System.out.println(pro);
// Map<String,Integer> imap = new HashMap<>();
// imap.put("lily",0);
/**
* 储存时要new一个输出流,有多种选项,就用字符输出流
*/
pro.store(new FileWriter("CertExam\\src\\Users.properties.txt"),
"This is a first properties file");
}
}
load
public class PropertiesDemo2 {
public static void main(String[] args) throws IOException {
/**配置文件properties的读取还是很方便的*/
Properties pr = new Properties();
pr.load(new FileReader("CertExam\\src\\Users.properties.txt"));
System.out.println(pr);
}
}