JAVA读写文件操作

一、java读文件:


public class ReadFromFile {
	/**
	 * 读取文件
	 * 
	 * @param fileName
	 */
	public static void readFile(String fileName) {
		String lineStr = "";
		BufferedReader bf = null;
		try {
			bf = new BufferedReader(new FileReader(fileName));
			while ((lineStr = bf.readLine()) != null) {
				if (!(lineStr.trim()).equals("")) {
					System.out.println(lineStr);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(bf!=null){
				try {
					bf.close();
					bf = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


	/**
	 * 设置读取文件编码
	 * 
	 * @param fileName
	 */
	public static void readFileCharset(String fileName) {

		String lineStr = "";
		BufferedReader bf = null;
		File file = new File(fileName);
		try {
			bf = new BufferedReader(new InputStreamReader(new FileInputStream(
					file), "UTF-8"));
			while ((lineStr = bf.readLine()) != null) {
				if (!(lineStr.trim()).equals("")) {
					System.out.println(lineStr);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(bf!=null){
				try {
					bf.close();
					bf = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
    /**
     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
     */
    public static void readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        try {
            System.out.println("以字节为单位读取文件内容,一次读一个字节:");
            // 一次读一个字节
            in = new FileInputStream(file);
            int tempbyte;
            while ((tempbyte = in.read()) != -1) {
                System.out.write(tempbyte);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        try {
            System.out.println("以字节为单位读取文件内容,一次读多个字节:");
            // 一次读多个字节
            byte[] tempbytes = new byte[100];
            int byteread = 0;
            in = new FileInputStream(fileName);
            ReadFromFile.showAvailableBytes(in);
            // 读入多个字节到字节数组中,byteread为一次读入的字节数
            while ((byteread = in.read(tempbytes)) != -1) {
                System.out.write(tempbytes, 0, byteread);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 以字符为单位读取文件,常用于读文本,数字等类型的文件
     */
    public static void readFileByChars(String fileName) {
        File file = new File(fileName);
        Reader reader = null;
        try {
            System.out.println("以字符为单位读取文件内容,一次读一个字节:");
            // 一次读一个字符
            reader = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
                // 但如果这两个字符分开显示时,会换两次行。
                // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
                if (((char) tempchar) != '\r') {
                    System.out.print((char) tempchar);
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            System.out.println("以字符为单位读取文件内容,一次读多个字节:");
            // 一次读多个字符
            char[] tempchars = new char[30];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(fileName));
            // 读入多个字符到字符数组中,charread为一次读取字符数
            while ((charread = reader.read(tempchars)) != -1) {
                // 同样屏蔽掉\r不显示
                if ((charread == tempchars.length)
                        && (tempchars[tempchars.length - 1] != '\r')) {
                    System.out.print(tempchars);
                } else {
                    for (int i = 0; i < charread; i++) {
                        if (tempchars[i] == '\r') {
                            continue;
                        } else {
                            System.out.print(tempchars[i]);
                        }
                    }
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 以行为单位读取文件,常用于读面向行的格式化文件
     */
    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 随机读取文件内容
     */
    public static void readFileByRandomAccess(String fileName) {
        RandomAccessFile randomFile = null;
        try {
            System.out.println("随机读取一段文件内容:");
            // 打开一个随机访问文件流,按只读方式
            randomFile = new RandomAccessFile(fileName, "r");
            // 文件长度,字节数
            long fileLength = randomFile.length();
            // 读文件的起始位置
            int beginIndex = (fileLength > 4) ? 4 : 0;
            // 将读文件的开始位置移到beginIndex位置。
            randomFile.seek(beginIndex);
            byte[] bytes = new byte[10];
            int byteread = 0;
            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
            // 将一次读取的字节数赋给byteread
            while ((byteread = randomFile.read(bytes)) != -1) {
                System.out.write(bytes, 0, byteread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomFile != null) {
                try {
                    randomFile.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 显示输入流中还剩的字节数
     */
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("当前字节输入流中的字节数为:" + in.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String fileName = "C:/temp/newTemp.txt";
        ReadFromFile.readFileByBytes(fileName);
        ReadFromFile.readFileByChars(fileName);
        ReadFromFile.readFileByLines(fileName);
        ReadFromFile.readFileByRandomAccess(fileName);
    }
}

5、将内容追加到文件尾部
public class AppendToFile {
    /**
     * A方法追加文件:使用RandomAccessFile
     */
    public static void appendMethodA(String fileName, String content) {
        try {
            // 打开一个随机访问文件流,按读写方式
            RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
            // 文件长度,字节数
            long fileLength = randomFile.length();
            //将写文件指针移到文件尾。
            randomFile.seek(fileLength);
            randomFile.writeBytes(content);
            randomFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * B方法追加文件:使用FileWriter
     */
    public static void appendMethodB(String fileName, String content) {
        try {
            //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String fileName = "C:/temp/newTemp.txt";
        String content = "new append!";
        //按方法A追加文件
        AppendToFile.appendMethodA(fileName, content);
        AppendToFile.appendMethodA(fileName, "append end. \n");
        //显示文件内容
        ReadFromFile.readFileByLines(fileName);
        //按方法B追加文件
        AppendToFile.appendMethodB(fileName, content);
        AppendToFile.appendMethodB(fileName, "append end. \n");
        //显示文件内容
        ReadFromFile.readFileByLines(fileName);
    }
}


二、java写文件:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class TxtFile {
public void read() {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("F://a.txt");
br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();// 关闭文件
} catch (Exception e) {
System.out.println(e);
}
}
}

public void write() {
File file = null;
FileWriter fw = null;
try {
file = new File("F://a.txt");
fw = new FileWriter(file);
for (int i = 0; i < 20; i++) {
fw.append("第" + i + 1 + "次");
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (fw != null)
fw.close();// 关闭文件
} catch (Exception e) {
System.out.println(e);
}
}
}
}

三、Java文件读写总结3各方法效率测试

读文件:

FileInputStream

通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名name 指定。创建一个新 FileDescriptor 对象来表示此文件连接。


InputStreamReader

是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,否则可能接受平台默认的字符集。


BufferedReader
从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。


StringBuffer

线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。

public static void main(String[] args) {

 //读取文件内容
            File a = new File("C:/add2.txt");
            if(a.exists()){
             FileInputStream fi = new FileInputStream(a);
             InputStreamReader isr = new InputStreamReader(fi, "GBk");
             BufferedReader bfin = new BufferedReader(isr);
             String rLine = "";
             while((rLine = bfin.readLine())!=null){
              System.out.println(rLine);
             }
            }

}

写文件:

 在java写文件中,通常会使用FileOutputStream和FileWriter,FileWriter只能写文本文件。FileOutputStream也经常结合BufferedOutputStream。因为实际应用中写文本文件的情况占了大多数。所以下面测试用不同的方式生成一个相同行数、大小相同的文件的三种不同方式。

import java.io.File;
import java.io.FileOutputStream;
import java.io.*;

public class FileTest {
    publicFileTest() {
    }

    publicstatic void main(String[] args) {
       FileOutputStream out = null;
       FileOutputStream outSTr = null;
       BufferedOutputStream Buff=null;
       FileWriter fw = null;
       int count=1000;//写文件行数
       try {
           out = new FileOutputStream(new File("C:/add.txt"));
           long begin = System.currentTimeMillis();
           for (int i = 0; i < count; i++) {
               out.write("测试java 文件操作\r\n".getBytes());
           }
           out.close();
           long end = System.currentTimeMillis();
           System.out.println("FileOutputStream执行耗时:" + (end - begin) + "豪秒");

           outSTr = new FileOutputStream(new File("C:/add0.txt"));
            Buff=new BufferedOutputStream(outSTr);
           long begin0 = System.currentTimeMillis();
           for (int i = 0; i < count; i++) {
               Buff.write("测试java 文件操作\r\n".getBytes());
           }
           Buff.flush();
           Buff.close();
           long end0 = System.currentTimeMillis();
           System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) +" 豪秒");


           fw = new FileWriter("C:/add2.txt");
           long begin3 = System.currentTimeMillis();
           for (int i = 0; i < count; i++) {
               fw.write("测试java 文件操作\r\n");
           }
                       fw.close();
           long end3 = System.currentTimeMillis();
           System.out.println("FileWriter执行耗时:" + (end3 - begin3) + "豪秒");

       } catch (Exception e) {
           e.printStackTrace();
       }
       finally {
           try {
               fw.close();
               Buff.close();
               outSTr.close();
               out.close();
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
    }


}   

以下结果经过多次执行,取常出现的数据,由于只是简单比较,不做详细统计。
1.当count=1000的,即写文件1000行的时候,写出的文件大小为18.5KB:
FileOutputStream执行耗时:46 豪秒
BufferedOutputStream执行耗时:31 豪秒
FileWriter执行耗时:15 豪秒

2.当count=10000的,即写文件10000行的时候,写出的文件大小为185KB:
FileOutputStream执行耗时:188 豪秒
BufferedOutputStream执行耗时:32 豪秒
FileWriter执行耗时:16 豪秒

3.当count=100000的,即写文件100000行的时候,写出的文件大小为1856KB:
FileOutputStream执行耗时:1266 豪秒
BufferedOutputStream执行耗时:125 豪秒
FileWriter执行耗时:93 豪秒

4.当count=1000000的,即写文件1000000行的时候,写出的文件大小为18555KB:
FileOutputStream执行耗时:12063 豪秒
BufferedOutputStream执行耗时:1484 豪秒
FileWriter执行耗时:969 豪秒

由以上数据可以看到,如果不用缓冲流BufferedOutputStream,FileOutputStream写文件的鲁棒性是很不好的。当写1000000行的文件的时候,FileOutputStream比FileWriter要慢11094毫秒(11秒),BufferedOutputStream比FileWriter慢515毫秒。

不要小看这几秒的时间。当操作的数据量很大的时候,这点性能的差距就会很大了。在通用数据迁移工具导出数据库2千万条记录生成sql脚本文件的时候,性能性能相差10分钟以上。

四、Java创建文件及文件夹

    package Test;


    import java.io.File;
    import java.io.IOException;

    public class CreateFileTest {
    /**
    * 创建单个文件
    * @param destFileName 文件名
    * @return 创建成功返回true,否则返回false
    */
    public static boolean CreateFile(String destFileName) {
       File file = new File(destFileName);
       
       if (file.exists()) {
        System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
        return false;
       }
       
       
       if (destFileName.endsWith(File.separator)) {
        System.out.println("创建单个文件" + destFileName + "失败,目标不能是目录!");
        return false;
       }
       
       if (!file.getParentFile().exists()) {
        System.out.println("目标文件所在路径不存在,准备创建。。。");
        if (!file.getParentFile().mkdirs()) {
       
        System.out.println("创建目录文件所在的目录失败!");
        return false;
        }
       }
       
    // 创建目标文件
       try {
        if (file.createNewFile()) {
        System.out.println("创建单个文件" + destFileName + "成功!");
        return true;
        } else {
        System.out.println("创建单个文件" + destFileName + "失败!");
        return false;
        }
       } catch (IOException e) {
        e.printStackTrace();
        System.out.println("创建单个文件" + destFileName + "失败!");
        return false;
       }
    }
    /**
    * 创建目录
    * @param destDirName 目标目录名
    * @return 目录创建成功返回true,否则返回false
    */
    public static boolean createDir(String destDirName) {
       File dir = new File(destDirName);
       if(dir.exists()) {
        System.out.println("创建目录" + destDirName + "失败,目标目录已存在!");
        return false;
       }
       if(!destDirName.endsWith(File.separator))
        destDirName = destDirName + File.separator;
       // 创建单个目录
       if(dir.mkdirs()) {
        System.out.println("创建目录" + destDirName + "成功!");
        return true;
       } else {
        System.out.println("创建目录" + destDirName + "成功!");
        return false;
       }
    }

    /**
    * 创建临时文件
    * @param prefix 临时文件的前缀
    * @param suffix 临时文件的后缀
    * @param dirName 临时文件所在的目录,如果输入null,则在用户的文档目录下创建临时文件
    * @return 临时文件创建成功返回抽象路径名的规范路径名字符串,否则返回null
    */
    public static String createTempFile(String prefix, String suffix, String dirName) {
       File tempFile = null;
       try{
        if(dirName == null) {
        // 在默认文件夹下创建临时文件
        tempFile = File.createTempFile(prefix, suffix);
        return tempFile.getCanonicalPath();
        }else {
        File dir = new File(dirName);
        // 如果临时文件所在目录不存在,首先创建
        if(!dir.exists()) {
        if(!CreateFileTest.createDir(dirName)){
        System.out.println("创建临时文件失败,不能创建临时文件所在目录!");
        return null;
        }
        }
        tempFile = File.createTempFile(prefix, suffix, dir);
        return tempFile.getCanonicalPath();
        }
       
       } catch(IOException e) {
        e.printStackTrace();
        System.out.println("创建临时文件失败" + e.getMessage());
        return null;
       }
    }
    public static void main(String[] args) {
    // 创建目录
       String dirName = "d:/test/test0/test1";
       CreateFileTest.createDir(dirName);
       // 创建文件
       String fileName = dirName + "/test2/testFile.txt";
       CreateFileTest.CreateFile(fileName);
       // 创建临时文件
       String prefix = "temp";
       String suffix = ".txt";
       for(int i = 0; i < 10; i++) {
        System.out.println("创建了临时文件:" + CreateFileTest.createTempFile(prefix, suffix, dirName));
       }
    }
    }



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值