@Test public void testFileReader() { // 1.创建文件 File file = new File("test.txt"); FileReader fileReader = null; try { // 2.创建输入流 fileReader = new FileReader(file); // 3.读取输入 int c; while ((c = fileReader.read()) != -1) { System.out.print((char) c); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.关闭资源 if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testFileReader1() { // 1.创建文件 File file = new File("test.txt"); FileReader fileReader = null; try { // 2.创建输入流 fileReader = new FileReader(file); // 3.读取输入 char[] buffer = new char[5]; int len; while ((len = fileReader.read(buffer)) != -1) { System.out.print(new String(buffer, 0, len)); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.关闭资源 if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
构造方法:
注意事项:
@Test public void testFileWriter() { FileWriter fileWriter = null; try { // 1.创建文件 File file = new File("test1.txt"); // 2.创建输出流 fileWriter = new FileWriter(file, false); // 3.写入 fileWriter.write("Hello World!\n"); fileWriter.write("我爱北京天安门\n"); } catch (IOException e) { e.printStackTrace(); } finally { // 4.关闭资源 if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * 使用FileInputStream和FileOutputStream实现通用的文件拷贝 */ public void fileCopy(String src, String dest) { FileInputStream in = null; FileOutputStream out = null; try { File srcFile = new File(src); File destFile = new File(dest); in = new FileInputStream(srcFile); out = new FileOutputStream(destFile, false); byte[] buffer = new byte[1024]; int len; while((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组,缺省使用8192个字节或字符的缓冲区
public void fileCopy(String src, String dest) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); in.close(); }
注意点:
public void testBufferedReaderWriter() throws IOException { BufferedReader reader = new BufferedReader(new FileReader("test.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("test(1).txt")); // 方式1: // char[] cbuf = new char[10]; // int len; // while ((len = reader.read(cbuf)) != -1) { // writer.write(cbuf, 0, len); // } // 方式2: String line; while ((line = reader.readLine()) != null) { writer.write(line); writer.newLine(); } writer.close(); reader.close(); }
转换流提供了字节流和字符流之间的转换,常用来时间文件编码的转换:
public void utf8ToGbk() throws IOException { InputStreamReader reader = new InputStreamReader(new FileInputStream("test.txt"), "UTF-8"); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("test_gbk.txt"), "GBK"); char[] cbuf = new char[10]; int len; while ((len = reader.read(cbuf)) != -1) { writer.write(cbuf, 0, len); } reader.close(); writer.close(); }
/** * 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。 * 然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。 */ public static void main(String[] args) throws IOException { // 为了借助BufferedReader的readLine()方法,这里先把System.in(InputStream)转为转换流,又转为了缓冲流 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = reader.readLine()) != null) { if (!line.equals("e") && !line.equals("exit")) { System.out.println(line); } else { break; } } reader.close(); }
打印流:PrintStream和PrintWriter
public void testPrintStream() throws FileNotFoundException { PrintStream out = new PrintStream(new FileOutputStream("test11.txt")); System.setOut(out); for (int i = 0; i <= 255; i++) { System.out.print((char) i); if (i % 50 == 0) { System.out.println(); } } out.close(); }
数据流有两个类:(用于读取和写出基本数据类型、String类的数据)DataInputStream 和 DataOutputStream
/** * 数据流:写入 */ @Test public void testDataOutputStream() throws IOException { DataOutputStream out = new DataOutputStream(new FileOutputStream("test.dat")); out.writeUTF("张三"); out.writeInt(30); out.writeBoolean(true); out.close(); } /** * 数据流:读取 */ @Test public void testDataInputStream() throws IOException { DataInputStream in = new DataInputStream(new FileInputStream("test.dat")); String name = in.readUTF(); int age = in.readInt(); boolean isAlive = in.readBoolean(); System.out.println("name:" + name); System.out.println("age:" + age); System.out.println("isAlive:" + isAlive); in.close(); }
可序列化对象的要求:
/** * 对象序列化与反序列化 */ public class IOTest7 { /** * 对象序列化 */ @Test public void testObjectOutputStream() throws IOException { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.obj")); out.writeObject("我爱你中国"); Person person = new Person("张三", 30, true); out.writeObject(person); out.close(); } /** * 对象反序列化 */ @Test public void testObjectInputStream() throws IOException, ClassNotFoundException { ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.obj")); String str = (String) in.readObject(); System.out.println(str); Person person = (Person) in.readObject(); System.out.println(person); in.close(); } } class Person implements Serializable { public static final long serialVersionUID = 20211021L; private String name; private int age; private boolean isAlive; public Person(String name, int age, boolean isAlive) { this.name = name; this.age = age; this.isAlive = isAlive; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", isAlive=" + isAlive + '}'; } }