Java IO 对字符的读取和写出
来源:互联网 发布:淘宝打开店铺自动收藏 编辑:程序博客网 时间:2024/06/05 17:33
1、对字符的读取
只能读取纯文本,不能读取二进制文件如音频,视频等,doc文件以及其他不是纯文本的文档。
1.1 纯文本的读取
使用java.io.Reader类读取纯文本文件,其源代码重要的几个方法如下:
package java.io;public abstract class Reader implements Readable, Closeable { /** * 读取一个字符 */ public int read() throws IOException { char cb[] = new char[1]; if (read(cb, 0, 1) == -1) return -1; else return cb[0]; } /** * 读取一个字符数组 */ public int read(char cbuf[]) throws IOException { return read(cbuf, 0, cbuf.length); } /** * 读取一个字符数组或者字符数组的一部分 */ abstract public int read(char cbuf[], int off, int len) throws IOException; /** * 关闭流 */ abstract public void close() throws IOException;}
1.2 读取纯文本的demo如下:
/** * * @param file * 文件对象 */public static void readFile(File file) { Reader reader = null; try { reader = new FileReader(file); char[] buffer = new char[1024]; int len = 0; while (-1 != (len = reader.read(buffer))) { System.out.println(new String(buffer, 0, len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != reader) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } }}/** * * @param file * 文件对象 */public static void readFileWithJdk7(File file) { // java 7 新特性 try-with-resource // 不用手动释放资源,程序自动释放,因为Reader实现了AutoCloseable接口 try (Reader reader = new FileReader(file)) { char[] buffer = new char[1024]; int len = 0; while (-1 != (len = reader.read(buffer))) { System.out.println(new String(buffer, 0, len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}/** * * @param filePath * 文件路径 */public static void readFile(String filePath) { readFile(new File(filePath));}
2、对字符的写出
只能对纯文本写出。
2.1 纯文本的写出
使用java.io.Writer类向纯文本进行写出,其中源码几个重要的方法如下:
package java.io;public abstract class Writer implements Appendable, Closeable, Flushable { /** * 写一个字符 */ public void write(int c) throws IOException { synchronized (lock) { if (writeBuffer == null){ writeBuffer = new char[WRITE_BUFFER_SIZE]; } writeBuffer[0] = (char) c; write(writeBuffer, 0, 1); } } /** * 写一个字符数组 */ public void write(char cbuf[]) throws IOException { write(cbuf, 0, cbuf.length); } /** *写一个字符数组或字符数组的一部分 */ abstract public void write(char cbuf[], int off, int len) throws IOException; /** * 写一个字符串 */ public void write(String str) throws IOException { write(str, 0, str.length()); } /** * 写一个字符串或字符串的一部分 */ public void write(String str, int off, int len) throws IOException { synchronized (lock) { char cbuf[]; if (len <= WRITE_BUFFER_SIZE) { if (writeBuffer == null) { writeBuffer = new char[WRITE_BUFFER_SIZE]; } cbuf = writeBuffer; } else { // Don't permanently allocate very large buffers. cbuf = new char[len]; } str.getChars(off, (off + len), cbuf, 0); write(cbuf, 0, len); } } /** * 追加特定字符到此流中 */ public Writer append(CharSequence csq) throws IOException { if (csq == null) write("null"); else write(csq.toString()); return this; } /** * 追加特定字符或特定字符的一部分到此流中 */ public Writer append(CharSequence csq, int start, int end) throws IOException { CharSequence cs = (csq == null ? "null" : csq); write(cs.subSequence(start, end).toString()); return this; } /** * 追加特定字符到此流中 */ public Writer append(char c) throws IOException { write(c); return this; } /** * 刷新流 */ abstract public void flush() throws IOException; /** * 关闭流 */ abstract public void close() throws IOException;}
2.2 纯文本写出demo如下:
/** * jdk 7 新特性 * * @param file * 指定输出文件 * @param msg * 输出内容 * @param append * 是否追加,true 追加 false 不追加 */public static void writeToFileWithJdk7(File file, String msg, boolean append) { try (Writer writer = new FileWriter(file)) { writer.write(msg); writer.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}/** * * @param file * 指定输出文件 * @param msg * 输出内容 * @param append * 是否追加,true 追加 false 不追加 */public static void writeToFile(File file, String msg, boolean append) { Writer writer = null; try { writer = new FileWriter(file); writer.write(msg); writer.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != writer) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } }}/** * * @param filePath * 指定输出文件路径 * @param msg * 输出内容 * @param append * 是否追加,true 追加 false 不追加 */public static void writeToFile(String filePath, String msg, boolean append) { writeToFile(new File(filePath), msg, append);}
0 0
- Java IO 对字符的读取和写出
- Java IO 对字节的读取和写出
- io的读取和写出
- java.io 对字节流和字符流的讨论
- java.io 对字节流和字符流的讨论
- 黑马程序员_java的IO流(对第十八课IO理解以及文件读取和字符写入总结)
- Java IO 缓冲流的读入和写出
- Java IO 转换流的读入和写出
- IO学习(七)用字符流进行纯文本的读取和写出
- java.io.BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
- java.io 对字节流和字符流的讨论[zz]
- 黑马程序员 IO 字符流中 的读取 和 写入
- Java IO学习【3】IO中异常的抛出/单个字符读取文件、数组读取字符文件
- IO 读取文件、写出到文件的普通方法
- Java IO字符流读取文件总结
- java io 写入写出
- 对一个文件的写入写出操作--IO流
- java-IO读取和写入
- Jersey中的基于MVC模式的JSP跳转方式:Viewable与request转发。
- LeetCode进阶之路( Search for a Range)
- 加权异构信息网络中的基于语义路径的个性化推荐
- 初识PopupWindow
- hdoj 5765 Bonds
- Java IO 对字符的读取和写出
- Python 之 re模块 正则表达式
- 如何设置linux添加开机启动项
- 001:Why is it faster to process a sorted array than an unsorted array?
- java挑战高并发(8):使用synchronized获取互斥锁的几点说明
- Android 自定义百分比布局
- 进程间通讯
- vim中显示和关闭行号
- Havel-Hakimi定理(判断一个序列是否可图)->POJ1659