这两个类都可以读入数据到缓冲区,FileInputStream在传递到buffer的时候要用byte定义buffer,不然报错。比如:

byte [] buffer = new byte[100];

  而用FileReader传递数据到buffer的时候只能用char定义buffer,不然报错。

char [] buffer = new char[100]

  下面的代码可以把目录下的一个文件里的内容传到另一个文件:

//transfer a huge file using a fileinputstream
import java.io.*;
class Test
{
public static void main(String args[])
{
FileInputStream fis = null ; //要写在try外面,否则finally里无法找到fis和fos;并且一定要赋值null
FileOutputStream fos = null ;
try
{
fis = new FileInputStream("C:/from.txt");
fos = new FileOutputStream("C:/to.txt");
byte [] buffer = new byte[100];//数组的定义方法
while(true)
{
int temp = fis.read(buffer , 0 , buffer.length);//这句话一定要写在while里面,不然会生成一个巨大的txt
if(temp == -1)//read完了之后返回-1
{break ;}
fos.write(buffer, 0 , temp);
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
try
{
fis.close();
fos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}

  

  用FileReader实现:

//transport a huge file using FileReader
import java.io.*;
class Test2
{
public static void main(String args[])
{
FileReader fr = null ;
FileWriter fw = null ;
try
{
fr = new FileReader("C:/from.txt");//此处不要忘了写参数
fw = new FileWriter("C:/to.txt");
char [] buffer = new char[100];//数组的定义方法
// int temp = fr.read(buffer, 0 , buffer.length);
// for(int i = 0 ; i < temp ; i++)
// System.out.println(buffer[i]);
while(true)
{
int temp = fr.read(buffer , 0 , buffer.length);//这句话一定要写在while里面,不然会生成一个巨大的txt
if(temp == -1)//read完了之后返回-1
{break ;}
fw.write(buffer, 0 , temp);
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
try
{
fr.close();
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}

需要注意的是用文件传递操作的时候一定要用到try..catch..finally,尤其要注意在finally里要再用一个try..catch语句调用close()语句把输入流和输出流关掉,否则FileReader甚至出现不工作的情况。

(END)

----------------Mar.5,2014更新-------------------------------

  今天想要用算法把txt中的文字加密,想要读取一整段的String。于是把while循环改了一下:

while (true) {
int temp = fr.read(buffer, 0, buffer.length);//temp返回的是长度。 这句话一定要写在while里面,不然会生成一个巨大的txt
if (temp == -1)// read完了之后返回-1
{
break;
}
contents = contents + String.valueOf(buffer);//char转成string
char [] cts = contents.toCharArray();//string转成char
fw.write(cts, 0, temp);
}

  注意如果再用FileWriter输出,将会得到ANSI编码的txt,所以如果原来的txt是Unicode编码或者其他编码,就会出现乱码。

FileInputStream和FileReader的更多相关文章

  1. java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr

    BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...

  2. [转]Java FileInputStream与FileReader的区别

    在解释Java中FileInputStream和FileReader的具体区别之前,我想讲述一下Java中InputStream和Reader的根本差异,以及分别什么时候使用InputStream和R ...

  3. InputStream和Reader,FileInputStream和 FileReader的区别

    一.InputStream和Reader的区别 InputStream和Reader都可以用来读数据(从文件中读取数据或从Socket中读取数据),最主要的区别如下: InputStream用来读取二 ...

  4. FileInputStream、FileReader、FileWriter和File

    FileInputStream提供了对文件的字节读取 用于读取诸如图像数据之类的原始字节流       如:FileInputStream fis=new FileInputStream(new Fi ...

  5. Java FileInputStream与FileReader的区别

    在解释Java中FileInputStream和FileReader的具体区别之前,我想讲述一下Java中InputStream和Reader的根本差异,以及分别什么时候使用InputStream和R ...

  6. FileInputStream、FileReader、FileInputStream、FileWriter使用小结

    本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识 InputStream包含如下三个方法: int read():从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转化为i ...

  7. java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例

    FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...

  8. 节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter

    节点流(文件流) FileInputStream(字节流)处理视频类的                   FileOutputStream(字节流) FileReader(字符流)处理文本文件    ...

  9. FileReader采用的默认编码

    很久以前听教学视频,里面讲到Java采用的默认编码是ISO-8859-1,一直记着. 但是最近重新看IO流的时候,惊讶地发现,在不指定字符编码的情况下,FileReader居然可以读取内容为中文的文本 ...

随机推荐

  1. ubuntu下安装jdk、tomcat、mysql

    1.JDK安装 方法1: 将JDK安装包解压缩之后,编辑~/.bashrc文件,在该文件里面加入下面的配置,然后通过source ~/.bashrc.JDK即安装成功. export JAVA_HOM ...

  2. Log4J使用详情

    一 .Log4J使用详情 Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以 ...

  3. Java重写父类使用@Override时出现The method destroy() of type xxx must override a superclass method的问题解决

    解决方法: 1.把JDK版本改成1.6以上的. 2.把Compiler改成1.6以上的. 关于这两者的区别,参考:http://www.cnblogs.com/EasonJim/p/6741682.h ...

  4. MongoDB下配置用户权限

    MongoDB默认设置为无权限訪问限制 注:研究成果基于Windows平台 在部署mongodb成功后.进入控制台: 输入命令:mongod  use admin,你会发现该DB下包括了一个syste ...

  5. 单点登录CAS-Demo

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 1安全证书配置 2部署服务端CAS-Server 3部署CAS-Client 4测试SSO   1,安全证书配置 CAS默认 ...

  6. 数据库系统学习(七)-SQL语言之复杂查询与视图

    第七讲 SQL语言之复杂查询与视图 基本内容 子查询 IN与NOT IN谓词子查询 判断某一表达式的值是否在子查询的结构中 非相关子查询 相关子查询 theta some /theta all谓词子查 ...

  7. [CSS3] Target HTML Elements not Explicitly set in the DOM with CSS Pseudo Elements (Blockquotes)

    Pseudo elements allow us to target elements that are not explicitly set in the DOM. Using ::before : ...

  8. 【JavaScript】数据类型

    学习不论什么一种程序设计语言.数据类型都是不可缺少的一部分内容,非常基础,也非常重要.该用何种数据类型定义变量.这也是编程中最基础的一项. ECMAScript中有5种简单数据类型:Undefined ...

  9. 当 外部 input 值的改变,获取 当前 input type="hidden" 的值

    1.如何用jquery获取<input id="test" name="test" type="text"/>中输入的值? 方法 ...

  10. 常用DOS下MSC指令

    xp:copy C:\WINDOWS\repair\*.* 到 c:\windows\system32\config 2k: copy C:\winnt\repair\*.* 到 c:\winnt\s ...