⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.java

📁 jaguey,网上的一个朋友给我的
💻 JAVA
字号:
package net.javapassion.jaguey.core;

import java.io.*;

//版本: JagueyBBS 1.1
//功能: 论坛文件输入输出流操作
//作者: 赵程佳
//时间: 2006-02-08 19:16:39

public class FileIO {
	
	public static String readFile(String filePath) {
		String result = "";
		try {
			File file = new File(filePath);
			if (file.exists()) {
				FileInputStream fileInputStream = new FileInputStream(file);
				int len = fileInputStream.available();
				byte[] str = new byte[len];
				if (fileInputStream.read(str) == -1) {
					result = "";
				} else {
					result = new String(str);
				}
				fileInputStream.close();
				fileInputStream = null;
			}
			file = null;
		} catch (IOException ioe) {
			Log.error(ioe.getMessage());
			result = null;
		}
		return result;
	}
	
	public static String readFile(String filePath, String charset) {
		String result = "";
		try {
			File file = new File(filePath);
			if (file.exists()) {
				FileInputStream fileInputStream = new FileInputStream(file);
				int len = fileInputStream.available();
				byte[] str = new byte[len];
				if (fileInputStream.read(str) == -1) {
					result = "";
				} else {
					result = new String(str, charset);
				}
				fileInputStream.close();
				fileInputStream = null;
			}
			file = null;
		} catch (IOException ioe) {
			Log.error(ioe.getMessage());
			result = null;
		}
		return result;
	}
	
	public static void writeFile(String msg, String filePath) {
		try {
			File file = new File(filePath);
			if (file.exists()) {
		    	file.delete();
		    }
			FileOutputStream fileOutputStream = new FileOutputStream(filePath);
			fileOutputStream.write(msg.getBytes());
			fileOutputStream.close();
			file = null;
			fileOutputStream = null;
		} catch (IOException ioe) {
			Log.error(ioe.getMessage());
		}
	}
	
	public static void writeFile(String msg, String filePath, String charset) {
		try {
			File file = new File(filePath);
			if (file.exists()) {
		        file.delete();
		    }
			FileOutputStream fileOutputStream = new FileOutputStream(filePath);
			fileOutputStream.write(msg.getBytes(charset));
			fileOutputStream.close();
			file = null;
			fileOutputStream = null;
		} catch (IOException ioe) {
			Log.error(ioe.getMessage());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -