输入流:
从文件输入到内存,即:读文件输出流:从内存输出到文件,即:写文件 一 节点流(进行文件读写的基本流)1 字符流(以字符char为单位进行读写)
1.1 抽象父类 Reader Writer 1.2 针对文件的字符流 FileReader read() read(char[]) FileWriter write() write(char[]) write(String)2 字节流(以字节byte为单位进行读写) 2.1 抽象父类 InputStream OutputStream 2.2 针对文件的字节流 FileInputStream read() read(byte[]) FileOutputStream write(int) write(byte[])二 处理流(对节点流的包装,使文件读写更快速更方便) 2.1 缓冲流(效率高) 字符缓冲流 BufferedReader readLine() BufferedWriter newLine() 字节缓冲流 BufferedInputStream read(byte[],int,int) BufferedOutputStream write(byte[],int,int) 2.2 打印流(格式化) PrintWrite print(String) println(String) PrintStream print(String) println(String) 2.3 Scanner next() nextInt() nextDouble() 2.4 数据流 DataInputStream DataOutputStream 2.5 对象流(要操作的对象的类必须实现Serializable接口) ObjectOutputStream ObjectInputStream练习:1 复制文件 从a.txt复制出b.txt 注意:怎么判断文件读到结尾2 把歌词一句一句打印出来3 循环输入用户信息,保存到文件 模拟登陆:键盘输入用户名和密码,检查用户名密码是否正确import java.io.File;import java.io.IOException;public class TestFile { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("a.txt"); System.out.println("exists=" + file.exists()); file.createNewFile();//创建file对象对应的文件 System.out.println("exists=" + file.exists()); System.out.println("dir=" + file.isDirectory()); System.out.println("file=" + file.isFile()); }}
import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class Testreaderwriter { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //1 流对象 FileReader fr = new FileReader("a.txt"); //2 读取字符 char c = (char)fr.read(); System.out.println(c); //3 关闭 fr.close(); FileWriter fw = new FileWriter("a.txt",true); //2 写入字符a fw.write('a'); fw.write("bcdefg"); //3 关闭 fw.close(); }}
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Arrays;public class TestFileStream { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //读文件,输入流 //1 搭建输入流 //FileInputStream fis1 = new FileInputStream("a.txt"); File file = new File("a.txt"); File file1 = new File("b.txt"); //如果是文件,就进行读写 if(file.isFile()) { FileInputStream fis2 = new FileInputStream(file); //2 从流中读取数据 byte b = (byte)fis2.read(); byte b1 = (byte)fis2.read(); byte[] bs = new byte[5]; fis2.read(bs); System.out.println("b = " + b + " b1 = " + b1); System.out.println("bs = " + Arrays.toString(bs)); //3 关闭流 fis2.close(); //1 构建文件输出流 FileOutputStream fos2 = new FileOutputStream(file1); //2 从流中读取数据 fos2.write(55); fos2.write(56); fos2.write(bs); //3 关闭流 fos2.close(); } }}
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class TestBuffered { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //缓冲流 //1 节点流 FileWriter fw = new FileWriter("c.txt"); //2 处理流包装节点流 BufferedWriter bw = new BufferedWriter(fw); bw.write("这是第一行"); bw.newLine(); bw.write("这是第二行"); bw.newLine(); bw.close(); //1 节点流 FileReader fr = new FileReader("c.txt"); //2 处理流包装节点流 BufferedReader br = new BufferedReader(fr); System.out.println(br.readLine()); System.out.println(br.readLine()); br.close(); }}
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInput;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class TestObjectStream { /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub FileOutputStream fileOutputStream = new FileOutputStream("b.txt"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(new User("张三","123456")); objectOutputStream.close(); FileInputStream fileInputStream = new FileInputStream("b.txt"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); User user = (User)objectInputStream.readObject(); System.out.println(user); objectInputStream.close(); }}class User implements Serializable{ private String nameString;//用户名 private String pswdString;//密码 public User(String nameString,String pswdString) { this.nameString = nameString; this.pswdString = pswdString; } public String toString() { return "用户名:" + nameString + " 密码:" + pswdString; }}
import java.io.DataInputStream;import java.io.DataOutput;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class TestDataStream { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub FileOutputStream fileOutputStream = new FileOutputStream("a.txt"); DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream); dataOutputStream.writeInt(100); dataOutputStream.writeBoolean(true); dataOutputStream.writeDouble(100.11); dataOutputStream.close(); FileInputStream fileInputStream = new FileInputStream("a.txt"); DataInputStream dataInputStream = new DataInputStream(fileInputStream); int i = dataInputStream.readInt(); boolean b = dataInputStream.readBoolean(); Double d = dataInputStream.readDouble(); System.out.println(i + "," + b + "," + d); dataInputStream.close(); }}