**字节流读取文件和写入文件的方法**
输入流实例
import java.io.*;
class Test{
public static void main(String args[]){
//声明输入流引用
FileInputStream fis = null;
try{
//生成代表输入流的对象(并传入所要读取数据源的当前文件目录)
fis = new FileInputStream("d:/work/src/from.txt");
//生成一个字节数组
byte [] buffer = new byte[100];
//调用输入对象的read方法,读取数据源中的数据存放于数组之中
fis.read(buffer,0,buffer.length);
//根据数组buffer传入的参数把字节数组还原成一个字符串
String s = new String(buffer);
//调用一个String对象的trim方法,将会去掉这个字符串
//的首尾空格和空字符
s = s.trim();
System.out.println(s);
for(int i = 0 ; i < buffer.length; i++){
System.out.println(buffer[i]);
}
}
catch(Exception e){
System.out.println(e);
}
}
}
输出流实例
import java.io.*;
class Test{
public static void main(String args[]){
//声明输入流引用
FileInputStream fis = null;
//声明输出流引用
FileOutputStream fos = null;
try{
//生成代表输入流的对象
fis = new FileInputStream("d:/work/src/from.txt");
//生成代表输出流的对象
fos = new FileOutputStream("d:/work/src/to.txt");
//生成一个字节数组
byte [] buffer = new byte[100];
//调用输入对象的read方法,读取字节数组的数据
int temp = fis.read(buffer,0,buffer.length);
//temp临时定义用来接收read返回值类型,从而判断写入多少数据
fos.write(buffer,0,temp);
}
catch(Exception e){
System.out.println(e);
}
}
}
大文件的读写方法
import java.io.*;
class Test{
public static void main(String args[]){
//声明输入流引用
FileInputStream fis = null;
//声明输出流引用
FileOutputStream fos = null;
try{
//生成代表输入流的对象
fis = new FileInputStream("d:/work/src/from.txt");
//生成代表输出流的对象
fos = new FileOutputStream("d:/work/src/to.txt");
//生成一个字节数组
byte [] buffer = new byte[1024];
while(true){
//调用输入对象的read方法,读取字节数组的数据
int temp = fis.read(buffer,0,buffer.length);
if(temp == -1){
break;
}
//调用输出流的write方法,写入字节数组的数据
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);
}
}
}
}
**字符流读取文件和写入文件的方法**
import java.io.*;
public class TestChar{
public static void main(String argsp[]){
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("d:/work/src/from.txt");
fw = new FileWriter("d:/work/src/to.txt");
//与字节流读取的差别
char [] buffer = new char[100];
int temp = fr.read(buffer,0,buffer.length);
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);
}
}
}
}
大文件的读写方法(Buffer)
import java.io.*;
class Test{
public static void main(String args[]){
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try{
fileReader = new FileReader("d:/work/src/users.txt");
//以上是把fileReader对象传入bufferedReader引用中
bufferedReader = new BufferedReader(fileReader);
String line = null;//定义一个空值的line引用(对象)
while(true){
line = bufferedReader.readLine();//这是一个while循环
if(line != null){
<span style="line-height: 29.7px; font-family: 'microsoft yahei'; white-space: pre-wrap;">System.out.println(line);</span>
}
}
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
bufferedReader.close();
fileReader.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}
这里的bufferedReader是一个装饰者的角色,并不是真正去读取文件,而是装饰fileReader去读取文件,同样的也可装饰键盘等读取其他数据。