标签:io java
下面是几个常用的几个IO对象。对于IO流的学习。只需要搞清楚每个类的作用,熟悉一两个类的操作基本,IO流学习就差不多了。
字符流
package IO;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* 字符流
* @author Administrator
*
*/
public class string_io {
/**
* 通过FileReader字符流读取数据
* @throws IOException
*
*/
public void filereader() throws IOException{
File file = new File("file.txt");
Reader reader = new FileReader(file);
char []buffer = new char[1024];
int len=0;
while((len = reader.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
reader.close();
}
/**
* 通过FileWriter字符流写数据
* @throws IOException
*/
public void filewriter() throws IOException{
File file = new File("file.txt");
Writer writer = new FileWriter(file,true);
writer.write("字符流");
writer.flush();
writer.close();
}
/**
* 缓冲流,BufferedReader一次读一行
* @throws IOException
*/
public void buffereReader() throws IOException{
BufferedReader br = new BufferedReader(new FileReader(new File("file.txt")));
String buffer;
while((buffer=br.readLine())!=null){
System.out.println(buffer);
}
br.close();
}
/**
* 缓冲流一次写一行
* @throws IOException
*/
public void buffereWriter() throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("file2.txt")));
bw.write("第一行");
bw.newLine(); //换行符
bw.write("第二行");
bw.newLine();
bw.close();
}
/**
* 缓冲流复制
*/
public void copy(String Resouce,String Goal) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(new File(Resouce)));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(Goal)));
String buffer;
while((buffer=br.readLine())!=null){
bw.write(buffer);
bw.newLine(); //换行符
}
br.close();
bw.close();
}
}
字节流
package IO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
**************字节流************
* @author Administrator
*
*/
public class byte_io {
private String file_path;
public byte_io(String file_path){
this.file_path = file_path;
}
/**
* FileInputStream类读取文件
* @throws IOException
*/
public void inputstream() throws IOException{
File file = new File(file_path);
FileInputStream fis = new FileInputStream(file);
byte []buffer = new byte[100];
int len=0;
while((len=fis.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
fis.close();
}
/**
* FlieOutputStream写文件
* 写文件
* @throws IOException
*/
public void outputstream() throws IOException{
File file = new File(file_path);
FileOutputStream fos = new FileOutputStream(file,true);
fos.write("我想大声高数你".getBytes());
fos.flush();
fos.close();
}
/**
* 通过上面的读写 来完成文件复制
* @param sourcefile
* @param goalfile
* @throws IOException
*/
public void copy(String Sourcefile,String Goalfile) throws IOException{
//创建输入流
File sourcefile = new File(Sourcefile);
FileInputStream fis = new FileInputStream(sourcefile);
//创建输出流
File goalfile = new File(Goalfile);
FileOutputStream fos = new FileOutputStream(goalfile);
//读取源文件,写入输入到目标文件
byte[]buffer = new byte[1024];
int len=0;
while((len=fis.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
/**
* 缓冲流是提高了效率,缓冲8M,文件的复制,字节流可以复制任意格式的文件
* @param Sourcefile
* @param Goalfile
* @throws IOException
*/
public void copy2(String Sourcefile,String Goalfile) throws IOException{
//声明缓冲输入流,读取文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(Sourcefile)));
//声明缓冲输出流,写文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(Goalfile)));
byte[] buffer = new byte[1024*2];
int len=0;
while((len=bis.read(buffer))!=-1){
bos.write(buffer, 0, len);
}
bis.close();
bos.close();
}
}
http://www.2cto.com/kf/201312/262036.html
http://blog.csdn.net/yczz/article/details/38761237
附上jdk1.6中文版下载地址,遇到不懂的类,没事多查查,翻翻。
http://download.csdn.net/detail/xiaomin1992/8652131
标签:io java
原文地址:http://blog.csdn.net/xiaomin1992/article/details/45419649