美文网首页
Java知识点-week2(枚举,继承)

Java知识点-week2(枚举,继承)

作者: 梦幽辰 | 来源:发表于2019-12-22 15:11 被阅读0次

枚举类

将普通的Java类的calss替换为enum,创建属性值时不需要添加;

例:

public enum Animal {
    tiger,
    lion,
    dog,
    cat
}

继承

public class Apple extends Fruit{

}

父类转换成子类,需要强制转换。

子类转换成父类,直接=符号赋值。

文件读写

文件读取

public static void read(){
    File file = new File("E:\\临时.txt");
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if( br != null){
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

BufferedReader是比FileReader更为简洁的代码编写,主要体现在缓存区中,BufferedReader读取时将字符串一行一行的读取。

文件写入

public static void write(){
    File file = new File("E:\\临时.txt");
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(file));
        bw.write("hello bmatch");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(bw != null){
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Excel文件读写

Excel文件读取

public static void read(){
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream("E:\\test\\workbook.xlsx");
        Workbook wb = WorkbookFactory.create(inputStream);
        DataFormatter formatter = new DataFormatter();
        Sheet sheet1 = wb.getSheetAt(0);
        for (Row row : sheet1) {
            for (Cell cell : row) {
                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);

                // 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();
            }
        }
    }

}

Excel文件写入

public static void write(){
    Workbook wb = null;
    OutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream("E:\\test\\workbook.xlsx");
        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");

        CreationHelper createHelper = wb.getCreationHelper();
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setDataFormat(
                createHelper.createDataFormat().getFormat("m/d/yy h:mm"));

        Cell cell2 = row.createCell(2);
        cell2.setCellValue(new Date());
        cell2.setCellStyle(cellStyle);
        wb.write(fileOut);
    }  catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileOut != null) {
            try {
                fileOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

相关文章

  • Java知识点-week2(枚举,继承)

    枚举类 将普通的Java类的calss替换为enum,创建属性值时不需要添加; 例: 继承 父类转换成子类,需要强...

  • 枚举

    一、枚举 我们所定义的每个枚举类型都继承自java.lang.Enum类。枚举中的每个成员默认都是publiic ...

  • Enum枚举类的认识

    枚举类:默认继承java.lang.Enum类,不能再继承其他类,但是可以实现一个或多个接口。不是个抽象类。 枚举...

  • javaSE - 010 - enum

    枚举 1.定义的每个枚举类型都继承自java.lang.Enum类,枚举中的每个成员默认都是public stat...

  • 枚举资料笔记

    概念:被enum关键字修饰的类型就是枚举类型,枚举类型都隐式继承Enum,根据java不可以多继承的原理,不能再继...

  • 学习路线

    Java Se基础 Java基础语法、数组、类与对象、继承与多态、异常、范型、集合、流与文件、反射、枚举、自动装箱...

  • Java枚举详解

    enum修饰的类默认继承了Enum类,默认被static final修饰。因为Java不支持多继承,故枚举类不能继...

  • Think In Java 第19章 枚举类型

    本文发表于KuTear's Blog,转载请注明 默认枚举继承至Enum类,由于Java的单一继承机制,所以不能在...

  • JAVA基础篇-初识枚举

    面试官问:枚举是一个接口还是一个类?答:枚举是一个标识类,枚举本身继承自java.lang.Enum. 面试官问:...

  • kotlin-sealed classes

    sealed classes是一堆存在继承关系的类的集合。 类似于java中的枚举。不同的是,每个枚举类型只能存在...

网友评论

      本文标题:Java知识点-week2(枚举,继承)

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