zoukankan      html  css  js  c++  java
  • Java 读写文件示例

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class Test4 {
    
        public static void main(String[] args) {
            FileUtil f = new FileUtil();
            System.out.println(f.read("c:/a.txt"));
            final String fileName = "c:/a.txt";
            System.out.println(f.delete(fileName));
            System.out.println(f.write(fileName, "这是java写入的内容1"));
            System.out.println(f.append(fileName, "这是java写入的内容2"));
            System.out.println(f.write(fileName, "这是java写入的内容3"));
    
        }
    }
    
    /**
     * 文件读写类
     */
    class FileUtil {
    
        /*
         * 删除文件
         */
        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 + "
    " + 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;
        }
    
    }
    
    import java.io.*;
    
    public class Test4 {
    
        /**
         * 读取文件写入到另外一个文件
         * @param args
         */
        public static void main(String[] args) {
            BufferedReader buffReader = null;
            BufferedWriter buffWriter = null;
            try {
                buffReader = new BufferedReader(new FileReader("C:\a.txt"));
                buffWriter = new BufferedWriter(new FileWriter("C:\a_bak.txt"));
                String line = null;
                while ((line = buffReader.readLine()) != null) {
                    buffWriter.write(line);
                    buffWriter.newLine();
                    buffWriter.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    
  • 相关阅读:
    关于本人对javascript闭包的理解
    关于闭包内存泄露的处理方法
    javascript超时调用、间歇调用
    浏览器加载和渲染html的顺序
    CSS hack
    JS在操作IE与FF的一些区别
    javascript对select option操作
    jsp端使用ApplicationContext
    人生的35个经典好习惯
    2008个人总结
  • 原文地址:https://www.cnblogs.com/smartsmile/p/11617285.html
Copyright © 2011-2022 走看看