文件操作

基本文件操作

读文件

        File file = new File("bmathc.txt");
        FileReader fr=null;
        try {
            fr=new FileReader(file);
            //1
            int tmp=fr.read();
            while (tmp!=-1){
                System.out.println((char)tmp);
                tmp=fr.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        File file = new File("bmathc.txt");
        BufferedReader br=null;

        try {
            br=new BufferedReader(new FileReader(file));
            String line=null;
            while ((line=br.readLine())!=null);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

写文件

   File file=new File("hello.text");
        BufferedWriter bw=null;

        try {
            bw=new BufferedWriter(new FileWriter(file));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

java 对excel操作

可以搜索java excel得道大部分结果与apache相关去其官网得知是个poi
搜索maven apache poi 查找apache poi引入

2020-03-13

org.apache.poi poi-ooxml 4.1.2

读文件

 public static void read(){

        InputStream inputStream=null;
        try {
           inputStream=new FileInputStream("workbook.xlsx");
           Workbook wb=WorkbookFactory.create(inputStream);


            DataFormatter formatter = new DataFormatter();
            //找到sheet
            Sheet sheet1 = wb.getSheetAt(0);
            //循环sheet
            for (Row row : sheet1) {
                //循环row
                for (Cell cell : row) {
                    //找到cell某个格的指针
                    CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
                    //指针的坐标
                    System.out.print(cellRef.formatAsString());
                    System.out.print(" - ");

                    //格式化文本
                    // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
                    String text = formatter.formatCellValue(cell);
                    System.out.println(text);

                    //判断cell的type
                    // Alternatively, get the value and format it yourself
                    switch (cell.getCellType()) {
                        case STRING:
                            System.out.println(cell.getRichStringCellValue().getString());
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                System.out.println(cell.getDateCellValue());
                            } else {
                                System.out.println(cell.getNumericCellValue());
                            }
                            break;
                        case BOOLEAN:
                            System.out.println(cell.getBooleanCellValue());
                            break;
                        case FORMULA:
                            System.out.println(cell.getCellFormula());
                            break;
                        case BLANK:
                            System.out.println();
                            break;
                        default:
                            System.out.println();
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

写文件

 public static void write(){

        OutputStream fileOut = null;

        try {
            fileOut = new FileOutputStream("workbook.xlsx");
            Workbook wb = new XSSFWorkbook();
            Sheet sheet = wb.createSheet("new sheet");
            Row row=sheet.createRow(0);
            Cell cell0=row.createCell(0);
            cell0.setCellValue(1.0);

            Cell cell1=row.createCell(1);
            cell1.setCellValue("hello bmatch");

            Cell cell2=row.createCell(2);
            cell2.setCellValue(new Date());

            CellStyle cellStyle = wb.createCellStyle();
            CreationHelper createHelper = wb.getCreationHelper();
            cellStyle.setDataFormat(
                    //   m/d/yy h:mm    月日年
                    createHelper.createDataFormat().getFormat("MM/dd/yyyy hh:mm"));
            cell2.setCellStyle(cellStyle);


            wb.write(fileOut);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileOut!=null){
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

image

存在
存在能读入
存在报错
不存在
image

image

##附带excel操作

  @Override
    public void record(AccountingRecord record) {
        File file=new File("./record.xlsx");
        Workbook wb=null;
        Sheet sheet=null;
        if(file.exists()){
            try {
                wb=new XSSFWorkbook(file);
                sheet=wb.getSheetAt(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            wb=new XSSFWorkbook();
            sheet=wb.createSheet();
        }
        OutputStream out=null;
        try {
            int rows=sheet.getPhysicalNumberOfRows();
             Row row=sheet.createRow(rows);
            //把记账时间,记录在第一列上
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
             String time=simpleDateFormat.format(record.getTime());
            row.createCell(0).setCellValue(time);
            //把类型记录在第二列上
            row.createCell(1).setCellValue(record.getType());
            //把科目记录在第三列上
            row.createCell(2).setCellValue(record.getCateGory());
            //把发生时间记录在第四列上
            row.createCell(3).setCellValue(record.getCreateTime());
            //把金额记录在第五列上
            row.createCell(4).setCellValue(record.getAmount());
             //创建临时文件
            File newFile=new File(file+".bak");
           out=new FileOutputStream(newFile);
            wb.write(out);
            //删除老文件
            file.deleteOnExit();
            //把新的文件更名为老的文件名
            newFile.renameTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                wb.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public List<AccountingRecord> query() {
        File file=new File("./record.xlsx");
        List<AccountingRecord> records=new ArrayList<>();
        //如果文件不存在,直接返回数据
        if(!file.exists()){
            return records;
        }
        Workbook wb= null;
        try {
            wb = new XSSFWorkbook(file);Sheet sheet=wb.getSheetAt(0);
            int rows=sheet.getPhysicalNumberOfRows();
            for(int i=0;i<rows;i++){
                Row row=sheet.getRow(i);
                AccountingRecord record=new AccountingRecord();
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                Date time=simpleDateFormat.parse(row.getCell(0).getStringCellValue());
                record.setTime(time);
                record.setType(row.getCell(1).getStringCellValue());
                record.setCateGory(row.getCell(2).getStringCellValue());
                record.setCreateTime(row.getCell(3).getStringCellValue());
                record.setAmount((int)row.getCell(4).getNumericCellValue());
                records.add(record);
                }
            } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }catch (ParseException e){
            e.printStackTrace();
        }

        return records;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值