System类的两个字段:
in(System.in)
out(System.out)
键盘录入新方法
package _13.homework;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Demo1 {
public static void main(String[] args) throws IOException {
InputStream is = System.in;//创建字节输入流对象
InputStreamReader isr = new InputStreamReader(is) ;//将对象转换成字符串输入流
BufferedReader br=new BufferedReader(isr);
System.out.print("请您输入一个字符串: ");
String line=br.readLine();
System.out.println("您输入的字符串是: "+line);
System.out.println();
System.out.print("请您输入一个整数: ");
String line1=br.readLine();
Integer i=Integer.parseInt(line1);
System.out.println("您输入的整数是: "+i);
}
}
输出流:
代码如下:
package _13.homework;
import java.io.PrintStream;
public class Demo1 {
public static void main(String[] args) {
PrintStream ps=System.out;
ps.println("我");
ps.println("爱");
ps.println("睡");
ps.println("觉");
}
}
打印流:
字节打印流:PrintStream
字符打印流:PrintWriter
构造方法:public PrintWriter(String fileName)
package _13.homework;
import java.io.IOException;
import java.io.PrintWriter;
public class Demo1 {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("F:\\a.txt");
pw.write("老鼠过街,人人喊打");
pw.write("仙女的裙子——拖拖拉拉");
pw.flush();
pw.close();
}
}
PrintWriter的特有功能:
自动刷新功能:public PrintWriter(Writer out,boolean autoFlush)
相当于:flush();
newLine();
write(“”);
具体代码及用法见下列代码:
package _13.homework;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Demo1 {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("F:\\a.txt"), true);
pw.println("直");
pw.println("面");
pw.println("未");
pw.println("来");
pw.println("!");
pw.close();
}
}