Java 文件操作

//自我复习,Java 文件操作

import java.awt.List;
import java.io.BufferedOutputStream;
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.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

 


public class FileOperation {

	boolean createFile(String filePath){
		boolean result = false;
		File file = new File(filePath);
		if(!file.exists()){
			try {
				result = file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
	
	boolean deleteFile(String pathname){
		boolean result = false;
		File file = new File(pathname);
		if(file.exists() && file.isFile()){
			result = file.delete();
		}
		return result;
	}
	
	String readFileByByte(String pathname){
		File file = new File(pathname);
		FileInputStream fileInputStream = null;
		if(!file.exists() || !file.isFile()){
			return null;
		}
		StringBuffer content = new StringBuffer();
		byte[] temp = new byte[1024];
		try {
			fileInputStream = new FileInputStream(file);
			while(fileInputStream.read(temp) != -1){
				content.append(new String(temp));
				temp = new byte[1024];
			}
			fileInputStream.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} 
		
		return content.toString();
	}
	
	String readFileByChar(String filePath){
		File file = new File(filePath);
		if(!file.exists() || !file.isFile()){
			return null;
		}
		
		StringBuffer content = new StringBuffer();
		char[] temp = new char[1024];
		FileInputStream fileInputStream;
		try {
			fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader =  new InputStreamReader(fileInputStream, "utf-8");


			while(inputStreamReader.read(temp) != -1){
				content.append(new String(temp));
				temp = new char[1024];
			}

			fileInputStream.close();
			inputStreamReader.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return content.toString();
		
	}
	
	ArrayList<String> readFileByLine(String filePath){
		File file = new File(filePath);
		if(!file.exists() && !file.isFile()){
			return null;
		}
		ArrayList<String> content = new ArrayList<String>();
		
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
			BufferedReader reader = new BufferedReader(inputStreamReader);
			String lineContent = "";
			while( (lineContent = reader.readLine() ) != null){
				content.add(lineContent);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return content;
	}
	
	void writeFileByByte(String filePath,String content){
		File file = new File(filePath);
		FileOutputStream fos = null;
		synchronized (this) {
			
			try {
				fos = new FileOutputStream(file);
				fos.write(content.getBytes());
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally{}		
			
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	void writeFileByBufferedOutputStream(String filePath, String content){
		File file = new File(filePath);
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(content.getBytes());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally{
			try {
				fos.close();
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
	}

/*
 *  InputStreamReader  An InputStreamReader is a bridge from byte streams to character streams
 *  OutputStreamWriter An OutputStreamWriter is a bridge from character streams to byte streams
 * */	
	
	void writeFileByBufferedWrite(String filePath){
		File file = new File(filePath);

		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)  {
		
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值