打印流以及Properties
字节打印流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.io.*; import java.nio.charset.StandardCharsets;
public class Test { public static void main(String[] args) throws IOException { PrintStream out=System.out; out.println("join,hello"); out.write("你好啊".getBytes(StandardCharsets.UTF_8)); System.setOut(new PrintStream("D:\\文件下载\\hello.txt")); System.out.println("hello"); out.close(); } }
|
字符打印流
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.io.*; import java.nio.charset.StandardCharsets;
public class Test { public static void main(String[] args) throws IOException { PrintWriter printWriter=new PrintWriter(new FileWriter("D:\\文件下载\\hello.txt")); printWriter.println("你好啊"); printWriter.close(); } }
|
Properties
传统方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.io.*;
public class Test { public static void main(String[] args) throws IOException { BufferedReader bufferedReader=new BufferedReader( new FileReader("src\\Information.properties")); String line; while ((line=bufferedReader.readLine())!=null){ String []split=line.split("="); System.out.println(split[0]+"的值"+split[1]); } bufferedReader.close(); } }
|
使用Properties类
专门用于读写配置文件的集合类,配置文件的格式:
键=值,键值对不需要有空格,值不需要用引号,默认为String类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.io.*; import java.util.Properties;
public class Test { public static void main(String[] args) throws IOException { Properties properties=new Properties(); properties.load(new FileReader("src\\Information.properties")); properties.list(System.out); String user= properties.getProperty("user"); System.out.println(user); properties.setProperty("password","55555"); properties.list(System.out); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.io.*; import java.util.Properties;
public class Test { public static void main(String[] args) throws IOException { Properties properties=new Properties(); properties.setProperty("password","55555"); properties.setProperty("user","zss"); properties.setProperty("id","11"); properties.store(new FileOutputStream("src\\test.properties"),"hello");
}
}
|
练习
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
| import java.io.*; import java.util.Properties;
public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException { Properties properties=new Properties(); properties.load(new FileReader("src\\test.properties")); Employee employee=new Employee(properties.getProperty("user"),Integer.parseInt(properties.getProperty("id")), properties.getProperty("password") ); System.out.println(employee); ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("D:\\文件下载\\test.dat")); objectOutputStream.writeObject(employee); objectOutputStream.close(); ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("D:\\文件下载\\test.dat")); Employee employee1=(Employee) objectInputStream.readObject(); System.out.println(employee1); objectInputStream.close();
} } class Employee implements Serializable{ String user; int id; String password;
public Employee(String user, int id, String password) { this.user = user; this.id = id; this.password = password; }
@Override public String toString() { return "Employee{" + "user='" + user + '\'' + ", id=" + id + ", password='" + password + '\'' + '}'; } }
|