FileInputStream和FileReader
这两个类都可以读入数据到缓冲区,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的更多相关文章
- java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr
BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...
- [转]Java FileInputStream与FileReader的区别
在解释Java中FileInputStream和FileReader的具体区别之前,我想讲述一下Java中InputStream和Reader的根本差异,以及分别什么时候使用InputStream和R ...
- InputStream和Reader,FileInputStream和 FileReader的区别
一.InputStream和Reader的区别 InputStream和Reader都可以用来读数据(从文件中读取数据或从Socket中读取数据),最主要的区别如下: InputStream用来读取二 ...
- FileInputStream、FileReader、FileWriter和File
FileInputStream提供了对文件的字节读取 用于读取诸如图像数据之类的原始字节流 如:FileInputStream fis=new FileInputStream(new Fi ...
- Java FileInputStream与FileReader的区别
在解释Java中FileInputStream和FileReader的具体区别之前,我想讲述一下Java中InputStream和Reader的根本差异,以及分别什么时候使用InputStream和R ...
- FileInputStream、FileReader、FileInputStream、FileWriter使用小结
本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识 InputStream包含如下三个方法: int read():从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转化为i ...
- java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例
FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...
- 节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter
节点流(文件流) FileInputStream(字节流)处理视频类的 FileOutputStream(字节流) FileReader(字符流)处理文本文件 ...
- FileReader采用的默认编码
很久以前听教学视频,里面讲到Java采用的默认编码是ISO-8859-1,一直记着. 但是最近重新看IO流的时候,惊讶地发现,在不指定字符编码的情况下,FileReader居然可以读取内容为中文的文本 ...
随机推荐
- Laravel 静态资源管理
<link rel="stylesheet" href="{{ asset('bootstrap/css/bootstrap.min.css') }}" ...
- mac 获得进程信息的方法
NSProcessInfo可以获得当前进程的信息.获得所有活动进程信息可以尝试使用下面的方法. 进程的信息可以通过ps命令得到也可以通过sysctl方法得到. 但是我总是不能获取进程的流量信息,关于这 ...
- Java Socket应用
Java Socket(套接字)通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求.
- cssnext下一代的css
前端技术更新迭代的速度令人咂舌,互联网+的风头刚起那几年,前端技术大多还停留在jquery阶段,按需加载还停留在seajs和requirejs的阶段,css3和H5也不过才崭露头角,但经过几年的飞速发 ...
- Adobe Premiere Pro导入插件开发遇到的一个问题
最近在更新公司一款Premiere Pro CC导入插件的时候,遇到了一个神奇的现象.具体的现象是这样的:我们的插件需要将一些私有的文件数据放到插件中,比如说当前活动的文件名.当插件中收到不同的sel ...
- T2597 团伙 codevs
http://codevs.cn/problem/2597/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 1920年的芝加 ...
- linux find grep 查找命令
原文:fhqdddddd.blog.163.com/blog/static/186991542012417105729415/ find 1.作用 find命令的作用是在目录中搜索文件,它的使用权限是 ...
- sqlplus登陆scott用户,以及退出连接
进入sqlplus界面 即登陆成功,PLsql也一样 退出连接:
- JStorm学习
一.简介 JStorm是一个分布式实时计算引擎.JStorm是一个类似于Hadoop MapReduce的系统,用户按照指定的接口实现一个任务,然后将这个任务交给JStorm系统,JStorm将这个任 ...
- [转]通俗易懂的php多线程解决方案
原文: https://www.w3cschool.cn/php/php-thread.html --------------------------------------------------- ...