美文网首页
关于文件File的存取

关于文件File的存取

作者: 一条IT | 来源:发表于2018-12-27 18:52 被阅读13次

    将文件读取并写入一个文件夹里面的文件中

    ①生成一个文件路径(选择D盘)

     private static String path = "D:\\file\\"; 
    

    ②创建文件的路径和名称

    private static String filenameTemp;
    

    ③创建一个“写入文件”的方法:

    /**
         * 向文件中写入内容
         * @param filepath 文件路径与名称
         * @param new_str  写入的内容
         * @return
         * @throws IOException
         */
        public static boolean writeFileContent(String filepath,String new_str) throws IOException{
            Boolean bool = false;
            String filein = new_str+"\r\n";//新写入的行,换行
            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);
                    // 行与行之间的分隔符 相当于“\n”
                    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;
            } 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;
        }
    

    ④创建一个“创建文件的方法”,并且引用“写入文件”的方法。

    public static boolean createFile(String fileName,String filecontent){
            Boolean bool = false;
            filenameTemp = path+fileName+".txt";//文件路径+名称+文件类型
            File file = new File(filenameTemp);//new一个文件对象
            try {
                //如果文件不存在,则创建新的文件
                if(!file.exists()){
                    file.createNewFile();//创建一个新的文件目录。
                    bool = true;
                    System.out.println("文件创建成功,文件名是:"+filenameTemp);
                    //调用writeFileContent()方法,创建文件成功后,写入内容到文件里
                    writeFileContent(filenameTemp, filecontent);
                }else {
                    bool = true;//开关打开。
                    System.out.println("文件已存在!");
                    //调用writeFileContent()方法,继续写入内容到文件里
                    writeFileContent(filenameTemp, filecontent);
                }
            } catch (Exception e) {
                bool = false;
                e.printStackTrace();
            }       
            return bool;
        }
    

    ⑤引用“createFile”方法就可以写入文件!

    以下是总结:

    
    public class Test2 {
        //生成文件路径
        private static String path = "D:\\file\\";    
        //文件路径+名称
        private static String filenameTemp;
        /**
         * 创建文件
         * @param fileName  文件名称
         * @param filecontent   文件内容
         * @return  是否创建成功,成功则返回true
         */
        public static boolean createFile(String fileName,String filecontent){
            Boolean bool = false;
            filenameTemp = path+fileName+".txt";//文件路径+名称+文件类型
            File file = new File(filenameTemp);//new一个文件对象
            try {
                //如果文件不存在,则创建新的文件
                if(!file.exists()){
                    file.createNewFile();//创建一个新的文件目录。
                    bool = true;
                    System.out.println("文件创建成功,文件名是:"+filenameTemp);
                    //调用writeFileContent()方法,创建文件成功后,写入内容到文件里
                    writeFileContent(filenameTemp, filecontent);
                }else {
                    bool = true;//开关打开。
                    System.out.println("文件已存在!");
                    //调用writeFileContent()方法,继续写入内容到文件里
                    writeFileContent(filenameTemp, filecontent);
                }
            } catch (Exception e) {
                bool = false;
                e.printStackTrace();
            }       
            return bool;
        }
        
        /**
         * 向文件中写入内容
         * @param filepath 文件路径与名称
         * @param new_str  写入的内容
         * @return
         * @throws IOException
         */
        public static boolean writeFileContent(String filepath,String new_str) throws IOException{
            Boolean bool = false;
            String filein = new_str+"\r\n";//新写入的行,换行
            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);
                    // 行与行之间的分隔符 相当于“\n”
                    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;
            } 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;
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            return bool;
        }
        public static void main(String[] args) {
            UUID uuid = UUID.randomUUID();
            //分开用uuID把数据分开
            createFile(uuid+"myfile", "我的梦说别停留等待,就让光芒折射泪湿的瞳孔,映出心中最想拥有的彩虹,带我奔向那片有你的天空,因为你是我的梦 我的梦"); 
            //总共的数据
            createFile("myfile", "");
            
        }   
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:关于文件File的存取

          本文链接:https://www.haomeiwen.com/subject/sfydlqtx.html