I/O是什么

I/O 是Input/Output(输入、输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出。

流是一个连续的数据流,可以从流中读取数据,也可以往流中写数据。流与数据源,或数据源流向的媒介相关联。

在Java IO流中,流可以是字节流,也可以是字符流。

Java I/O 用途与对应的流一览



注:粗体为节点流。蓝色为转换流(字节流转为字符流)。

流的处理

流分节点流和处理流两种。

  • 节点流:可以从或向一个特定的地方(节点)读写数据。如FileInputStream、FileReader。

  • 处理流:是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写。如BufferedReader.处理流的构造方法总是要带一个其他的流对象做参数。一个流对象经过其他流的多次包装,称为流的链接

文件访问

读取文件####

如果你需要在不同端使用读取文件,你可以根据你要读的文件是二进制文件还是文本文件,或者根据你要处理的数据是准备采取字节方式还是字符方式,决定使用 FileInputStream 或者 FileReader。两者支持你从文件开头开始到文件结尾读取一个字节或者字符,也可以将读取的多个字节或字符,写入到内存的字节数组或字符数组。

单字节读取文件示例:

  1. public static void readFileAsByte() throws IOException {
  2. String filepath = "file.bin";
  3. java.io.InputStream is = null;
  4. try {
  5. is = new FileInputStream(filepath);
  6. int data = -1;
  7. while ((data = is.read()) != -1) {// -1 表示读取到达文件结尾
  8. //操作数据
  9. System.out.print((byte)data + " ");
  10. }
  11. } finally {
  12. if (is != null) {
  13. is.close();// 关闭流
  14. }
  15. }
  16. }

字节数组读取文件示例:

  1. public static void readFileAsByteArray() throws IOException {
  2. String filepath = "file.bin";
  3. java.io.InputStream is = null;
  4. try {
  5. is = new BufferedInputStream(new FileInputStream(filepath));// 组装BufferedInputStream流,加入缓冲能力
  6. byte[] data = new byte[256];
  7. int len = -1;
  8. while ((len = is.read(data)) != -1) {// -1 表示读取到达文件结尾
  9. //操作数据
  10. for (int i = 0; i < len; i++) {
  11. System.out.print(data[i] + " ");
  12. }
  13. }
  14. } finally {
  15. if (is != null) {
  16. is.close();// 关闭流
  17. }
  18. }
  19. }

单字符读取文件示例:

  1. public static void readFileAsChar() throws IOException {
  2. String filepath = "file.txt";
  3. java.io.Reader r = null;
  4. try {
  5. r = new FileReader(filepath);
  6. int data = -1;
  7. while ((data = r.read()) != -1) {// -1 表示读取到达文件结尾
  8. //操作数据
  9. System.out.print((char) data);
  10. }
  11. } finally {
  12. if (r != null) {
  13. r.close();// 关闭流
  14. }
  15. }
  16. }

字符数组读取文件示例:

  1. public static void readFileAsCharArray() throws IOException {
  2. String filepath = "file.txt";
  3. java.io.Reader r = null;
  4. try {
  5. r = new BufferedReader(new FileReader(filepath));// 组装BufferedReader流,加入缓冲能力
  6. char[] data = new char[256];
  7. int len = -1;
  8. while ((len = r.read(data)) != -1) {// -1 表示读取到达文件结尾
  9. //操作数据
  10. for (int i = 0; i < len; i++) {
  11. System.out.print(data[i]);
  12. }
  13. }
  14. } finally {
  15. if (r != null) {
  16. r.close();// 关闭流
  17. }
  18. }
  19. }

写入文件####

与读取文件类似:

如果你需要在不同端使用写入文件,你可以根据你要写的文件是二进制文件还是文本文件,或者根据你要处理的数据是准备采取字节方式还是字符方式,决定使用 FileOutputStream 或者 FileWriter。两者支持你可以一次写入一个字节或者字符到文件中,也可以直接写入一个字节数组或者字符数据。数据按照写入的顺序存储在文件当中。

单字节写入文件示例:

  1. public static void writeFileAsByte() throws IOException {
  2. String filepath = "file.bin";
  3. java.io.OutputStream os = null;
  4. try {
  5. os = new FileOutputStream(filepath);
  6. os.write('1');
  7. os.write('2');
  8. os.write('3');
  9. os.write('4');
  10. os.flush();// 把缓冲区内的数据刷新到磁盘
  11. } finally {
  12. if (os != null) {
  13. os.close();// 关闭流
  14. }
  15. }
  16. }

字节数组写入文件示例:

  1. public static void writeFileAsByteArray() throws IOException {
  2. String filepath = "file.bin";
  3. java.io.OutputStream os = null;
  4. try {
  5. os = new BufferedOutputStream(new FileOutputStream(filepath));
  6. // 模拟
  7. byte[] data = new byte[256];
  8. new Random().nextBytes(data);
  9. os.write(data);
  10. os.flush();// 把缓冲区内的数据刷新到磁盘
  11. } finally {
  12. if (os != null) {
  13. os.close();// 关闭流
  14. }
  15. }
  16. }

单字符写入文件示例:

  1. public static void writeFileAsChar() throws IOException {
  2. String filepath = "file.txt";
  3. java.io.Writer w = null;
  4. try {
  5. w = new FileWriter(filepath);
  6. w.write('1');
  7. w.write('2');
  8. w.write('3');
  9. w.write('4');
  10. w.flush();// 把缓冲区内的数据刷新到磁盘
  11. } finally {
  12. if (w != null) {
  13. w.close();// 关闭流
  14. }
  15. }
  16. }

字符数组写入文件示例:

  1. public static void writeFileAsCharArray() throws IOException {
  2. String filepath = "file.txt";
  3. java.io.Writer w = null;
  4. try {
  5. w = new BufferedWriter(new FileWriter(filepath));// 组装BufferedWriter流,加入缓冲能力
  6. // 模拟
  7. char[] data = new char[256];
  8. String f = "0123456789abcdefghijklmnopqrstuvwxyz";
  9. Random rd = new Random();
  10. for (int i = 0; i < data.length; i++) {
  11. data[i] = f.charAt(rd.nextInt(f.length()));
  12. }
  13. w.write(data);
  14. w.flush();// 把缓冲区内的数据刷新到磁盘
  15. } finally {
  16. if (w != null) {
  17. w.close();// 关闭流
  18. }
  19. }
  20. }

随机访问文件####

如果你需要不按特定的存取顺序,随意读取或者写入文件,可以考虑RandomAccessFile。

void seek(long pos) 设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。

简单示例:

  1. public static void main(String[] args) throws IOException {
  2. RandomAccessFile file = null;
  3. try {
  4. file = new java.io.RandomAccessFile("file.bin", "rw");
  5. file.seek(0);
  6. file.writeChar('1');
  7. file.seek(0);
  8. System.out.println(file.readChar());
  9. /**
  10. * 读取
  11. */
  12. int data = -1;
  13. while ((data = file.read()) != -1) {// -1 表示读取到达文件结尾
  14. //操作数据
  15. System.out.print((byte)data + " ");
  16. }
  17. } finally {
  18. if (file != null) {
  19. file.close();// 关闭流
  20. }
  21. }
  22. }

管道(线程内存)

管道为同一JVM中运行的线程提供基于内存的通信机制。但是你不能利用管道在不同的JVM中的线程间通信。

在概念上,Java的管道不同于Unix/Linux系统中的管道。在Unix/Linux中,运行在不同地址空间的两个进程可以通过管道通信。在Java中,通信的双方应该是运行在同一进程中的不同线程。当然除了管道之外,一个JVM中不同线程之间还有许多通信的方式。实际上,线程在大多数情况下会传递完整的对象信息而非原始的字节数据。但是,如果你需要在线程之间传递字节数据,Java IO的管道是一个不错的选择。

当使用两个相关联的管道流时,务必将它们分配给不同的线程。read()方法和write()方法调用时会导致流阻塞,这意味着如果你尝试在一个线程中同时进行读和写,可能会导致线程死锁。

简单示例:

  1. static class Input implements Runnable {
  2. private final PipedInputStream inputStream = new PipedInputStream();
  3. public Input() {
  4. }
  5. public PipedInputStream getInputStream() {
  6. return inputStream;
  7. }
  8. @Override
  9. public void run() {
  10. try {
  11. byte[] buf = new byte[1024];
  12. int len = -1;
  13. System.out.println("管道读取准备。");
  14. StringBuffer result = new StringBuffer();
  15. while ((len = inputStream.read(buf)) > 0) {
  16. //System.out.println(new String(buf, 0, len));
  17. result.append(new String(buf, 0, len));
  18. }
  19. System.out.println("管道读取结果:" + result.toString());
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. } finally {
  23. try {
  24. if (inputStream != null)
  25. inputStream.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. }
  32. static class Output implements Runnable {
  33. private final PipedOutputStream outputStream = new PipedOutputStream();
  34. public Output() {
  35. }
  36. public PipedOutputStream getOutputStream() {
  37. return outputStream;
  38. }
  39. @Override
  40. public void run() {
  41. try {
  42. System.out.println("管道写出准备。");
  43. StringBuilder sb = new StringBuilder();
  44. // 模拟 通过for循环写入2050个字节
  45. for (int i = 0; i < 201; i++) {
  46. sb.append("0123456789");
  47. if (i > 0 && (i % 10 == 0)) {
  48. sb.append("\r\n");
  49. }
  50. }
  51. String str = sb.toString();
  52. outputStream.write(str.getBytes());
  53. System.out.println("管道写出完成。");
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. } finally {
  57. try {
  58. if (outputStream != null)
  59. outputStream.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. }
  66. public static void main(String[] args) throws IOException {
  67. Input input = new Input();
  68. Output output = new Output();
  69. /**
  70. * 将“管道输入流”和“管道输出流”关联起来。
  71. */
  72. //input.getInputStream().connect(output.getOutputStream());// 与下面一行等价
  73. output.getOutputStream().connect(input.getInputStream());
  74. new Thread(input).start();
  75. new Thread(output).start();
  76. }

序列化与ObjectInputStream、ObjectOutputStream

使用ObjectInputStream、ObjectOutputStream读取或写入对象,首先该对象必须实现Serializable接口,使得能够序列化和反序列化。

简单示例:

  1. @SuppressWarnings("unused")
  2. public static void main(String[] args) throws IOException {
  3. class A implements java.io.Serializable {
  4. private static final long serialVersionUID = -9115696482036699559L;
  5. private int i = 1;
  6. private float f = 3;
  7. private String s = "风策信";
  8. public A() {
  9. super();
  10. }
  11. public A(int i, float f, String s) {
  12. super();
  13. this.i = i;
  14. this.f = f;
  15. this.s = s;
  16. }
  17. @Override
  18. public String toString() {
  19. StringBuilder builder = new StringBuilder();
  20. builder.append("A [i=").append(i).append(", f=").append(f).append(", s=").append(s).append("]");
  21. return builder.toString();
  22. }
  23. }
  24. class B implements java.io.Serializable {
  25. private static final long serialVersionUID = 6124575321340728225L;
  26. private long i = 2;
  27. private double f = 4;
  28. private String str = "风策信";
  29. public B() {
  30. super();
  31. }
  32. public B(long i, double f, String str) {
  33. super();
  34. this.i = i;
  35. this.f = f;
  36. this.str = str;
  37. }
  38. @Override
  39. public String toString() {
  40. StringBuilder builder = new StringBuilder();
  41. builder.append("B [i=").append(i).append(", f=").append(f).append(", str=").append(str).append("]");
  42. return builder.toString();
  43. }
  44. }
  45. A a = new A(1, 3, "a");
  46. B b = new B(2, 4, "b");
  47. //System.out.println(a);
  48. //System.out.println(b);
  49. ObjectOutputStream oos = null;
  50. try {
  51. oos = new ObjectOutputStream(new FileOutputStream("object.data.bin"));
  52. oos.writeObject(a);
  53. oos.writeObject(b);
  54. oos.flush();// 把缓冲区内的数据刷新到磁盘
  55. } finally {
  56. if (oos != null)
  57. oos.close();
  58. }
  59. ObjectInputStream ois = null;
  60. try {
  61. ois = new ObjectInputStream(new FileInputStream("object.data.bin"));
  62. A a1 = (A) ois.readObject();
  63. B b1 = (B) ois.readObject();
  64. System.out.println(a1);
  65. System.out.println(b1);
  66. } catch (ClassNotFoundException e) {
  67. e.printStackTrace();
  68. } finally {
  69. if (ois != null)
  70. ois.close();
  71. }
  72. }

回推流:PushbackInputStream与PushbackReader

PushbackInputStream/PushbackReader 用于解析InputStream/Reader内的数据,允许你读取字节/字符后,回推(pushback)到流中,而不破坏流。

PushbackInputStream类具有以下构造函数:

  1. PushbackInputStream(InputStream inputStream)
  2. PushbackInputStream(InputStream inputStream,int numBytes)

第一种形式创建的流对象允许将一个字节返回到输入流; 第二种形式创建的流对象具有一个长度为numBytes的回推缓存,从而允许将多个字节回推到输入流中。

提供了unread()方法,如下所示:

  1. void unread(int b)
  2. void unread(byte[] buffer)
  3. void unread(byte[] buffer,int offset,int numBytes)

第一种形式回推b的低字节,这会使得后续的read()调用会把这个字节再次读取出来。第二种形式回推buffer中的字节。第三种形式回推buffer中从offset开始的numBytes个字节。当回推缓存已满时,如果试图回推字节,就会抛出IOException异常。

示例:

  1. public static void main(String[] args) throws IOException {
  2. String filepath = "file.bin";
  3. java.io.OutputStream os = null;
  4. try {
  5. os = new FileOutputStream(filepath);
  6. os.write('#');
  7. os.write(new byte[]{'a', 'b', 'c', 'd'});
  8. os.flush();// 把缓冲区内的数据刷新到磁盘
  9. } finally {
  10. if (os != null) {
  11. os.close();// 关闭流
  12. }
  13. }
  14. /**
  15. * 回推(pushback)
  16. */
  17. PushbackInputStream pis = null;
  18. try {
  19. //pis = new PushbackInputStream(new FileInputStream(filepath));
  20. pis = new PushbackInputStream(new FileInputStream(filepath), 3);
  21. int len = -1;
  22. byte[] bytes = new byte[2];
  23. while ((len = pis.read(bytes)) != -1) {
  24. if ('b' == bytes[0]) {
  25. //pis.unread('U');
  26. //pis.unread(bytes);
  27. pis.unread(new byte[]{'1', '2', '3'});
  28. }
  29. for (int i = 0; i < len; i++) {
  30. System.out.print(((char) bytes[i]));
  31. }
  32. }
  33. System.out.println();
  34. } finally {
  35. if (pis != null)
  36. pis.close();
  37. }
  38. /**
  39. * 会发现PushbackInputStream并没有改变目标介质的数据,不破坏流
  40. */
  41. try {
  42. pis = new PushbackInputStream(new FileInputStream(filepath));
  43. int len = -1;
  44. byte[] bytes = new byte[2];
  45. while ((len = pis.read(bytes)) != -1) {
  46. for (int i = 0; i < len; i++) {
  47. System.out.print(((char) bytes[i]));
  48. }
  49. }
  50. } finally {
  51. if (pis != null)
  52. pis.close();
  53. }
  54. }

行数记录:LineNumberInputStream与LineNumberReader

LineNumberInputStream与LineNumberReader提供跟踪行号的附加功能。行是以回车符 ('\r')、换行符 ('\n') 或回车符后面紧跟换行符结尾的字节序列。在所有这三种情况下,都以单个换行符形式返回行终止字符。 行号以 0 开头,并在 read 返回换行符时递增 1。

使用getLineNumber()可以获取当前读取所在行数。

示例:

  1. public static void main(String[] args) throws IOException {
  2. String filepath = "file.txt";
  3. java.io.Writer w = null;
  4. try {
  5. w = new FileWriter(filepath);
  6. w.write("百世山河任凋换,一生意气未改迁。愿从劫火投身去,重自寒灰飞赤鸾。\r\n");
  7. w.write("沧海桑田新几度,月明还照旧容颜。琴心剑魄今何在,留见星虹贯九天。 \n");
  8. w.write("冰轮腾转下西楼,永夜初晗凝碧天。长路寻仙三山外,道心自在红尘间。 \n");
  9. w.write("何来慧剑破心茧,再把貂裘换酒钱。回望天涯携手处,踏歌重访白云间。\n");
  10. w.write("何以飘零去,何以少团栾,何以别离久,何以不得安? \n");
  11. w.flush();// 把缓冲区内的数据刷新到磁盘
  12. } finally {
  13. if (w != null) {
  14. w.close();// 关闭流
  15. }
  16. }
  17. /**
  18. * LineNumberReader
  19. */
  20. LineNumberReader lnr = null;
  21. try {
  22. lnr = new LineNumberReader(new FileReader(filepath));
  23. int len = -1;
  24. char[] chars = new char[2];
  25. //int lastLineNumber = -1;
  26. while ((len = lnr.read(chars)) != -1) {
  27. for (int i = 0; i < len; i++) {
  28. System.out.print(((char) chars[i]));
  29. }
  30. /*int lineNumber = lnr.getLineNumber();
  31. if (lineNumber != lastLineNumber) {
  32. System.out.println("---------------行数:" + lineNumber);
  33. lastLineNumber = lineNumber;
  34. }*/
  35. }
  36. int lineNumber = lnr.getLineNumber();
  37. System.out.println("行数:" + lineNumber);
  38. System.out.println();
  39. } finally {
  40. if (lnr != null)
  41. lnr.close();
  42. }
  43. }

StreamTokenizer的使用

StreamTokenizer定义了几种基本的常量用于标识解析过程:TT_EOF(流结尾)、TT_EOL(行结尾)、TT_NUMBER(数字符号, 0 1 2 3 4 5 6 7 8 9 . -都属于数字语法)、TT_WORD(一个单词)。

ttype 在调用 nextToken 方法之后,此字段将包含刚读取的标记的类型。

nval 如果当前标记是一个数字,则此字段将包含该数字的值。

sval 如果当前标记是一个文字标记,则此字段包含一个给出该文字标记的字符的字符串。

  1. public static void main(String[] args) throws IOException {
  2. StreamTokenizer tokenizer = new StreamTokenizer(new StringReader("Sven had 7 shining ring..."));
  3. while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {// 流末尾
  4. if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
  5. System.out.println(tokenizer.sval);
  6. } else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
  7. System.out.println(tokenizer.nval);
  8. } else if (tokenizer.ttype == StreamTokenizer.TT_EOL) {// 行末尾
  9. System.out.println();
  10. }
  11. }
  12. //System.out.println(tokenizer.lineno());
  13. }

基本方法介绍一下:

nextToken() - 从此标记生成器的输入流中解析下一个标记。

  1. 标记注释

    commenChar(int ch) - 指定某个字符为注释字符,此字符之后直到行结尾都被stream tokenizer忽略。

    slashSlashComments(boolean flag) - 如果为true,则//之间的都被认为是注释,反之,不是。

    slashStartComments(boolean flag) - 如果为true,则//之后到行结尾的所有都被认为是注释,反之,不是。

  2. 基本语义

    eolIsSignificant(boolean flag) - 决定一个行结束符是否被当作一个基本的符号处理,如果是true,则被当作一个基本符号,不当作普通的分隔符,如果是false,则保持原义,即当作普通的分隔符。

    lowerCaseMode(boolean flag) - 决定是否读取一个单词时是否转变成小写。

    parseNumbers() - 当stream tokenizer遭遇到一个单词为双精度的浮点数时,会把它当作一个数字,而不是一个单词。

    resetSyntax() - 重置语法表使所有的字符都被认为是“ordinary”。

  3. 指定字符语义

    ordinaryChar(int ch) - 指定字符在这个tokenizer中保持原义,即只会把当前字符认为普通的字符,不会有其他的语义。

    ordinaryChars(int low, int hi) - 指定范围内的字符保持语义,同上

    whitespaceChars(int low, int hi) - 字符low与hi之间的所有字符都被当作为空格符,即被认识为tokenzier的分隔符。

    wordChars(int low, int hi) - 字符low与hi之间的所有字符都被当作为单词的要素。一个单词是由一个单词要素后面跟着0个或者更多个单词要素或者数字要素。

合并流SequenceInputStream

SequenceInputStream会将与之相连接的流集组合成一个输入流并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的末尾为止。 合并流的作用是将多个源合并合一个源。


  1. public static void main(String[] args) throws IOException {
  2. String filepath1 = "file1.txt";
  3. String filepath2 = "file2.txt";
  4. java.io.Writer w = null;
  5. try {
  6. w = new FileWriter(filepath1);
  7. w.write("百世山河任凋换,一生意气未改迁。愿从劫火投身去,重自寒灰飞赤鸾。\r\n");
  8. w.write("沧海桑田新几度,月明还照旧容颜。琴心剑魄今何在,留见星虹贯九天。 \n");
  9. w.write("冰轮腾转下西楼,永夜初晗凝碧天。长路寻仙三山外,道心自在红尘间。 \n");
  10. w.write("何来慧剑破心茧,再把貂裘换酒钱。回望天涯携手处,踏歌重访白云间。\n");
  11. w.flush();// 把缓冲区内的数据刷新到磁盘
  12. } finally {
  13. if (w != null) {
  14. w.close();// 关闭流
  15. }
  16. }
  17. try {
  18. w = new FileWriter(filepath2);
  19. w.write("何以飘零去,何以少团栾,何以别离久,何以不得安? ");
  20. w.flush();// 把缓冲区内的数据刷新到磁盘
  21. } finally {
  22. if (w != null) {
  23. w.close();// 关闭流
  24. }
  25. }
  26. java.io.Reader r = null;
  27. try {
  28. Vector<InputStream> v = new Vector<InputStream>(2);
  29. InputStream s1 = new FileInputStream(filepath1);
  30. InputStream s2 = new FileInputStream(filepath2);
  31. v.addElement(s1);
  32. v.addElement(s2);
  33. r = new BufferedReader(new InputStreamReader(new SequenceInputStream(v.elements())));
  34. char[] data = new char[256];
  35. int len = -1;
  36. while ((len = r.read(data)) != -1) {// -1 表示读取到达文件结尾
  37. //操作数据
  38. for (int i = 0; i < len; i++) {
  39. System.out.print(data[i]);
  40. }
  41. }
  42. } finally {
  43. if (r != null) {
  44. r.close();// 关闭流
  45. }
  46. }
  47. }

更多Demo:https://git.oschina.net/svenaugustus/MyJavaIOLab

本文只针对标准IO的知识总结,其他IO总结姊妹篇(NIO)请参见:

JavaNIO编程一览笔录

Java标准I/O流编程一览笔录的更多相关文章

  1. Java 标准 IO 流编程一览笔录( 下 )

    8.回推流:PushbackInputStream与PushbackReader PushbackInputStream/PushbackReader 用于解析InputStream/Reader内的 ...

  2. Java 标准 IO 流编程一览笔录( 上 )

    Java标准I/O知识体系图: 1.I/O是什么? I/O 是Input/Output(输入.输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出. 2.流 流是一个连续的数据流,可以从流中读 ...

  3. Java 多线程并发编程一览笔录

    Java 多线程并发编程一览笔录 知识体系图: 1.线程是什么? 线程是进程中独立运行的子任务. 2.创建线程的方式 方式一:将类声明为 Thread 的子类.该子类应重写 Thread 类的 run ...

  4. Java多线程并发编程一览笔录

    线程是什么? 线程是进程中独立运行的子任务. 创建线程的方式 方式一:将类声明为 Thread 的子类.该子类应重写 Thread 类的 run 方法 方式二:声明实现 Runnable 接口的类.该 ...

  5. Java标准I/O流介绍

    1.I/O是什么? I/O 是Input/Output(输入.输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出. 2.流 流是一个连续的数据流,可以从流中读取数据,也可以往流中写数据.流与 ...

  6. UNIX环境编程学习笔记(13)——文件I/O之标准I/O流

    lienhua342014-09-29 1 标准 I/O 流 之前学习的都是不带缓冲的 I/O 操作函数,直接针对文件描述符的,每调用一次函数可能都会触发一次系统调用,单次调用可能比较快捷.但是,对于 ...

  7. 《Unix环境高级编程》读书笔记 第5章-标准I/O流

    1. 引言 标准I/O库由ISO C标准说明,由各个操作系统实现 标准I/O库处理很多细节,如缓冲区分配.以优化的块长度执行I/O等.这些处理使用户不必担心如何使用正确的块长度,这使得它便于用于使用, ...

  8. Java 标准 I/O 介绍

    一.Java标准I/O知识体系图: 二.I/O是什么 I/O 是Input/Output(输入.输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出. 三.Java I/O 用途与对应的流一览 ...

  9. Java基础教程:JDBC编程

    Java基础教程:JDBC编程 1.什么是JDBC JDBC 指 Java 数据库连接,是一种标准Java应用编程接口( JAVA API),用来连接 Java 编程语言和广泛的数据库. JDBC A ...

随机推荐

  1. LeetCode: Partition List 解题报告

    Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...

  2. 【linux】dpkg info修复及dpkg: warning: files list file for package

    mv /var/lib/dpkg/info /var/lib/dpkg/info.bak //现将info文件夹更名 sudo mkdir /var/lib/dpkg/info //再新建一个新的in ...

  3. chmod 权限 命令详细用法

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  4. 如何搭建iOS项目基本框架

    今天我们来谈谈如何搭建框架,框架需要做一些什么. 第一步:找到我们的目标我们的目标是让其他开发人员拿到手后即可写页面,不再需要考虑其他的问题. 第二步:我们需要做哪些东西各位跟着我一步一步来进行. 假 ...

  5. WebSphere ILOG JRules 规则引擎运行模式简介

    WebSphere ILOG JRules 规则引擎运行模式简介 引言 作为 JRules 的核心组件,规则引擎决定了在规则集的执行过程中,哪些业务规则会被执行,以及以何种顺序执行.理解并合理选择规则 ...

  6. [转]mysql中的字符串的拼接

    字符串的拼接 1,Mysql 在Java.C#等编程语言中字符串的拼接可以通过加号“+”来实现,比如:"1"+"3"."a"+"b ...

  7. ubi 文件系统加载失败原因记录

    尝试升级 kernel 到 4.4.12版本,然后出现 kernel 加载 ubi 文件系统失败的现象,现象如下 [ 3.152220] ubi0 error: vtbl_check: too lar ...

  8. BMP位图文件格式详解及编程建议

    BMP文件渊源流长,虽然对JPG.PNG等格式图像文件来说,确实有点土,但是毕竟BMP文件格式相对简单,容易理解,至于BMP众多的位图格式也不能责怪微软,主要是早期谁也没料到图片技术会发展的这么快,而 ...

  9. 事件EVENT与waitforsingleobject的使用以及Mutex与Event的区别

    Mutex与Event控制互斥事件的使用详解 最近写一程序,误用了Mutex的功能,错把Mutex当Event用了. [Mutex] 使用Mutex的主要函数:CreateMutex.ReleaseM ...

  10. android开发(25) - 推送的实现,使用百度云推送

    什么叫推送? 中文名称:推送 英文名称:push 定义:描述因特网内容提供者和因特网用户之间工作方式的术语.“推送”指因特网内容提供者定期向预订用户“提供”数据. 项目中有可能会用到推送.如果自己写一 ...