一、IO简述

1.1、简述

IO:input/output

IO流用来处理设备之间的数据传输

Java对数据的操作是通过流的方式

Java用于操作流的对象都在IO包中。

1.2、结构

字节流抽象类:

InputStream,OutputStream

字符流抽象类:

Reader、Writer。

ps:由这四4个派生出来子类名称都是以父类名作为子类名的后缀

如:InputStream的子类FileInputStream

如:Reader的子类FileReader;

1.3、分类

按操作数据方式为两种:字节流与字符流

按流向分:输入流,输出流

二、Writer

2.1、writer

写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。

字段摘要
protected Object lock
用于同步针对此流的操作的对象。

构造方法摘要
protected
Writer()

创建一个新的字符流 writer,其关键部分将同步 writer 自身。
protected
Writer(Object lock)

创建一个新的字符流 writer,其关键部分将同步给定的对象。

方法摘要
Writer append(char c)

将指定字符添加到此 writer。
Writer append(CharSequence csq)

将指定字符序列添加到此 writer。
Writer append(CharSequence csq, int start,
int end)

将指定字符序列的子序列添加到此
writer.Appendable
abstract
void
close()

关闭此流,但要先刷新它。
abstract
void
flush()

刷新该流的缓冲。
void write(char[] cbuf)

写入字符数组。
abstract
void
write(char[] cbuf,
int off, int len)

写入字符数组的某一部分。
void write(int c)

写入单个字符。
void write(String str)

写入字符串。
void write(String str,
int off, int len)

写入字符串的某一部分。

三、FileWriter与FileReader

3.1、FileWriter

构造方法摘要
FileWriter(File file)
根据给定的 File 对象构造一个 FileWriter 对象。
FileWriter(File file,
boolean append)

根据给定的 File 对象构造一个 FileWriter 对象。
FileWriter(FileDescriptor fd)

构造与某个文件描述符相关联的 FileWriter 对象。
FileWriter(String fileName)

根据给定的文件名构造一个 FileWriter 对象。
FileWriter(String fileName,
boolean append)

根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造
FileWriter 对象。

