Java Io读写实例
2015-06-13 19:25
471 查看
Java读写IO方法总结
最近学习Java读写文件,总结了以下几种方法:
(1) 个人认为效率最高的还是使用BufferedReader和BufferedWriter读写效率更高。
① BufferedReader从文件中读取文件。传入的参数分别为路径名与文件名。
public static String ReadeFileBuffered(StringstrPath,String FileName) throws FileNotFoundException
{
Stringstr=null;
Filefile=new File(strPath,FileName);
//判断文件不存在或者目录不存在存在
if(!file.exists()||!file.isDirectory())
{
thrownew FileNotFoundException();
}
Readerinfile=new FileReader(file);
BufferedReaderreader=new BufferedReader(infile);
try{
str=reader.readLine();
reader.close();
infile.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnstr;
}
② 使用BufferedWriter把数据写入文件中。传入的参数分别为路径名与文件名,还有要写进文件的内容。
public static booleanWriteFileBuffered(String strPath,String FileName,String strConcern) throwsIOException
{
Filefile=new File(strPath,FileName);
Writeroutfile=new FileWriter(file);
BufferedWriterwriter=new BufferedWriter(outfile);
writer.write(strConcern);
writer.newLine();
writer.close();
outfile.close();
returntrue;
}
(2) 最基本的应该是使用文件字节输入输出流了。就是FileInputStream和FileoutputStream。
① 其中FileInputStream的使用方法如下:
public static String ReadFile(StringstrPath,String FileName)
{
Stringstr="";
Filefile=new File(strPath,FileName);
if(!file.exists()||file.isDirectory())
{
try{
thrownew FileNotFoundException();
}catch (FileNotFoundException e) {
// TODO Auto-generated catchblock
System.out.println("error:"+e);
}
}
try{
FileInputStreaminfile=new FileInputStream(file);
byte[] b=new byte[1024];
StringBuffersb=new StringBuffer();
try{
while(infile.read(b)!=-1)
{
sb.append(newString(b));
b=newbyte[1024];
}
str=sb.toString();
}catch (IOException e) {
//TODO Auto-generated catch block
System.out.println("error:"+e);
}
}catch (FileNotFoundException e) {
//TODO Auto-generated catch block
System.out.println("error:"+e);
}
returnstr;
}
② 其中FileInputStream的使用方法如下:
//use FileOutputStream Write IO
publicstatic boolean WriteFile(String strPath,String FileName,String strConcern)
{
try
{
Filefile=new File(strPath,FileName);
FileOutputStreamoutfile=new FileOutputStream(file);
//System.out.println(outfile.);
byte[]str1=strConcern.getBytes();
outfile.write(str1);
outfile.close();
}
catch(FileNotFoundExceptione)
{
returnfalse;
}catch (IOException e) {
//TODO Auto-generated catch block
System.out.println("error+"+e);
returnfalse;
}
returntrue;
}
(3) 第三个方法便是使用PrintStream写文件。代码实例如下:
//use PrintStream write IO
@SuppressWarnings("resource")
publicstatic boolean WriteFilePrintStream(String strPath,String FileName,StringstrConcern)
{
Filefile=new File(strPath,FileName);
//if(file.exists()||file.isDirectory())
FileOutputStreamoutfile = null;
PrintStreamp=new PrintStream(outfile);
try{
outfile= new FileOutputStream(file);
p.println(strConcern);
try{
outfile.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returntrue;
}
(4) 使用文件字符输入输出流:
public static void WriteAndRead()
{
FilesourceFile=new File("a.txt");
FiletargetFile=new File("b.txt");
chararr[]=new char[9];
try{
Writerw=new FileWriter(targetFile,true);
Readerr=new FileReader(sourceFile);
intn=-1;
while((n=r.read(arr))!=-1)
{
w.write(arr,0,n);
}
w.flush();
w.close();
}
catch(IOExceptione)
{
System.out.println("Error"+e);
}
}
(5) 使用随机流读写文件,不需要专门定义是读文件还是写文件,只需要一个随机流解决掉问题,既能读也能写,功能就是强大。
public static void RandomWriteAndRead()throws IOException
{
RandomAccessFileinAndOut=null;
intdata[]={1,2,3,4,5,6,7,8,9,0};
try{
inAndOut=newRandomAccessFile("a.txt","rw");
for(inti=0;i<data.length;i++)
{
inAndOut.writeInt(data[i]);
}
for(inti=data.length-1;i>=0;i--)
{
inAndOut.seek(i*4);
System.out.printf("\t%d",inAndOut.readInt());
//每隔4个字节往前读一个整数
}
inAndOut.close();
}catch (FileNotFoundException e) {
//TODO Auto-generated catch block
System.out.println("Error"+e);
}
}
最近学习Java读写文件,总结了以下几种方法:
(1) 个人认为效率最高的还是使用BufferedReader和BufferedWriter读写效率更高。
① BufferedReader从文件中读取文件。传入的参数分别为路径名与文件名。
public static String ReadeFileBuffered(StringstrPath,String FileName) throws FileNotFoundException
{
Stringstr=null;
Filefile=new File(strPath,FileName);
//判断文件不存在或者目录不存在存在
if(!file.exists()||!file.isDirectory())
{
thrownew FileNotFoundException();
}
Readerinfile=new FileReader(file);
BufferedReaderreader=new BufferedReader(infile);
try{
str=reader.readLine();
reader.close();
infile.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnstr;
}
② 使用BufferedWriter把数据写入文件中。传入的参数分别为路径名与文件名,还有要写进文件的内容。
public static booleanWriteFileBuffered(String strPath,String FileName,String strConcern) throwsIOException
{
Filefile=new File(strPath,FileName);
Writeroutfile=new FileWriter(file);
BufferedWriterwriter=new BufferedWriter(outfile);
writer.write(strConcern);
writer.newLine();
writer.close();
outfile.close();
returntrue;
}
(2) 最基本的应该是使用文件字节输入输出流了。就是FileInputStream和FileoutputStream。
① 其中FileInputStream的使用方法如下:
public static String ReadFile(StringstrPath,String FileName)
{
Stringstr="";
Filefile=new File(strPath,FileName);
if(!file.exists()||file.isDirectory())
{
try{
thrownew FileNotFoundException();
}catch (FileNotFoundException e) {
// TODO Auto-generated catchblock
System.out.println("error:"+e);
}
}
try{
FileInputStreaminfile=new FileInputStream(file);
byte[] b=new byte[1024];
StringBuffersb=new StringBuffer();
try{
while(infile.read(b)!=-1)
{
sb.append(newString(b));
b=newbyte[1024];
}
str=sb.toString();
}catch (IOException e) {
//TODO Auto-generated catch block
System.out.println("error:"+e);
}
}catch (FileNotFoundException e) {
//TODO Auto-generated catch block
System.out.println("error:"+e);
}
returnstr;
}
② 其中FileInputStream的使用方法如下:
//use FileOutputStream Write IO
publicstatic boolean WriteFile(String strPath,String FileName,String strConcern)
{
try
{
Filefile=new File(strPath,FileName);
FileOutputStreamoutfile=new FileOutputStream(file);
//System.out.println(outfile.);
byte[]str1=strConcern.getBytes();
outfile.write(str1);
outfile.close();
}
catch(FileNotFoundExceptione)
{
returnfalse;
}catch (IOException e) {
//TODO Auto-generated catch block
System.out.println("error+"+e);
returnfalse;
}
returntrue;
}
(3) 第三个方法便是使用PrintStream写文件。代码实例如下:
//use PrintStream write IO
@SuppressWarnings("resource")
publicstatic boolean WriteFilePrintStream(String strPath,String FileName,StringstrConcern)
{
Filefile=new File(strPath,FileName);
//if(file.exists()||file.isDirectory())
FileOutputStreamoutfile = null;
PrintStreamp=new PrintStream(outfile);
try{
outfile= new FileOutputStream(file);
p.println(strConcern);
try{
outfile.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returntrue;
}
(4) 使用文件字符输入输出流:
public static void WriteAndRead()
{
FilesourceFile=new File("a.txt");
FiletargetFile=new File("b.txt");
chararr[]=new char[9];
try{
Writerw=new FileWriter(targetFile,true);
Readerr=new FileReader(sourceFile);
intn=-1;
while((n=r.read(arr))!=-1)
{
w.write(arr,0,n);
}
w.flush();
w.close();
}
catch(IOExceptione)
{
System.out.println("Error"+e);
}
}
(5) 使用随机流读写文件,不需要专门定义是读文件还是写文件,只需要一个随机流解决掉问题,既能读也能写,功能就是强大。
public static void RandomWriteAndRead()throws IOException
{
RandomAccessFileinAndOut=null;
intdata[]={1,2,3,4,5,6,7,8,9,0};
try{
inAndOut=newRandomAccessFile("a.txt","rw");
for(inti=0;i<data.length;i++)
{
inAndOut.writeInt(data[i]);
}
for(inti=data.length-1;i>=0;i--)
{
inAndOut.seek(i*4);
System.out.printf("\t%d",inAndOut.readInt());
//每隔4个字节往前读一个整数
}
inAndOut.close();
}catch (FileNotFoundException e) {
//TODO Auto-generated catch block
System.out.println("Error"+e);
}
}
相关文章推荐
- java I/O系统总结
- Spring之JDBC
- [ubuntu Setup] ubuntu 14.10 安装 JDK
- [Java基础] java多线程关于消费者和生产者
- Spring Bean的作用域
- Spring三种实例化Bean的方式
- java静态绑定和动态绑定(转载)
- java单例模式详解
- java虚拟机
- JAVA基础学习之多线程
- JAVA基础——正则表达式
- Struts1文件上传
- JAVA线程池
- Struts1防止表单重复提交
- Struts1表单校验
- Spring.net 在aps.net Web的配置复习
- spring中事务管理的几种方式
- 在eclipse中导入spring源码
- Struts1之编码问题
- Struts1之bean标签