复制多级文件夹打印流字节打印流 PrintStream PrintStream ps = new PrintStream("文件路径") ps.write方法(会转码输出) ps.print方法(输出不变) ps.println方法(输出不变) package test01;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class Demo4 {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream("guoqingTest\\a.txt");
// 字节输出流方法
// ps.write(97);
//字节打印流特殊方法
ps.print(97);
ps.println();
ps.print(98);
}
}
字符打印流 PrintWriter在使用字符打印流的饿时候注意: 只有输出方法 PrintWriter pw = new PrintWriter(new PrintStream("guoqingTest\Copy.txt"), true);只有后面写了true, 才能自动刷新(且只作用于println方法,printf方法,printfarm方法)
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class Demo4 {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream("guoqingTest\\a.txt");
// 字节输出流方法
// ps.write(97);
//字节打印流特殊方法
ps.print(97);
ps.println();
ps.print(98);
}
}字符打印流复制文件
import java.io.*;
public class Demo5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("guoqingTest\\a.txt"));
PrintWriter pw = new PrintWriter(new PrintStream("guoqingTest\\Copy.txt"), true);
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
}
br.close();
pw.close();
}
}
import java.io.*;
public class Demo5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("guoqingTest\\a.txt"));
/* 打印输出流方法
PrintWriter pw = new PrintWriter(new PrintStream("guoqingTest\\Copy.txt"), true);
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
}
br.close();
pw.close();
*/
// 以下方法是字符流方法输出
BufferedWriter bw = new BufferedWriter(new FileWriter("guoqingTest\\Copy.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}对象序列化流对象序列化:就是将对象保存到磁盘中,或者在网络中传输对象.也就是说字节序列到文件中 反之就叫对象反序列化流. Serializable 接口 是实现序列化和反序列化必须实现后才能(且ArrayList自动承接Serializable) transient 修饰的成员变量就不会被序列化 例:private transient int age; Properties作为Map集合的特有方法
import java.util.Properties;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) {
Properties prop = new Properties();
// prop.put("01","lisa");
// prop.put("02","rose");
// prop.put("03","coco");
prop.setProperty("heina01","lisa");
prop.setProperty("heina02","rose");
prop.setProperty("heina03","coco");
//遍历数组
Set<Object> keySet = prop.keySet();
for (Object key: keySet){
String value = prop.getProperty((String)key);
System.out.println(key+","+value);
}
}
}Properties 和IO流结合的方法1.load()方法 2.store()方法
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Demo2 {
public static void main(String[] args) throws IOException {
// myStore();
myLoad();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
FileReader fr = new FileReader("guoqingTest\\fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("001","lisa");
prop.setProperty("002","rose");
prop.setProperty("003","coco");
FileWriter fw = new FileWriter("guoqingTest\\fw.txt");
prop.store(fw,null);
fw.close();
}
}
|