package com.pb.io.demo1;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/*
* close:关闭流,会将流关闭,不能再使用
* flush:flush刷新后,流可以继续使用,
*/
public class WriterDemo { public static void main(String[] args) {
Scanner input=new Scanner(System.in);
File file=null; //文件
FileWriter fw=null; //字符输出流
try {
file=new File("d:/demo.txt");
// fw=new FileWriter(file);//如果有这个文件就会覆盖
//如果要追加到文件内容后面就使用true
fw=new FileWriter(file,true);
System.out.println("请输入内容,请输入 over后结束");
String str=null; //接收输入的内容
char [] buf=new char[1024];//定义缓冲区大小
do{
str=input.nextLine();
buf=str.toCharArray();//转换为字符数组
fw.write(buf); //将字符数组写入
fw.flush();//刷新流
}while(!str.equals("over"));
} catch (IOException e) {
e.printStackTrace();
}finally{
//使用finally关闭流
try {
if(fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("写入结束"); } }

3.2、FileReader

构造方法摘要
FileReader(File file)
在给定从中读取数据的 File 的情况下创建一个新 FileReader
FileReader(FileDescriptor fd)

在给定从中读取数据的 FileDescriptor 的情况下创建一个新
FileReader
FileReader(String fileName)

在给定从中读取数据的文件名的情况下创建一个新
FileReader


3.3示例 单个字符读取

package com.pb.io.demo1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; /*
* 单个字符读取
*/ public class FileReaderDemo { public static void main(String[] args) { File file=null; //文件
FileReader fr=null; //字符输入流 try {
file=new File("d:/demo.txt");
fr=new FileReader(file);
int len=0;//接收读出内容 while((len=fr.read())!=-1){
System.out.println((char)len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//关闭流
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

3.4、示例 数组方式读取 建议使用

package com.pb.io.demo1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays; /*
* 通过字符数组 读取
*/ public class FileReaderDemo { public static void main(String[] args) { File file=null; //文件
FileReader fr=null; //字符输入流 try {
file=new File("d:/demo.txt");
fr=new FileReader(file);
//接收读出内容数组
char [] buf=new char [1024]; //一般为1024的整数倍
int len=0;
while((len=fr.read(buf))!=-1){ //读取内容到字符数组
System.out.println(new String(buf,0,len));//读有多少,就输出多少 }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//关闭流
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

四、完整和复制文件

4.1、字符输入流和字符输入流

package com.pb.io.demo1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner; public class FileWriterAndFileReader { public static void main(String[] args) {
File file = new File("d:\\demo.txt");
Output(file);
input(file);
} // 写入文件
private static void Output(File file) {
FileWriter fw = null;
Scanner input = new Scanner(System.in);
try {
fw = new FileWriter(file, true);
// 声明变量来接收用户输入
System.out.println("请输入要写入的内容:输入over结束");
String str = null;
// 声明缓冲区
char[] buf = new char[1024];
do {
str = input.nextLine(); // 接收用户输入
buf = str.toCharArray(); // 转换为数组
fw.write(buf, 0, buf.length);// 写入 } while (!(str.equals("over")));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} // 读写文件
private static void input(File file) {
FileReader fr = null;
try {
fr = new FileReader(file);
// 定义字符数组
char[] buf = new char[1024];// 缓冲区大小
int len = 0;// 长度读取的字符个数
while ((len = fr.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

4.2、复制文件

package com.pb.io.demo1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; /**
* 复制的原理 将一个文件存储到另一个文件中 边读边写 先读再写
*
*/
public class CopyFileDemo { public static void main(String[] args) {
File soure = new File("d:\\demo.txt");
File objFile = new File("e:\\q.txt");
copy1(soure, objFile);
copy2(soure, objFile);
}
/*
* 读一个复制一个
*/
public static void copy1(File soure, File objFile) {
FileReader fr = null;
FileWriter fw = null;
try {
// 声明读,写流对象
fr = new FileReader(soure);
fw = new FileWriter(objFile,true);
int ch = 0;
int count=0;
// 开始读
while ((ch = fr.read()) != -1) {
count++;
System.out.println("正在读取"+count+"行");
fw.write(ch);
fw.write("\r\n"); //换行
System.out.println("正在写入"+count+"行");
}
System.out.println("读写完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { try {
if (fw != null)
fw.close(); } catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
/*
* 数组方式读取复制
*/
public static void copy2(File soure, File objFile) {
FileReader fr = null;
FileWriter fw = null;
try {
// 声明读,写流对象
fr = new FileReader(soure);
fw = new FileWriter(objFile,true);
int ch = 0;
int count=0;
char [] buf=new char[1024];
// 开始读
while ((ch = fr.read(buf)) != -1) {
count++;
System.out.println("正在读取"+count+"行");
fw.write(buf,0,ch);
fw.write("\r\n"); //换行
System.out.println("正在写入"+count+"行");
}
System.out.println("读写完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { try {
if (fw != null)
fw.close(); } catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} } }

 

JAVA基础学习day19--IO流一、FileWrite与FileReader的更多相关文章

  1. 【java基础学习】IO流

    IO流 字节流InputStream和OutputStream 字符流Writer和Reader 装饰模式

  2. Java基础之详谈IO流

    Java基础知识.IO流详细讲解.你所要的IO这里都有

  3. Java基础教程:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  4. Java基础系列8——IO流超详细总结

    该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 在初学Java时,I ...

  5. java基础11(IO流)-字符流

    转换流 由于字节流操作中文不是特别方便,所以java中提供了转换流 编码表:由现实世界的字符和对应的数值组成的一张表 编码:把看得懂的变成看不懂的(String-------byte[]) 解码:把看 ...

  6. java基础10(IO流)-字节流

    IO流 输入与输出[参照物是程序] 如果从键盘.文件.网络甚至是另一个进程(程序或系统)将数据读入到程序或系统中,称为输入 如果是将程序或系统中的数据写到屏幕.硬件上的文件.网络上的另一端或者是一个进 ...

  7. Java基础12一IO流

    1.IO流的原理 利用数据通道实现程序和数据源之间数据的的读写操作.  2.IO流分类 输入流.输出流.字节流.字符流.节点流.过滤流  3.InputStream 字节输入流 实现类FileInpu ...

  8. Java基础之(IO流)

    简介: 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作. 一.File ...

  9. java基础9(IO流)-File类

    File类 File:文件和目录路径名的抽象表示形式.即java中把文件或者目录都封装成File对象 代码练习1 import java.io.File; public class FileDemo1 ...

  10. 【Java基础总结】IO流

    字节流 1. InputStream 字节输入流 代码演示 InputStream in = System.in; System.out.println("int read(byte b) ...

随机推荐

  1. [ML] Naive Bayes for email classification

    20 Newsgroups (Original) Author: Jeffrey H 1. Introduction This is only a test report for naive baye ...

  2. nodejs 常用全局包

    1.nodemon 更改node程序后程序自动启动 (nodemon app.js) npm install nodemon  -g 2.gulp 压缩合并代码等 npm install gulp - ...

  3. MyBatis魔法堂:各数据库的批量Update操作

    一.前言   MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...

  4. Unity中简单使用Opengl

    简介 由于项目特殊需求,需要在unity中使用一些OpenGL的东西来绘制图形(PS:其实就是有一个拖尾算法只有OpenGL版本~~~懒得改了,直接在unity中使用OpenGL算了).所以琢磨咯下如 ...

  5. WPF 程序自删除(自毁)|卸载程序删除

    一般是在MainWindow_Closed 事件中调用批处理命令删除. 在借鉴别人的想法的基础上的算是改进. 自删除步骤: 1.删除文件 2.删除存放文件夹. 实现代码: private static ...

  6. 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性 作者:webabcd 介绍重新想象 Windows 8. ...

  7. python代码风格-PEP8

    转载自http://www.douban.com/note/134971609/ Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下 ...

  8. Linux 安装 PHP 环境

    使用虚拟机玩linux时,发现CentOS中的php版本是5.1.6.如果要安装新版的php,需要把旧的版本删除. 先查看下php版本:# php -v 如果执行该命令提示该命令不存在,那么可以通过以 ...

  9. Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):2.解析获取]

    前言 Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github.com/auto ...

  10. RCA端子颜色(红、白、黄)

    RCA端子(红白黄)的作用: 黄:视频 红:左声道 白:右声道 RCA为两口插头,红色代表左声道,白色为右声道,3.5(AUX口)同样为立体声接头,虽然它只有一个端口,同样也具有左右声道分开传输的功能 ...