枚举类
将普通的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();
}
}
}
}
网友评论