• 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","插入数据啦");
        }
    }
  • 相关阅读:
    POJ3258River Hopscotch(二分)
    POJ3273Monthly Expense(二分)
    POJ1002487-3279(map)
    HDU 3123 GCC
    POJ2031Building a Space Station
    POJ3096Surprising Strings(map)
    模板类
    POJ1265Area
    【ZZ】C 语言中的指针和内存泄漏 & 编写高效的C程序与C代码优化
    学习笔记之MySQL
  • 原文地址:https://www.cnblogs.com/lonske/p/6297021.html
Copyright © 2020-2023  润新知