import java.io.*;
public class PrintStreamDemo01 {
public static void main(String args[]) throws IOException
{
File file=new File("d:"+File.separator+"demo.txt");
PrintStream out=new PrintStream(new FileOutputStream(file));
out.println("hello");
out.println("world");
out.println(20.1);
out.close();
}
}
打印流可以设置格式,提供以下的方法:
public PrintStream printf(String format,Object ...args);可以设置格式和多个参数
import java.io.*;
public class PrintStreamDemo01 {
public static void main(String args[]) throws IOException
{
File file=new File("d:"+File.separator+"demo.txt");
PrintStream out=new PrintStream(new FileOutputStream(file));
// out.println("hello");
// out.println("world");
// out.println(20.1);
String name="fjsklafsd";
int age=3;
float score=99.9f;
char c='m';
//格式化输出
out.printf("姓名:%s;年龄:%d;成绩:%5.2f;性别:%c;", name,age,score,c);
out.close();
}
}
在打印流中,根据实例化其子类的不同,完成的打印输出功能也不同