Java read and write files example

java.io.File Import; 
Import a java.io.FileInputStream; 
Import java.io.FileOutputStream; 

public class Test4 { 

    public static void main (String [] args) { 
        FileUtil FileUtil new new F = (); 
        System.out.println ( reached, f.read ( "C: /a.txt")); 
        Final String fileName = "C: /a.txt"; 
        System.out.println (f.delete (fileName)); 
        System.out.println (F. write (fileName, "which is the content written in java. 1")); 
        System.out.println (f.append (fileName, "which is the content written java 2")); 
        System.out.println (F. write (fileName, "which is the content written java 3"));

    } 
} 

/ ** 
 * file reading and writing classes 
 * / 
class FileUtil { 

    / * 
     * delete the file 
     * /
    public 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 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 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 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;
    }

}

  

Guess you like

Origin www.cnblogs.com/smartsmile/p/11617285.html