<Java> File文件操作

Java的文件读取使用File类, 进行读取:

exists(): 判断文件存在与否;

createNewFile(): 创建新的文件;

isDirectory(): 是否是文件夹;

FileOutputStream: 写文件;

FileInputStream: 读文件;

	
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class JavaTest {

	public static void main(String args[]) {
		FtDebug.setFileName("test.txt");
		
		FtDebug.init();
		FtDebug.writeLog("this is the first log");
		FtDebug.close();

		System.out.println(FtDebug.getContent("test.txt"));
	}


	public static class Log {
		public static void e(String tag, String log) {
			System.out.println(log);
		}
	}

	public static class FtDebug {
		private static final String TAG = "DEBUG";
		private static  String debugFileName = null;
		private static  String debugFileBakName = null;
		private static File fileDebug = null;
		private static FileOutputStream fosDebug = null;
		
		public static void setFileName(String name) {
			debugFileName = name;
			debugFileBakName = debugFileName + "_bak";
		}
		public static boolean init() {
			if (debugFileName == null) {
				return false;
			}
			if ((fileDebug != null) && fileDebug.exists()) {
				fileDebug.renameTo(new File(debugFileBakName));	// move file to backup file, delete file
				fileDebug.delete();
			}
			fileDebug = new File(debugFileName);
			try {
				if (fileDebug.exists()) {
					fileDebug.renameTo(new File(debugFileBakName));	// move file to backup file, delete file
					fileDebug.delete();
				}
				if (!fileDebug.createNewFile()) {
					Log.e(TAG, "create debug file failed!");
					return false;
				}
				fosDebug = new FileOutputStream(fileDebug);
				if (fosDebug == null) {
					Log.e(TAG, "create file output stream failed!");
					return false;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e(TAG, "create debug file exception!");
				return false;
			}
			return true;
		}
		public static void addTimeStamp() {
			
		}

		public static void writeLog(String log) {
			try {
				String timeStamp = String.format("<%d>", System.currentTimeMillis());
				fosDebug.write(timeStamp.getBytes());  // add time stamp
				fosDebug.write(log.getBytes());
				fosDebug.write(new byte[]{'\r', '\n'});
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e(TAG, "write file output stream exception!");
			}
		}
		
		public static void close() {
			try {
				fosDebug.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e(TAG, "close file output stream exception!");
			}
		}
		
		public static String getContent(String fileName) {
			File file = new File(fileName);
			StringBuffer content = new StringBuffer();
			if (!file.exists()) {
				return "";
			}
			try {
				FileInputStream fis = new FileInputStream(file);
				int character;
				
				while ( (character = fis.read()) != -1) {
					content.append((char)character);
				}
				fis.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return content.toString();
		}
	}

	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值