java IO流_文件操作大全

java 文件操作实例(列出文件目录,创建文件,按字节读入数据,按行读入数据等)
2008年07月23日 星期三 11:30

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.Reader;
import java.util.List;

 

public class Testfile {
public static void main(String[] args) {
  
/*=======读取并写入文件===============================*/
//   try {
// //   FileInputStream f = new FileInputStream("f:\\test\\test.txt");
//   
//    FileReader rd=new FileReader("f:\\test\\aa.txt");
//    BufferedReader br=new BufferedReader(rd);
//    File ff=new File("f:\\");
//    int i=0;
//    File[] files=ff.listFiles();
//    for(File aa:files){
//     System.out.println(aa);
//     i++;
//    }
//    System.out.print("共有文件或文件夹"+i+"个");
//   
    boolean xx=ff.createNewFile();
//   
//    System.out.print(ff.getName()+"        "+ff.canRead());
   
//    if (xx==true)System.out.println("文件创建成功,开始写入数据");
//    else System.out.println("文件已经存在");
//    FileWriter fw=new FileWriter("f:\\test\\test.dat");
//    BufferedWriter bw=new BufferedWriter(fw);
//    String x;      
//    String s;
//    int i=0;
//    while((s=br.readLine())!=null){
//     if(i!=0)System.out.print("\n");
//     System.out.print(s);
//     bw.flush();
//     if(i!=0)bw.newLine();
//     bw.write(s);
//     if(i==0)i++;
//
//    }
//    bw.close();
//    fw.close();
//    br.close();
//    rd.close();
//
//   } catch (FileNotFoundException e) {
//    // TODO Auto-generated catch block
//    e.printStackTrace();
//   } catch (IOException e) {
//    // TODO Auto-generated catch block
//    e.printStackTrace();
//   }
/*========end读取并写入文件==============================*/

/**======文本文件的创建=========================
   File ff=new File("f:\\test\\test.txt");
   try {
    boolean exists=ff.createNewFile();
//    System.out.print(exists);
    if(exists==false)System.out.println("文件已存在");
    else System.out.println("文件创建成功");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
=========end文本文件的创建==============================*/

/**==文本文件按指定字节数读取操作===================================*/

   try {
    FileInputStream f = new FileInputStream("f:\\test\\aa.txt");
    int len=f.available();
    byte[] b=new byte[len];
  
    int byteRead = f.read(b);
    while (byteRead!=-1) {
     String x=new String(b,"GBK");
     System.out.print(x);
     byteRead = f.read(b);
    }
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
/*==end文本文件读取操作===================================*/
  
/**==文本文件按单字节读取操作===================================

   try {
    FileInputStream f = new FileInputStream("f:\\test\\test.txt");
  
    int byteRead = f.read();
    while (byteRead!=-1) {
    
     System.out.print((char)byteRead);
     byteRead = f.read();
    }
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
==end文本文件读取操作===================================*/  
}
}

/

 

Java的IO操作都是基于流进行操作的,为了提高读写效率一般需要进行缓冲。
    简单的示例程序如下:
   
    /**
 * 读出1.txt中的内容,写入2.txt中
 *
 */
import java.io.*;
public class ReadWriteFile{
 public static void main(String[] args){
  try{
  
   File read = new File("c:\\1.txt");
   File write = new File("c:\\2.txt");
  
   BufferedReader br = new BufferedReader(
         new FileReader(read));
   BufferedWriter bw = new BufferedWriter(
         new FileWriter(write));
   String temp = null;
   temp = br.readLine();
   while(temp != null){
    //写文件
    bw.write(temp + "\r\n"); //只适用Windows系统
    //继续读文件
    temp = br.readLine();
   }
  
   bw.close();
   br.close();
  
  }catch(FileNotFoundException e){ //文件未找到
   System.out.println (e);
  }catch(IOException e){
   System.out.println (e);
  }
 }
}

文章出处:DIY部落(http://www.diybl.com/course/3_program/java/javashl/20090306/158600.html)

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值