这两个类都可以读入数据到缓冲区,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. laravel 查询构造器2

    //查询构造器 public function query() { //获取所有的数据 $student = DB::table('student')->get(); var_dump($stu ...

  2. centos7 搭建hadoop

    参考文档:http://blog.csdn.net/xiaoxiangzi222/article/details/52757168 https://waylau.com/centos-7-instal ...

  3. mac 获得进程信息的方法

    NSProcessInfo可以获得当前进程的信息.获得所有活动进程信息可以尝试使用下面的方法. 进程的信息可以通过ps命令得到也可以通过sysctl方法得到. 但是我总是不能获取进程的流量信息,关于这 ...

  4. npm start 修改启动端口的不同方式

    antd的启动配置文件基于package.json文件,配合roadhog使用时,启动配置是: "scripts": { "start": "road ...

  5. BZOJ——1626: [Usaco2007 Dec]Building Roads 修建道路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1626 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

  6. route命令走一波

    1.写文章去了解某个命令完全是兴起,并没有下定决心去学哪一套课程,目前的状态仍然是犹豫中,废话不多说进入正文,九月二十七这天看到了route命令,发现对路由这个命令很陌生,陌生到根本不知道它是用来干什 ...

  7. 【Todo】【转载】Scala中Array, List, Tuple的区别

    参考了这篇文章: https://my.oschina.net/u/1034176/blog/512314 1. 在Scala 2.7中,Array.List都不能混合类型,只有Tuple可以:而在S ...

  8. BUPT复试专题—哈夫曼树(2010)

    https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=67&tqId=29635&tPage= ...

  9. 零基础学python-3.2 变量赋值

    这一节我们来具体展开变量赋值 1.以下我们举各种赋值的样例 anInt=12 anFloat=2.2 anStr='string' aList=['a','a','a'] anArray=(1,2,3 ...

  10. PS 如何把大嘴变小嘴

    Photoshop整容教程:让MM美唇大嘴变小嘴 2009-06-17 14:15作者:佚名出处:天极网软件频道责任编辑:王健       下面就开始实际操作了.       1.首先从Photosh ...