Chinaunix首页 | 论坛 | 博客
  • 博客访问: 514253
  • 博文数量: 114
  • 博客积分: 271
  • 博客等级: 二等列兵
  • 技术积分: 733
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-24 13:40
文章分类
文章存档

2014年(5)

2013年(14)

2012年(95)

分类: Java

2012-07-06 09:57:27

java 文件操作
1、从文件读取数据并打印到控制台

点击(此处)折叠或打开

  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;

  3. public class File {

  4.     /**
  5.      * @param args
  6.      * @throws FileNotFoundException
  7.      */
  8.     public static void main(String[] args){
  9.         // TODO Auto-generated method stub
  10.         byte[] buff = new byte[5];
  11.         try {
  12.             FileInputStream fileinputstream = new FileInputStream("c://1.txt");
  13.             while (fileinputstream.read(buff) != -1){
  14.                 System.out.write(buff);
  15.             }
  16.         } catch (Exception e) {
  17.             // TODO Auto-generated catch block
  18.             e.printStackTrace();
  19.         }
  20.         
  21.     }
  22. }
2、写文件

点击(此处)折叠或打开

  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3. import java.io.PrintStream;

  4. public class Fileoutput {

  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         try {
  11.             FileOutputStream fileOutputStream = new FileOutputStream("c://1.txt");
  12.             PrintStream printStream = new PrintStream(fileOutputStream);
  13.             printStream.println("i'm fine!");
  14.             printStream.close();
  15.         } catch (Exception e) {
  16.             // TODO Auto-generated catch block
  17.             e.printStackTrace();
  18.         }
  19.         

  20.     }

  21. }
3、文件拷贝

点击(此处)折叠或打开

  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintStream;

  5. public class Copy {

  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         byte[] buff = new byte[100];
  12.         int n;
  13.         try {
  14.             FileOutputStream fileOutputStream = new FileOutputStream("c://2.txt");
  15.             FileInputStream fileInputStream = new FileInputStream("c://1.txt");
  16.             while ((n = fileInputStream.read(buff)) != -1){
  17.                 fileOutputStream.write(buff);
  18.             }
  19.             fileInputStream.close();
  20.             fileOutputStream.close();
  21.         } catch (Exception e) {
  22.             // TODO Auto-generated catch block
  23.             e.printStackTrace();
  24.         }
  25.     
  26.     }
  27. }
4、按行读取并显示

点击(此处)折叠或打开

  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;

  4. public class Fileline {

  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         try {
  11.             FileReader fileReader = new FileReader("c://1.txt");
  12.             BufferedReader bufferedReader = new BufferedReader(fileReader);
  13.             String content;
  14.             int count = 0;
  15.             while ((content = bufferedReader.readLine()) != null){
  16.                 count++;
  17.                 System.out.print("第"+count+"行:"+content+"\n");
  18.             }
  19.         } catch (Exception e) {
  20.             // TODO Auto-generated catch block
  21.             e.printStackTrace();
  22.         }
  23.         

  24.     }

  25. }
5、随机访问文件

点击(此处)折叠或打开

  1. import java.io.FileNotFoundException;
  2. import java.io.RandomAccessFile;
  3. import java.util.RandomAccess;

  4. public class FileAccess {

  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         try {
  11.             RandomAccessFile accessFile = new RandomAccessFile("c://1.txt", "rw");
  12.             accessFile.seek(accessFile.length());
  13.             accessFile.writeBytes(" ok");
  14.             accessFile.seek(0);
  15.             System.out.print(accessFile.readLine());
  16.             
  17.         } catch (Exception e) {
  18.             // TODO Auto-generated catch block
  19.             e.printStackTrace();
  20.         }
  21.         

  22.     }

  23. }

阅读(1355) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~