java学习:文件读写
java中有好几种读写文件的方法,但是个人觉得最简单的还是FileInputStream、FileOutputStream类,示例代码:
package jmyang.file;
import java.io.*;
public class FileTest {
    
    /*
     * 删除文件
     */
    public static boolean delete(String fileName){
        boolean result = false;
        File f = new File(fileName);
        if (f.exists()){
            try{
                result = f.delete();
            }
            catch(Exception e){
                e.printStackTrace();
            }           
        }
        else{
            result = true;
        }
        return result;
    }
    
    /*
     * 读取文件
     */
    public static String read(String fileName) {
        File f = new File(fileName);
        if (!f.exists()) {
            return "File not found!";
        }
        FileInputStream fs;
        String result = null;
        try {
            fs = new FileInputStream(f);
            byte[] b = new byte[fs.available()];
            fs.read(b);
            fs.close();
            result = new String(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /*
     *写文件
     */
    public static boolean write(String fileName, String fileContent) {
        boolean result = false;
        File f = new File(fileName);
        try {
            FileOutputStream fs = new FileOutputStream(f);
            byte[] b = fileContent.getBytes();
            fs.write(b);
            fs.flush();
            fs.close();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /*
     * 追加内容到文件
     */
    public static boolean append(String fileName, String fileContent) {
        boolean result = false;
        File f = new File(fileName);
        try {
            if (f.exists()) {
                FileInputStream fsIn = new FileInputStream(f);
                byte[] bIn = new byte[fsIn.available()];
                fsIn.read(bIn);
                String oldFileContent = new String(bIn);
                //System.out.println("旧内容:" + oldFileContent);
                fsIn.close();
                if (!oldFileContent.equalsIgnoreCase("")) {
                    fileContent = oldFileContent + "\r\n" + fileContent;
                    //System.out.println("新内容:" + fileContent);
                }
            }
            FileOutputStream fs = new FileOutputStream(f);
            byte[] b = fileContent.getBytes();
            fs.write(b);
            fs.flush();
            fs.close();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
调用示例:
//FileTest f = new FileTest();
//System.out.println(f.read("c:/a.txt"));
final String fileName = "c:/a.txt";
System.out.println(FileTest.delete(fileName));
//System.out.println(FileTest.append(fileName,"这是java写入的内容1"));
//System.out.println(FileTest.append(fileName,"这是java写入的内容2"));
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号