JAVA IO - 格式化输出流

The PrintStream class enables you to write formatted data to an underlying OutputStream. For instance, writing intlong and other primtive data formatted as text, rather than as their byte values. java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class PrintStreamTest {
	public static void main(String args[]) {
		try {
			OutputStream out = new FileOutputStream("helloworld.txt");
			
			PrintStream ps = new PrintStream(out);
			ps.printf("This is my %s Test", "PrintStream");
			ps.close();
			
			InputStream in = new FileInputStream("helloworld.txt");
			byte[] bs = new byte[1024];
			int len = -1;
			if((len = in.read(bs)) != -1){
				System.out.println(new String(bs, 0, len));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
相关文章
相关标签/搜索