PrintWriter
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class PrintWriterDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//讀取鍵盤錄入。將錄入的數(shù)據(jù)轉成大寫保存到文件中。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out,true);//true自動刷新,對println有效。
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line)){
break;
}
out.println(line.toUpperCase());
// out.flush();
}
out.close();
//想要將數(shù)據(jù)打印到文件中,并使用自動刷新。
//PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);
}
}
PrintStream
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class PrintStreamDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//需求:希望寫一個整數(shù),到目的地整數(shù)的表現(xiàn)形式不變。可以將整數(shù)轉成字符串在寫入到目的地。
// FileOutputStream fos = new FileOutputStream("tempfile/int.txt");
// fos.write(String.valueOf(97));//字節(jié)流的write方法只將一個整數(shù)的最低字節(jié)寫入到目的地;//00000000 00000000 00000001 01100001
// fos.close();
// FileOutputStream fos = new FileOutputStream("tempfile/int.txt");
// //需要額外功能嗎?保證數(shù)據(jù)值的表示形式。需要。
// PrintStream ps = new PrintStream(fos);
//// ps.write(97);// 只能寫入最低字節(jié)。
// ps.print(97);//將數(shù)據(jù)轉成字符串在寫入。保證數(shù)據(jù)值的表現(xiàn)形式。
// ps.close();
PrintStream ps = new PrintStream("tempfile/int.txt");
ps.print(98);
ps.close();
}
}