java读写文件

/**      读文件
         * @param str 路径
         * @param res 要写入文件的字符串
         */

 public static void readFileByBuffer(String filename){
            //  File readFile = new File(filename);
            BufferedReader br = null;
            try{
                //   br = new BufferedReader(new FileReader(readFile));
                br = new BufferedReader(new FileReader(filename));
                StringBuffer stb = new StringBuffer();
                String rline = null;
                while((rline=br.readLine())!=null){
                    //byte[] bb = rline.getBytes("utf-8");
                    //utf8Togb2312(rline);
                    mywrite("E:/vblcfgsc/20181205/18120501_VehicleBLList.db",rline);
                    /*String gbk = new String(bb,"gbk");
                    byte[] cc = gbk.getBytes("gbk");
                    String gb2312 = new String(cc,"gb2312");*/
                }
                
                
                br.close();
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(br!=null){
                    try{
                        br.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }

 

 

 

 

/**      写文件
         * @param str 路径
         * @param res 要写入文件的字符串
         */
        public static void mywrite(String str,String res){
            File file = new File(str);    //1、建立连接
            OutputStream os = null;
            try {
                //2、选择输出流,以追加形式(在原有内容上追加) 写出文件 必须为true 否则为覆盖
                os = new FileOutputStream(file,true);    
//                //和上一句功能一样,BufferedInputStream是增强流,加上之后能提高输出效率,建议
//                os = new BufferedOutputStream(new FileOutputStream(file,true));
                String string = res;
                byte[] data = string.getBytes();    //将字符串转换为字节数组,方便下面写入

                os.write(data, 0, data.length);    //3、写入文件
                os.flush();    //将存储在管道中的数据强制刷新出去
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("文件没有找到!");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("写入文件失败!");
            }finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("关闭输出流失败!");
                    }
                }
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值