public class IOTest { public static void main(String[] args) throws IOException { File file = new File("D:/test.txt"); write(file); System.out.println(read(file)); } public static void write(File file) throws IOException { OutputStream os = new FileOutputStream(file, true); // 要写入的字符串 String string = "松下问童子,言师采药去。只在此山中,云深不知处。"; // 写入文件 os.write(string.getBytes()); // 关闭流 os.close(); } public static String read(File file) throws IOException { InputStream in = new FileInputStream(file); // 一次性取多少个字节 byte[] bytes = new byte[1024]; // 用来接收读取的字节数组 StringBuilder sb = new StringBuilder(); // 读取到的字节数组长度,为-1时表示没有数据 int length = 0; // 循环取数据 while ((length = in.read(bytes)) != -1) { // 将读取的内容转换成字符串 sb.append(new String(bytes, 0, length)); } // 关闭流 in.close(); return sb.toString(); } }
public class IOTest { public static void write(File file) throws IOException { // 缓冲字节流,提高了效率 BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(file, true)); // 要写入的字符串 String string = "松下问童子,言师采药去。只在此山中,云深不知处。"; // 写入文件 bis.write(string.getBytes()); // 关闭流 bis.close(); } public static String read(File file) throws IOException { BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file)); // 一次性取多少个字节 byte[] bytes = new byte[1024]; // 用来接收读取的字节数组 StringBuilder sb = new StringBuilder(); // 读取到的字节数组长度,为-1时表示没有数据 int length = 0; // 循环取数据 while ((length = fis.read(bytes)) != -1) { // 将读取的内容转换成字符串 sb.append(new String(bytes, 0, length)); } // 关闭流 fis.close(); return sb.toString(); } }
public class IOTest { public static void write(File file) throws IOException { // OutputStreamWriter可以显示指定字符集,否则使用默认字符集 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"); // 要写入的字符串 String string = "松下问童子,言师采药去。只在此山中,云深不知处。"; osw.write(string); osw.close(); } public static String read(File file) throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8"); // 字符数组:一次读取多少个字符 char[] chars = new char[1024]; // 每次读取的字符数组先append到StringBuilder中 StringBuilder sb = new StringBuilder(); // 读取到的字符数组长度,为-1时表示没有数据 int length; // 循环取数据 while ((length = isr.read(chars)) != -1) { // 将读取的内容转换成字符串 sb.append(chars, 0, length); } // 关闭流 isr.close(); return sb.toString() } }
public class IOTest { public static void write(File file) throws IOException { FileWriter fw = new FileWriter(file, true); // 要写入的字符串 String string = "松下问童子,言师采药去。只在此山中,云深不知处。"; fw.write(string); fw.close(); } public static String read(File file) throws IOException { FileReader fr = new FileReader(file); // 一次性取多少个字节 char[] chars = new char[1024]; // 用来接收读取的字节数组 StringBuilder sb = new StringBuilder(); // 读取到的字节数组长度,为-1时表示没有数据 int length; // 循环取数据 while ((length = fr.read(chars)) != -1) { // 将读取的内容转换成字符串 sb.append(chars, 0, length); } // 关闭流 fr.close(); return sb.toString(); } }
public class IOTest { public static void write(File file) throws IOException { // BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new // FileOutputStream(file, true), "UTF-8")); // FileWriter可以大幅度简化代码 BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); // 要写入的字符串 String string = "松下问童子,言师采药去。只在此山中,云深不知处。"; bw.write(string); bw.close(); } public static String read(File file) throws IOException { BufferedReader br = new BufferedReader(new FileReader(file)); // 用来接收读取的字节数组 StringBuilder sb = new StringBuilder(); // 按行读数据 String line; // 循环取数据 while ((line = br.readLine()) != null) { // 将读取的内容转换成字符串 sb.append(line); } // 关闭流 br.close(); return sb.toString(); } }
public class File extends Object implements Serializable, Comparable<File>
public class FileTest { public static void main(String[] args) throws IOException { File file = new File("C:/Mu/fileTest.txt"); // 判断文件是否存在 if (!file.exists()) { // 不存在则创建 file.createNewFile(); } System.out.println("文件的绝对路径:" file.getAbsolutePath()); System.out.println("文件的大小:" file.length()); // 刪除文件 file.delete(); } }
public abstract class InputStream extends Object implements Closeable
public class MyTest { public static void main(String[] args) throws IOException { File file = new File("C:/Mu/test.txt"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 3000000; i ) { sb.append("abcdefghigklmnopqrstuvwsyz"); } byte[] bytes = sb.toString().getBytes(); long start = System.currentTimeMillis(); write(file, bytes); long end = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); bufferedWrite(file, bytes); long end2 = System.currentTimeMillis(); System.out.println("普通字节流耗时:" (end - start) " ms"); System.out.println("缓冲字节流耗时:" (end2 - start2) " ms"); } // 普通字节流 public static void write(File file, byte[] bytes) throws IOException { OutputStream os = new FileOutputStream(file); os.write(bytes); os.close(); } // 缓冲字节流 public static void bufferedWrite(File file, byte[] bytes) throws IOException { BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(file)); bo.write(bytes); bo.close(); } }
普通字节流耗时:250 ms 缓冲字节流耗时:268 ms
public synchronized void write(byte b[], int off, int len) throws IOException { if (len >= buf.length) { /* If the request length exceeds the size of the output buffer, flush the output buffer and then write the data directly. In this way buffered streams will cascade harmlessly. */ flushBuffer(); out.write(b, off, len); return; } if (len > buf.length - count) { flushBuffer(); } System.arraycopy(b, off, buf, count, len); count = len; }
public class MyTest { public static void main(String[] args) throws IOException { File data = new File("C:/Mu/data.zip"); File a = new File("C:/Mu/a.zip"); File b = new File("C:/Mu/b.zip"); StringBuilder sb = new StringBuilder(); long start = System.currentTimeMillis(); copy(data, a); long end = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); bufferedCopy(data, b); long end2 = System.currentTimeMillis(); System.out.println("普通字节流耗时:" (end - start) " ms"); System.out.println("缓冲字节流耗时:" (end2 - start2) " ms"); } // 普通字节流 public static void copy(File in, File out) throws IOException { // 封装数据源 InputStream is = new FileInputStream(in); // 封装目的地 OutputStream os = new FileOutputStream(out); int by = 0; while ((by = is.read()) != -1) { os.write(by); } is.close(); os.close(); } // 缓冲字节流 public static void bufferedCopy(File in, File out) throws IOException { // 封装数据源 BufferedInputStream bi = new BufferedInputStream(new FileInputStream(in)); // 封装目的地 BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(out)); int by = 0; while ((by = bi.read()) != -1) { bo.write(by); } bo.close(); bi.close(); } }
普通字节流耗时:184867 ms 缓冲字节流耗时:752 ms
public class IOTest { public static void main(String[] args) throws IOException { // 数据准备 dataReady(); File data = new File("C:/Mu/data.txt"); File a = new File("C:/Mu/a.txt"); File b = new File("C:/Mu/b.txt"); File c = new File("C:/Mu/c.txt"); long start = System.currentTimeMillis(); copy(data, a); long end = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); copyChars(data, b); long end2 = System.currentTimeMillis(); long start3 = System.currentTimeMillis(); bufferedCopy(data, c); long end3 = System.currentTimeMillis(); System.out.println("普通字节流1耗时:" (end - start) " ms,文件大小:" a.length() / 1024 " kb"); System.out.println("普通字节流2耗时:" (end2 - start2) " ms,文件大小:" b.length() / 1024 " kb"); System.out.println("缓冲字节流耗时:" (end3 - start3) " ms,文件大小:" c.length() / 1024 " kb"); } // 普通字符流不使用数组 public static void copy(File in, File out) throws IOException { Reader reader = new FileReader(in); Writer writer = new FileWriter(out); int ch = 0; while ((ch = reader.read()) != -1) { writer.write((char) ch); } reader.close(); writer.close(); } // 普通字符流使用字符流 public static void copyChars(File in, File out) throws IOException { Reader reader = new FileReader(in); Writer writer = new FileWriter(out); char[] chs = new char[1024]; while ((reader.read(chs)) != -1) { writer.write(chs); } reader.close(); writer.close(); } // 缓冲字符流 public static void bufferedCopy(File in, File out) throws IOException { BufferedReader br = new BufferedReader(new FileReader(in)); BufferedWriter bw = new BufferedWriter(new FileWriter(out)); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } // 释放资源 bw.close(); br.close(); } // 数据准备 public static void dataReady() throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 600000; i ) { sb.append("abcdefghijklmnopqrstuvwxyz"); } OutputStream os = new FileOutputStream(new File("C:/Mu/data.txt")); os.write(sb.toString().getBytes()); os.close(); System.out.println("完毕"); } }
普通字符流1耗时:1337 ms,文件大小:15234 kb 普通字符流2耗时:82 ms,文件大小:15235 kb 缓冲字符流耗时:205 ms,文件大小:15234 kb