Java的文件读写操作(xls,txt)

XLS读取

	public List<String> getXlsFile(String fileName) throws IOException, BiffException{
        List<String> result = new ArrayList();
        File inputFile = new File(fileName);
        FileInputStream fileInputStream = new FileInputStream(inputFile);
        Workbook workbook = Workbook.getWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheet(0);//获取第一张表(下标从零开始)
        int rows = sheet.getRows();//获取行数
        for(int i =0;i<rows;i++) {
            Cell cell = sheet.getCell(0, i);//获取第i行的第0列的数据,默认是string类型
            result.add(cell.getContents());
        }
        return result;
    }

TXT读取

	public List<String> getTxtFile(String fileName) throws IOException {
	    InputStreamReader read = new InputStreamReader(new FileInputStream(fileName),"utf-8");//编码格式
	    BufferedReader bufferedReader = new BufferedReader(read);
	    String lineTxt;
	    List<String> result = new ArrayList();
	    while((lineTxt = bufferedReader.readLine()) != null){//按行读入到最后一行
	        result.add(lineTxt);
	    }
	    return result;
	}

csv的写入

	public static void printCsv(String line,String outPutPath) throws IOException {
	    File outPutFile = new File(outPutPath);
	    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(outPutFile), "utf-8");
	    outputStreamWriter.write(line + "\r\n");
	    outputStreamWriter.flush();
	    outputStreamWriter.close();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值