1、打印流:只做输出,没有输入
打印流可以分为字节打印流和字符打印流
打印流的特点:
①可以打印各种数据类型;
②封装了字符输出流,还可以进行字符流和字节流的转换
③可以使用自动刷新
④可以直接向文件中写入数据
范例1:从文件中读取数据并且打印在控制台上
package com.rl.byt.print;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 从文件中读取数据并且打印在控制台上
* @author Administrator
*
*/
public class PrintDemo1 {
public static void main(String[] args) throws Exception {
//定义缓冲区输入流对象
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("student.txt"));
//pw = new PrintWriter(System.out);
//设置自动刷新的打印流
pw = new PrintWriter(System.out,true);
String line = null;
while((line = br.readLine()) !=null){
pw.print(line);
//pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pw != null){
pw.close();
}
if(br != null){
br.close();
}
}
}
}
范例2:使用打印流来复制文本文件
package com.rl.byt.print;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
* 使用打印流来复制文本文件
* @author Administrator
*
*/
public class PrintDemo2 {
public static void main(String[] args) throws Exception {
//定义缓冲区输入流对象
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("student.txt"));
//pw = new PrintWriter(System.out);
//设置自动刷新的打印流,读取
pw = new PrintWriter(new FileWriter("student1.txt"),true);
String line = null;
while((line = br.readLine()) !=null){
//写入
pw.print(line);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
package com.ri.byt.prop;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
/**
* 属性集合propertoes类的存取方法
* @author Administrator
*
*/
public class PropTest5 {
public static void main(String[] args) throws Exception {
//创建属性集类的对象,属性集类不支持泛型
Properties pp = new Properties();
pp.setProperty("name", "zhangsan");
pp.setProperty("age", "22");
pp.setProperty("gender", "male");
FileWriter fw = null;
try{
fw = new FileWriter("pp2.txt");
pp.store(fw, null);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fw != null){
fw.close();
}
}
}
}
if(pw != null){pw.close();}if(br != null){br.close();}}}}
2、Properties属性集合类
特点:
①集成于Hashtable,是线性安全的键对存储结构
②properties类可保存在流中或者从流中加载
③只能保存字符串的键对值
package com.ri.byt.prop;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;
public class PropTest1 {
public static void main(String[] args) {
//创建属性集类的对象,属性集类不支持泛型
Properties pp = new Properties();
pp.setProperty("name", "zhangsan");
pp.setProperty("age", "22");
pp.setProperty("gender", "male");
PrintWriter pw = null;
try{
pw = new PrintWriter(new FileWriter("pp.txt"),true);
//获得Hashtable类的键
Set keys = pp.keySet();
for(Object obj:keys){
//强转
String key =(String) obj;
//获得key对应的值
String value = pp.getProperty(key);
//写字符串
pw.println(key+"="+value);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(pw != null){
pw.close();
}
}
}
}
package com.ri.byt.prop;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;
public class PropTest2 {
public static void main(String[] args) {
//创建属性集类的对象,属性集类不支持泛型
Properties pp = new Properties();
pp.setProperty("name", "zhangsan");
pp.setProperty("age", "22");
pp.setProperty("gender", "male");
PrintWriter pw = null;
try{
pw = new PrintWriter(new FileWriter("pp1.txt"),true);
//将properties类的属性写入进流中
pp.list(pw);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(pw != null){
pw.close();
}
}
}
}
package com.ri.byt.prop;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* 属性集合propertoes类的存取方法
* @author Administrator
*
*/
public class PropTest3 {
public static void main(String[] args) throws Exception {
//创建属性集类的对象,属性集类不支持泛型
Properties pp = new Properties();
FileReader fr = null;
try{
fr = new FileReader("pp.txt");
//把字符流数据读到属性对象中
pp.load(fr);
//获得数据
String name = pp.getProperty("name");
String age = pp.getProperty("age");
String gender = pp.getProperty("gender");
System.out.println(name);
System.out.println(age);
System.out.println(gender);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fr != null){
fr.close();
}
}
}
}
package com.ri.byt.prop;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* 属性集合propertoes类的存取方法
* @author Administrator
*
*/
public class PropTest4 {
public static void main(String[] args) throws Exception {
//创建属性集类的对象,属性集类不支持泛型
Properties pp = new Properties();
FileReader fr = null;
try{
//实际项目中使用properties文件来存储数据,不使用txt文件
fr = new FileReader("pp2.properties");
//把字符流数据读到属性对象中
pp.load(fr);
//获得数据
String name = pp.getProperty("name");
String age = pp.getProperty("age");
String gender = pp.getProperty("gender");
System.out.println(name);
System.out.println(age);
System.out.println(gender);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fr != null){
fr.close();
}
}
}
}
package com.ri.byt.prop;
import java.util.Properties;
public class PropTest {
public static void main(String[] args) {
//创建属性集类的对象,属性集类不支持泛型
Properties pp = new Properties();
pp.setProperty("name", "zhangsan");
pp.setProperty("age", "22");
pp.setProperty("gender", "male");
System.out.println(pp);
String name = pp.getProperty("name");
String age = pp.getProperty("age");
String gender = pp.getProperty("gender");
System.out.println(name);
System.out.println(age);
System.out.println(gender);
}
}