`
xiang37
  • 浏览: 414270 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Java文件操作

阅读更多

Java对文件的操作,提供了上百个类来操作。

 

对与我们常用的有,OutputStreamWriter、Writer、BufferedOutputStream、FileOutputStream以及FileInputStream、Reader、InputStreamReader等相关类

 

先做个比较:对文本文件操作时,我们一般使用FileReader、BufferedReader类;而非文件、即二进制文件操作时,我们使用InputStream以及相关类、而对于流的操作,在远程接口RMI,或者通信类的业务处理中,应用较多。

 

FileOutputStream以及BufferedOutputStream对文件写的操作;当操作大文件时,使用BufferedOutputStream的效率会明显比FileOutputStream高很多。

当然BufferedOutputStream继承自FilterOutputStream;所以其功能也相当的丰富。并且在很多应用中我们都用到了FilterOutputStream这个类,比如zip,jar包、RMI类和接口、crypto提供加密的类和接口等等

 

文件操作类中,也还有很多比较有用的类,比如文件流描述类FileDescriptor等等;有待挖掘

 

当有特殊需求是,我们可以继承上面的接口或者扩展上面的类,来实现特殊的业务。

 

 

package com.xiva.demo;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;

import org.junit.Test;

public class FileDemo {
	
	/**
	 * 
	 * @Description 删除文件
	 * @author XIVA
	 */
	@Test public void deleteFile(){
		File file = new File("src/_tian.jpg");
		boolean res = false;
		if(file.exists()){
			res = file.delete();
		}
		if(res){
			System.out.println("Delete success!");
		}else{
			System.out.println("Delete fail!");
		}
	}
	
	/**
	 * 
	 * @Description 文件重命名
	 * @author XIVA
	 */
	@Test public void renameFile(){
		File file = new File("src/_tian.jpg");
		boolean res = false;
		
		if(file.exists() && file.isFile()){
			res = file.renameTo(new File("src/tian.jpg"));
		}
		if(res){
			System.out.println("Copy success!");
		}else{
			System.out.println("Copy fail!");
		}
	}
	
	/**
	 * 
	 * @Description 读取文本文件内容
	 * @author XIVA
	 * @throws IOException
	 */
	@Test public void readFile() throws IOException{
		InputStream is = FileDemo.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");

//		File file = new File("src/content.txt");
//		FileReader fReader = new FileReader(file);

		//流编码才是字节编码
//		Reader isReader = new InputStreamReader(is, "UTF-8");
//		BufferedReader buffer = new BufferedReader(isReader);
//		String lineStr;
//		lineStr = buffer.readLine();
//		while(lineStr != null){
//			System.out.println(lineStr);
//			lineStr = buffer.readLine();
//		} 
		//Scanner类提供更多方便的方法,最为主要的是为文本文件操作提供便利的函数
		Scanner sc = new Scanner(is,"utf-8");
		while(sc.hasNextLine()){
			System.out.println(sc.nextLine());
		}
		
		is.close();
	}
	
	/**
	 * 
	 * @Description FileWriter写文件
	 * @author XIVA
	 * @throws IOException
	 */
	@Test public void writeFile() throws IOException{
		File file = new File("src/test.txt");
		if(!file.exists()){
			file.createNewFile();
		}
		
//		Reader isReader = new FileReader(file);
//		BufferedReader buffer = new BufferedReader(isReader);
//		String lineStr;
//		lineStr = buffer.readLine();
		
		Scanner sc = new Scanner(file,"utf-8");
		StringBuffer sBuffer = new StringBuffer();
		while (sc.hasNextLine()){
			sBuffer.append(sc.nextLine());
		}
		//append if true, then bytes will be written to the end of the file rather than the beginning
		FileWriter fw = new FileWriter(file,true);
		BufferedWriter bw = new BufferedWriter(fw);
		
		bw.append(sBuffer.toString());
		bw.write("I'm XIVA,我是xiva");
		bw.newLine();
		bw.write("I'm NJIT.Shot_Go,我是Shot_Go");
//		String message = "I'm NJIT.Shot_Go,我是Shot_Go";
//		for(char mc:message.toCharArray()){
//			bw.append(mc);
//		}
		
		bw.flush();
		bw.close();
		fw.close();
	}
	
	/**
	 * 
	 * @Description FileOutputStream写文件
	 * @author XIVA
	 * @throws IOException
	 */
	@Test public void writeFOSFile() throws IOException{
		File file = new File("src/test.txt");
		if(!file.exists()){
			file.createNewFile();
		}
		//append if true, then bytes will be written to the end of the file rather than the beginning
		FileOutputStream fOS = new FileOutputStream(file,true);
		BufferedOutputStream bOS = new BufferedOutputStream(fOS);
		for (int i=0; i<100000; i++){
			String message = "I'm NJIT.Shot_Go,我是Shot_Go";
			bOS.write(message.getBytes());
			bOS.flush();
			bOS.write("\r\n".getBytes());
		}
		bOS.close();
		fOS.close();
	}
	
	/**
	 * 
	 * @Description 文件列表
	 * @author XIVA
	 * @param rootFile
	 */
	public void listDictionary(File rootFile){
		File[] files = rootFile.listFiles();
		for (File file:files){
			if(file.isDirectory()){
				System.out.println("Add To Tree Node");
				listDictionary(file);
			}else{
				System.out.println(file.getPath());
			}
		}
	}
	
	@Test public void testListDictionary(){
		File rootFile = new File("E:"+ File.separatorChar +"liber");
		listDictionary(rootFile);
	}
	
}
 

而对文件的操作,总结为:

1、删除文件或文件夹

2、创建文件或文件夹

3、读取文本或者二进制文件

4、写文本或者二进制文件(对于写二进制文件,我认为在图像处理以及音频视频处理中会使用到)

5、重命名文件,遍历文件,移动文件等等

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics