<Java> File文件操作
来源:互联网 发布:如何看淘宝价格曲线 编辑:程序博客网 时间:2024/05/16 19:39
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 filefileDebug.delete();}fileDebug = new File(debugFileName);try {if (fileDebug.exists()) {fileDebug.renameTo(new File(debugFileBakName));// move file to backup file, delete filefileDebug.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 blocke.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 stampfosDebug.write(log.getBytes());fosDebug.write(new byte[]{'\r', '\n'});} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();Log.e(TAG, "write file output stream exception!");}}public static void close() {try {fosDebug.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return content.toString();}}}
0 0
- <Java> File文件操作
- Java File文件操作
- JAVA FILE文件操作
- Java File文件操作
- java的文件File操作
- Java文件file操作总结
- Java文件file操作总结
- Java文件file操作总结
- Java文件file操作总结
- 文件操作---java.io.File
- Java的File文件操作
- Java:文件操作类File
- 【JAVA】文件操作(File)
- java file 文件操作 operate file of java
- 浅谈JAVA文件操作(File类)
- java File(dir, delete, move...) 文件操作
- java.io.file 各种文件操作
- java.io.file 各种文件操作
- 【转稿】:农村孩子为什么没有春天
- 手记NO1
- jsp自动登录设计思路(相对来说比较安全)
- ListView异步加载图片方法和滚动优化
- 【MATLAB工程人员的福音】MATLAB R2015b的Coder工具使用实例(一)
- <Java> File文件操作
- macbook 安装win8 双系统
- linux内核网络子系统
- 多例模式【Multition Pattern 】
- 使用PHP生成数据字典
- Android onLongClick事件触发的问题
- django cache的简单使用
- CentOS7.0下安装 mysql5.6
- Memcached的安装和使用