zoukankan      html  css  js  c++  java
  • java生成文件/插入数据

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    
    public class C1 {
        private static String path="E:\file\";
        private static String filenameTemp;
        public static boolean createFile(String fileName)
        {
        Boolean bool=false;
        filenameTemp=path+fileName+".txt";
        File file = new File(filenameTemp);
        try {
            if(!file.exists()){
                file.createNewFile();
                bool=true;
                System.out.println("success create file,the file is "+ filenameTemp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bool;
        }
        
        //写入内容:
        /**
         * 向文件中写入内容
         * @param filepath 文件路径与名称
         * @param newstr  写入的内容
         * @return
         * @throws IOException
         */
         public static boolean writeFileContent(String filepath,String newstr) throws IOException{
                Boolean bool = false;
                String filein = newstr+"
    ";//新写入的行,换行
                String temp  = "";
                
                FileInputStream fis = null;
                InputStreamReader isr = null;
                BufferedReader br = null;
                FileOutputStream fos  = null;
                PrintWriter pw = null;
                try {
                    File file = new File(filepath);//文件路径(包括文件名称)
                    //将文件读入输入流
                    fis = new FileInputStream(file);
                    isr = new InputStreamReader(fis);
                    br = new BufferedReader(isr);
                    StringBuffer buffer = new StringBuffer();
                    
                    //文件原有内容
                    for(int i=0;(temp =br.readLine())!=null;i++){
                        buffer.append(temp);
                        // 行与行之间的分隔符 相当于“
    ”
                        buffer = buffer.append(System.getProperty("line.separator"));
                    }
                    buffer.append(filein);
                    
                    fos = new FileOutputStream(file);
                    pw = new PrintWriter(fos);
                    pw.write(buffer.toString().toCharArray());
                    pw.flush();
                    bool = true;
                    System.out.println("Insert data successfully.");
    
    
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }finally {
                    //不要忘记关闭
                    if (pw != null) {
                        pw.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                    if (br != null) {
                        br.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (fis != null) {
                        fis.close();
                    }
                }
                return bool;
            }
            
    //        /**
    //         * 删除文件
    //         * @param fileName 文件名称
    //         * @return
    //         */
            public static boolean delFile(String fileName){
                Boolean bool = false;
                filenameTemp = path+fileName+".txt";
                File file  = new File(filenameTemp);
                try {
                    if(file.exists()){
                        file.delete();
                        bool = true;
                        System.out.println("success delete file,the file is " + filenameTemp);
    
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return bool;
            }
            
        
        
        public static void main(String[] args) throws IOException
        {
            createFile("james");
    //        delFile("james");
            writeFileContent("E:\file\james.txt","插入数据啦");
        }
    }
  • 相关阅读:
    转载:混淆包含SlidingMenu、gson等Android代码的proguard写法
    今天解决的两个问题
    C++中指针和引用的区别
    负载均衡服务器session共享的解决方案 (转载)
    Entity Framework的默认值BUG解决方法
    【转】SAPI中的IspeechRecoContext(接口)
    Sapi 添加语法的文章(转载)
    SAPI训练文件存储位置
    Flask第九篇 Flask 中的蓝图(BluePrint)
    Flask 第八篇 实例化Flask的参数 及 对app的配置
  • 原文地址:https://www.cnblogs.com/lonske/p/6297021.html
Copyright © 2011-2022 走看看