txt文件怎么导入java里_将.txt文件中的数字 导入java中

展开全部

见下32313133353236313431303231363533e59b9ee7ad9431333335323433面例子,编译后即可运行:import java.util.Scanner;

import java.util.StringTokenizer;

/**

* 仅演示,没有考虑严谨性

*/

public class Addition {

public static void main(String[] args) throws Exception {

int i = 0;

Scanner sc = new Scanner(System.in);

out: while (sc.hasNextLine()) {

String line = sc.nextLine();

StringTokenizer tk = new StringTokenizer(line);

while (tk.hasMoreTokens()) {

String t = tk.nextToken();

if ("0".equals(t)) {

break out;

}

i += Integer.parseInt(t);

}

}

System.out.println(i);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Apache POI的Java代码示例,可以导入Excel文件并校验数据。对于错误的数据,将它们标红并生成一个新的Excel文件供下载。 ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataValidation; import org.apache.poi.hssf.usermodel.HSSFDataValidationHelper; import org.apache.poi.hssf.usermodel.HSSFDataValidationList; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationConstraint; import org.apache.poi.ss.usermodel.DataValidationHelper; import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType; import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType; import org.apache.poi.ss.usermodel.DataValidationList; import org.apache.poi.ss.usermodel.DataValidationWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellUtil; import org.apache.poi.ss.util.CellRangeAddress; public class ExcelValidator { public static void main(String[] args) throws IOException { // 导入Excel文件 File inputFile = new File("input.xls"); FileInputStream fis = new FileInputStream(inputFile); Workbook workbook = new HSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); // 校验数据 List<Integer> errorRows = new ArrayList<>(); for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) { continue; } Cell idCell = row.getCell(0); Cell nameCell = row.getCell(1); if (idCell == null || nameCell == null || idCell.getCellType() != Cell.CELL_TYPE_NUMERIC || nameCell.getCellType() != Cell.CELL_TYPE_STRING) { errorRows.add(i); } } // 标注错误行 CellStyle errorStyle = workbook.createCellStyle(); errorStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); errorStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); for (Integer rowIndex : errorRows) { HSSFRow row = (HSSFRow) sheet.getRow(rowIndex); HSSFCell idCell = row.getCell(0); HSSFCell nameCell = row.getCell(1); CellUtil.setCellStyleProperty(idCell, workbook, CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.RED.getIndex()); CellUtil.setCellStyleProperty(nameCell, workbook, CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.RED.getIndex()); } // 创建新的Excel文件 File outputFile = new File("output.xls"); FileOutputStream fos = new FileOutputStream(outputFile); workbook.write(fos); fos.close(); // 下载生成的Excel文件 // TODO: 实现下载功能 } } ``` 在这个示例,我们假设Excel文件的第一列是ID(数字类型),第二列是名称(字符串类型)。我们使用Apache POI API来读取工作簿和工作表,然后检查单元格类型是否正确。如果存在错误的行,我们使用CellUtil工具类设置单元格样式以标注错误行。最后,我们创建一个新的Excel文件,将修改后的工作簿写入其,并提供下载功能。 需要注意的是,示例代码的数据校验仅仅是一种简单的实现方式,你需要根据实际需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值