文件流以及Excel解析
1、excel的常用操作
package com.lemon.excle;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ExcelDemo {
public static void main(String[] args) throws Exception {
//poi
/*
* 1、加载excel文件
* 2、获取所有sheet
* 3、选取第一个sheet
* 4、获取第一行
* 5、获取第二列
* 6、打印出单元格中的内容
* */
//1、加载excel
FileInputStream fis = new FileInputStream("src/test/resources/java24.xlsx");
//2、获取所有的表单sheet
Workbook sheets = WorkbookFactory.create(fis);
//3、从中取第一个sheet
Sheet sheet = sheets.getSheetAt(0);
//4、获取第一个sheet表单中的第一行
Row row = sheet.getRow(0);
//5、获取第二列
Cell cell = row.getCell(2);
System.out.println("第一行 第二列的值为:" + cell);
//枚举:可穷举,防止调用方式时传入错误的值。
Cell cell2 = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
// null new cell();
cell2.setCellType(CellType.STRING);
System.out.println(cell);
}
}
2、Excel的读取操作
package com.lemon.excle;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ExcelRead {
public static void main(String[] args) throws Exception {
//1、加载excel文件
FileInputStream fis = new FileInputStream("src/test/resources/java24.xlsx");
//2、获取所有sheet
//多态 xlsx xls
Workbook sheets = WorkbookFactory.create(fis);
//3、选取第一个sheet
Sheet sheet = sheets.getSheetAt(0);
//4、获取所有的行
for (Row row : sheet) {
//5、获取所有的列
for (Cell cell : row) {
cell.setCellType(CellType.STRING);
System.out.println(cell + ",");
}
System.out.println("所有行的内容");
}
//获取最后一行
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
int lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING);
System.out.print(cell + ",");
}
System.out.println("==========");
}
}
}
3、Excel写入操作
package com.lemon.excle;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ExcelWrite {
public static void main(String[] args) throws Exception {
//修改 = 读取 + 改
//1、加载excel文件
FileInputStream fis = new FileInputStream("src/test/resources/java24.xls");
//清空文件
//2、获取所有sheet
//多态 xlsx xls
Workbook sheets = WorkbookFactory.create(fis);
//3、选取第一个sheet
Sheet sheet = sheets.getSheetAt(0);
//4、获取第一行
Row row = sheet.getRow(0);
//5、获取第二列
// Cell cell = row.getCell(1);
//枚举:可穷举,防止调用方式时传入错误的值。
Cell cell = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
//修改前提:必须要拿到对应的单元格,cell。
cell.setCellValue("姓名");
//null new Cell();
//6、打印出单元格中的内容
cell.setCellType(CellType.STRING);
System.out.println(cell);
//先备份再操作。
FileOutputStream fos = new FileOutputStream("src/test/resources/java24.xls");
sheets.write(fos);
//7、关流
fis.close();
}
}
4、Properties
properties如下:
package com.lemon.properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) throws IOException { //抛出异常,我不处理
//Properties prop = new Properties();//HashMap 表兄弟
//Properties没有泛型,但是你可以理解为String,String
//prop.setProperty("username","root");
//prop.setProperty("password","123456");
//prop.setProperty("url","127.0.0.1");
//System.out.println(prop);
//获取或者写入配置文件。 读取
//FileOutputStream fos = new FileOutputStream("src/test/resources/config2.properties");
//写
//prop.store(fos,"备注");
//fos.close();
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("src/test/resources/config2.properties");
//读取
prop.load(fis);
System.out.println(prop);
System.out.println(prop.getProperty("username"));
fis.close();
//IO input output 输入输出流 a
int a = 10;
try {
//放可能会出现异常的代码
method();
System.out.println("========1");
System.out.println("========2");
System.out.println("========3");
} catch (Exception e) {
//出现异常之后,补偿操作。
e.printStackTrace();
System.out.println("========4");
} finally {
//不论是否发生异常都会执行,释放资源
System.out.println("==========6");
}
System.out.println("========5");
}
public static void method() throws Exception { //可以一直往外抛出异常
FileInputStream fis = new FileInputStream("src/test/resources/config2.properties");
}
}

浙公网安备 33010602011771号