java printfr_Java中I/O流之Print流

Java 中的 print 流:

print 流用于做输出将会非常的方便,并且具有以下特点:

1. printWriter、printStream 都属于输出流,分别针对字符,字节.

2. printWriter、printStream 提供了重载的 print 与 println 方法.

3.  print 与 println 能够用于多种数据类型的输出.

4. printWriter、printStream 的输出操作不会抛出异常,用户通过检测错误状态获取错误信息.

5. printWriter、printStream 都有自动 flush() 的功能.

6. print 流具有以下常用方法:

printWriter(Writer out)

printWriter(Writer out, boolean autoFlush)

printWriter(outputStream out)

printWriter(outputStream out,  boolean autoFlush)

printStream(outputStream out)

printStream(outputStream out,  boolean autoFlush)

Demo_1:

import java.io.*;

class Test {

public static void main(String[] args) {

try {

FileOutputStream fos = new FileOutputStream("E:/eclipse projiect/TEST0503.txt");

PrintStream ps = new PrintStream(fos);

if(ps!=null){

System.setOut(ps); // System.out 默认的是在命令行中输出

} // System.setOut 重新设置out的值

int ln = 0; // System.out 就是 printStream 类型

for(char c=0;c<=6000;c++){

System.out.println(c+" ");

ln++;

if(ln>=100){

System.out.println();

}

ln = 0;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

配图:

ac13715c67da1e583c7b46798c79eb17.png

Demo_2:

import java.io.*;

class Test {

public static void list(String fileName, PrintStream ps) {

try {

FileReader fr = new FileReader(fileName);

BufferedReader br = new BufferedReader(fr);

String s = null;

while((s=br.readLine())!=null){

ps.print(s);

System.out.println();

}

br.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e){

ps.println("无法读取文件");

}

}

public static void main(String[] args) {

String fileN = "E:/eclipse projiect/TEST.txt";

if(fileN != null) {

list(fileN, System.out);

}

}

}

运行结果:

ni hao: 中国0123

江城如画里,

山晚望晴空。

两水夹明镜,

双桥落彩虹。

人烟寒橘柚,

秋色老梧桐。

谁念北楼上,

临风怀谢公。

Demo_3:

import java.io.*;

import java.util.Date;

class Test {

public static void main(String[] args) {

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

try {

FileWriter fw = new FileWriter("E:/eclipse projiect/TEST0504.txt", true);

PrintWriter log = new PrintWriter(fw);

String s = null;

while((s=br.readLine())!=null){

if(s.equalsIgnoreCase("exit")){

break;

}

System.out.println(s.toUpperCase());

log.println("-----");

log.println(s.toUpperCase());

log.println("&&&&&"+ new Date()+"&&&&&");

log.flush(); // log 自带flush功能,但是写了比较好

}

log.println("&&&&&"+ new Date()+"&&&&&");

log.flush();

log.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

运行结果:

Eclipse 中的 console 中的内容如下:

wo men zhou mo qu na wan?

WO MEN ZHOU MO QU NA WAN?

qu chong qing ba ?

QU CHONG QING BA ?

hao ya

HAO YA

TEST0504.txt 文件中的内容如下:

-----

WO MEN ZHOU MO QU NA WAN?

&&&&&Wed May 03 15:18:30 CST 2017&&&&&

-----

QU CHONG QING BA ?

&&&&&Wed May 03 15:18:38 CST 2017&&&&&

-----

HAO YA

&&&&&Wed May 03 15:18:40 CST 2017&&&&&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值