Print流

                Print流:PrintWrite和PrintStream都属于输出流,分别针对字符和字节、PrintWrite和PrintStream提供了重载的print,println方法用于多种数据类型的输出、PrintWrite和PrintStream的输出操作不会抛异常,用户通过检测错误状态获取错误信息、PrintWrite和PrintStream有自动flush功能
import java.io.*;

public class TestPrintStream1 {
	public static void main(String[] args) {
		PrintStream ps = null;
		try {
			FileOutputStream fos = new FileOutputStream("d:\\bak\\log.dat");
			ps = new PrintStream(fos); // 套接到字节输出流
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (ps != null) {
			System.setOut(ps);  //设置文件输出路径
		}
		int ln = 0;
		for (char c = 0; c <= 60000; c++) {
			System.out.print(c + " ");
			if (ln++ >= 100) {
				System.out.println();
				ln = 0;
			}
		}
	}
}

import java.io.*;

public class TestPrintStream2 {
	public static void main(String[] args) {
		String filename = args[0];   //传入文件路径和文件名
		if (filename != null) {
			list(filename, System.out);//根据文件路径和文件名输出文件内容
		}
	}

	public static void list(String f, PrintStream fs) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(f));
			String s = null;
			while ((s = br.readLine()) != null) {
				fs.println(s);
			}
			br.close();
		} catch (IOException e) {
			fs.println("无法读取文件");
		}
	}
}

import java.util.*;
import java.io.*;

public class TestPrintStream3 {
	public static void main(String[] args) {
		String s = null;      //系统命令行读入
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			FileWriter fw = new FileWriter("d:\\bak\\logfile.log", true); // 写日志,在文件后追加
			PrintWriter log = new PrintWriter(fw);
			while ((s = br.readLine()) != null) {
				if (s.equalsIgnoreCase("exit"))
					break;
				System.out.println(s.toUpperCase());
				log.println("-----");
				log.println(s.toUpperCase());
				log.flush();
			}
			log.println("===" + new Date() + "===");
			log.flush();
			log.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,C++,c,C#,F#)