Java-IO之FileReader和FileWriter
public class FileReaderWriterTest { private static final String FileName = "file.txt"; private static final String CharsetName = "utf-8"; public static void main(String[] args) { testWrite(); testRead(); } /** * OutputStreamWriter 演示函数 * */ private static void testWrite() { try { // 创建文件“file.txt”对应File对象 File file = new File(FileName); // 创建FileOutputStream对应FileWriter:将字节流转换为字符流,即写入out1的数据会自动由字节转换为字符。 FileWriter out1 = new FileWriter(file); // 写入10个汉字 out1.write("字节流转为字符流示例"); // 向“文件中”写入"0123456789"+换行符 out1.write("0123456789\n"); out1.close(); } catch(IOException e) { e.printStackTrace(); } } /** * InputStreamReader 演示程序 */ private static void testRead() { try { // 方法1:新建FileInputStream对象 // 新建文件“file.txt”对应File对象 File file = new File(FileName); FileReader in1 = new FileReader(file); // 测试read(),从中读取一个字符 char c1 = (char)in1.read(); System.out.println("c1="+c1); // 测试skip(long byteCount),跳过4个字符 in1.skip(6); // 测试read(char[] cbuf, int off, int len) char[] buf = new char[10]; in1.read(buf, 0, buf.length); System.out.println("buf="+(new String(buf))); in1.close(); } catch(IOException e) { e.printStackTrace(); } } }
public class FileReader extends InputStreamReader { /** * Creates a new <tt>FileReader</tt>, given the name of the * file to read from. * * @param fileName the name of the file to read from * @exception FileNotFoundException if the named file does not exist, * is a directory rather than a regular file, * or for some other reason cannot be opened for * reading. */ public FileReader(String fileName) throws FileNotFoundException { super(new FileInputStream(fileName)); } /** * Creates a new <tt>FileReader</tt>, given the <tt>File</tt> * to read from. * * @param file the <tt>File</tt> to read from * @exception FileNotFoundException if the file does not exist, * is a directory rather than a regular file, * or for some other reason cannot be opened for * reading. */ public FileReader(File file) throws FileNotFoundException { super(new FileInputStream(file)); } /** * Creates a new <tt>FileReader</tt>, given the * <tt>FileDescriptor</tt> to read from. * * @param fd the FileDescriptor to read from */ public FileReader(FileDescriptor fd) { super(new FileInputStream(fd)); } }
public class FileWriter extends OutputStreamWriter { /** * Constructs a FileWriter object given a file name. * * @param fileName String The system-dependent filename. * @throws IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */ public FileWriter(String fileName) throws IOException { super(new FileOutputStream(fileName)); } /** * Constructs a FileWriter object given a file name with a boolean * indicating whether or not to append the data written. * * @param fileName String The system-dependent filename. * @param append boolean if <code>true</code>, then data will be written * to the end of the file rather than the beginning. * @throws IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */ public FileWriter(String fileName, boolean append) throws IOException { super(new FileOutputStream(fileName, append)); } /** * Constructs a FileWriter object given a File object. * * @param file a File object to write to. * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason */ public FileWriter(File file) throws IOException { super(new FileOutputStream(file)); } /** * Constructs a FileWriter object given a File object. If the second * argument is <code>true</code>, then bytes will be written to the end * of the file rather than the beginning. * * @param file a File object to write to * @param append if <code>true</code>, then bytes will be written * to the end of the file rather than the beginning * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason * @since 1.4 */ public FileWriter(File file, boolean append) throws IOException { super(new FileOutputStream(file, append)); } /** * Constructs a FileWriter object associated with a file descriptor. * * @param fd FileDescriptor object to write to. */ public FileWriter(FileDescriptor fd) { super(new FileOutputStream(fd)); } }
Java-IO之FileReader和FileWriter的更多相关文章
- 黑马程序员——JAVA基础之IO流FileReader,FileWriter
------- android培训.java培训.期待与您交流! ---------- IO(Input Output)流 IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 J ...
- java io系列22之 FileReader和FileWriter
FileReader 是用于读取字符流的类,它继承于InputStreamReader.要读取原始字节流,请考虑使用 FileInputStream.FileWriter 是用于写入字符流的类,它继承 ...
- IO流--FileReader&&FileWriter
(一)FileReader (1)第一种读取方式 package com.songyan.fileReader; import java.io.FileNotFoundException; impor ...
- Java IO 转换流 字节转字符流
Java IO 转换流 字节转字符流 @author ixenos 字节流 输入字节流:---------| InputStream 所有输入字节流的基类. 抽象类.------------| Fil ...
- java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr
BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...
- java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例
FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...
- Java IO: FileReader和FileWriter
作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本章节将简要介绍FileReader和FileWriter.与FileInputStream和File ...
- Java IO(十七)FIleReader 和 FileWriter
Java IO(十七)FIleReader 和 FileWriter 一.介绍 FIleReader 和 FileWriter 是读写字符文件的便利类,分别继承于 InputStreamReader ...
- java 输入输出IO流 字符流 FileWriter FileReader
为什么要使用字符流 当使用字节流读取文本文件时,可能会有一个小问题.就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储.所以Java提供一些字符流类,以字符为单位读写 ...
- java的文件流:字节流(FileInputStream、FileOutputStream)和字符流(FileReader、FileWriter)。
java的输入输出建立在4个抽象类的基础上:InputStream.OutputStream.Reader.Writer.InputSream和OutputStream被设计成字节流类,而Reader ...
随机推荐
- JS 实现点击页面任意位置隐藏div、span
通过调用下面的 showhidden(“标签ID”) 显示div/span/…等标签内容,可以实现点击页面任意地方再次隐藏该标签内容,而showhidden(“标签ID”,”nohidden”)可保存 ...
- Java8的重要新特性
一.Lambda表达式 java8中Lambda表达式的书写方式: (参数) -> 表达式 (参数) -> 单行语句 (参数) -> { 语句 } 1.Lambda遍历List和Ma ...
- Angular 和 Vue 使用的对比总结 -- 脚手架
前言 之前是用Vue的,现在由于工作原因,开始使用Angular.分别是Vue2和Angular5入的坑.只是从使用上来对比总结,加深记忆,避免混淆. 什么 ? 你问实现原理的异同及优劣? 本宝宝还 ...
- dfsdf
This project was bootstrapped with Create React App. Below you will find some information on how to ...
- 浅谈Trie树(字典树)
Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...
- redux 最简例子
方便初学redux的同学学习,这里是最简单的redux例子 1 import React, {Component, PropTypes} from 'react' 2 import ReactDOM ...
- linux上快速搭建宝塔面板来操作便捷功能
关于宝塔 Linux版请确保纯净系统安装(支持CentOS.Ubuntu.Debian.Fedora.Deepin),Web端管理,QQ群及论坛技术支持一键LAMP/LNMP.创建网站FTP.数据库. ...
- angular学习笔记 父子组件传值
一.如何将父组件的值传到子组件? 在子组件里面引入Input,然后用@Input 变量1 接收 接着在父组件中的,子组件标签上添加[msg]="msg",中括号里的名字要与子组件中 ...
- springMVC源码解析--ViewResolverComposite视图解析器集合(二)
上一篇博客springMVC源码分析--ViewResolver视图解析器(一)中我们介绍了一些springMVC提供的很多视图解析器ViewResolver,在开发的一套springMVC系统中是可 ...
- Bootstrap3 代码-程序输出
通过 <samp> 标签来标记程序输出的内容. This text is meant to be treated as sample output from a computer prog ...