基本数据类型流
可以读写基本数据类型
1.数据输入流:DateInputStream
DateInputStream(InputStream in)
2.数据输入流:DateOutputSteam
DateOutputStream(OutputStream out)
特点:
- 该流是一个字节流,可以读写字节的同事,还能够读写基本数据类型
- 通过数据类型输出流写入到文件中,使用文本文件打开是不能阅读,
- 读的时候必须和写的顺序保持一致,提高了基本数据类型在文件中保存的安全性
举个例子:
package LXT;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
public class lxt1 {
public static void main(String[] args) throws Exception {
copy2("dos,txt", "copy.txt");
copy3("dos.txt", "copy,txt");
}
private static void copy3(String srcFileName,String descFileName) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFileName));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(descFileName));
int len = 0;
byte[] bys = new byte[1024];
while ((len = bis.read(bys)) != -1) {
bos.write(bys,0,len);
}
}
private static void copy2(String srcFileName,String descFileName) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(srcFileName));
BufferedWriter bw = new BufferedWriter(new FileWriter(descFileName));
int len = 0;
char[] chs = new char[1024];
while ((len = br.read(chs)) != -1) {
bw.write(chs,0,len);
bw.flush();
}
}
}
万能输入流
/*
* PrintWriter PrintStream
概述:向文本输出流打印对象的格式化表示形式。此类实现在 PrintStream 中的所有 print 方法。
特点:
只能写数据,不能读取数据。
可以操作任意类型的数据。
如果启动了自动刷新,能够自动刷新。
如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作
代码演示打印流复制文件内容
*/
public class IODemo02 {
public static void main(String[] args) throws Exception {
// PrintWriter pw = new PrintWriter("pw.txt");
PrintWriter pw = new PrintWriter(new FileWriter("pw.txt"), true);
pw.write("abc");
pw.print(2.5);
pw.println("我换行了");
pw.print("efg");
// pw.flush();
// pw.close();
}
}
/*
* 1、模拟记录用户登录日志(采用追加方式记录),
从控制台接收用户输入的用户名和密码,在文件中记录用户名和密码以及登录时间。
格式如下:
name=zhangsan
pwd=123456
时间=2014-06-05 18:22:33
*/
public class Practice07 {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String userName = input.next();
System.out.println("请输入密码:");
String password = input.next();
System.out.println("是否登录?(Y/N)");
String yesOrNo = input.next();
if ("Y".equals(yesOrNo)) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateStr = sdf.format(date);
PrintWriter pw = new PrintWriter(new FileWriter("userInfo.txt"), true);
pw.println("name=" + userName);
pw.println("pwd=" + password);
pw.print("时间=" + dateStr);
pw.close();
System.out.println("登录成功");
} else {
System.out.println("登录失败");
}
}
}
输入输出流
/*
* System类中的静态变量:in,out 。
“标准”输入流:static InputStream in 。
“标准”输出流:static PrintStream out 。
它们各代表了系统标准的输入和输出设备。
默认输入设备是键盘,输出设备是显示器。
*/
public class IODemo03 {
public static void main(String[] args) throws IOException {
// InputStream in = System.in;
//
// int b = in.read();
//
// System.out.println(b);
PrintStream ps = System.out;
ps.println("Hello");
ps.println("World");
System.out.println("HeloWorld");
}
}
/*
* 请自己使用IO流编模拟Scanner键盘录入对象
*/
public class IODemo04 {
public static void main(String[] args) throws IOException {
// InputStream is = System.in;
//
// System.out.println("请输入一个字节:");
// int b = 0;
// b = is.read(); // 这个read()方法是一个阻塞方法,会阻塞main方法,等待用户输入
// System.out.println((char)b);
//
// b = is.read(); // 这个read()方法是一个阻塞方法,会阻塞main方法,等待用户输入
// System.out.println((char)b);
// Scanner input = new Scanner(System.in);
// System.out.println("请输入字符串:");
// String line = input.next();
// System.out.println(line);
// System.out.println("请输入小数:");
// double d = input.nextDouble();
// System.out.println(d);
MyScanner input = new MyScanner(System.in);
System.out.println("请输入一个字符串:");
String line = input.next();
System.out.println(line);
System.out.println("请输入一个小数");
double d = input.nextDouble();
System.out.println("您输入的小数是:" + d);
}
}
class MyScanner{
private InputStream is; // System.in
private BufferedReader br;
public MyScanner() {}
public MyScanner(InputStream is) {
this.is = is;
}
public String next() throws IOException {
return read();
}
public double nextDouble() throws IOException {
double d = 0.0;
try {
d = Double.parseDouble(read());
} catch (NumberFormatException nfe) {
throw new InputMismatchException("哥们,你这要输入小数啊!!!一看你就不严谨!!!");
}
return d;
}
public String read() throws IOException {
InputStreamReader isr = new InputStreamReader(is);
String line = null;
br = new BufferedReader(isr);
line = br.readLine();
return line;
}
}