我想读取两个Java文件,一个.xlsx文件和一个.csv文件,并根据文件的扩展名在控制台上显示内容
我想读取两个Java文件,一个.xlsx文件和一个.csv文件,并根据文件的扩展名在控制台上显示内容。我已经将扩展名存储在ext1和ext2中,但是我不知道如何基于扩展名显示文件的内容我想读取两个Java文件,一个.xlsx文件和一个.csv文件,并根据文件的扩展名在控制台上显示内容,java,excel,apache,csv,Java,Excel,Apache,Csv,我想读取两个Java文件,一个.xlsx文件和一个.csv文件,并根据文件的扩展名在控制台上显示内容。我已经将扩展名存储在ext1和ext2中,但是我不知道如何基于扩展名显示文件的内容 package com.pack; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.It
package com.pack;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.commons.io.FilenameUtils;
public class TP {
public static void main(String[] args) throws FileNotFoundException {
try {
String ext1 = FilenameUtils.getExtension("C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input.xlsx"); // returns "txt"
String ext2 = FilenameUtils.getExtension("C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input2.csv"); // returns "exe"
String input = null;
String excelPath = "C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input.xlsx";
if (excelPath == ext1)
{
FileInputStream fileInputStream = new FileInputStream(new File(excelPath));
// Create Workbook instance holding .xls file
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
// Get the first worksheet
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows
Iterator < Row > rowIterator = sheet.iterator();
System.out.println("\n******XLSX FILE******\n");
while (rowIterator.hasNext()) {
// Get Each Row
Row row = rowIterator.next();
// Iterating through Each column of Each Row
Iterator < Cell > cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Checking the cell format
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
}
}
System.out.println(" ");
}
System.out.println(ext1);
}
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
我认为你需要这种方法。请将字符串与等于函数进行比较。==仅表示地址
public class TP
{
public static final String extXlsx="xlsx";
public static void main(String[] args) throws FileNotFoundException
{
try
{
String ext1 = FilenameUtils.getExtension("C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input.xlsx"); // returns "txt"
String ext2 = FilenameUtils.getExtension("C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input2.csv"); // returns "exe"
String input = null;
String excelPath = "C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input.xlsx";
System.out.println(FilenameUtils.getExtension(excelPath));
if(ext1.equals(FilenameUtils.getExtension(excelPath))){
FileInputStream fileInputStream = new FileInputStream(new File(excelPath));
// Create Workbook instance holding .xls file
//Excel Handling
} else{
System.out.println("\n******CSV FILE******\n");
//CSV File handling
}
}
catch (IOException ie){
ie.printStackTrace();
}
}
}
为什么要将扩展名与字符串ext1、ext2进行比较。更好的是,您可以根据需要将文件扩展名与静态值进行比较
public static final String extXlsx="xlsx";
public static final String extXls="xls";
// Put the above two strings above Public static void main()
if(extXlsx.equals(FilenameUtils.getExtension(excelPath))||
extXls.equals(FilenameUtils.getExtension(excelPath))){
FileInputStream fileInputStream = new FileInputStream(new File(excelPath));
// Create Workbook instance holding .xls file
//Excel Handling
} else{
System.out.println("\n******CSV FILE******\n");
//CSV File handling
}
我认为这是你的目标。如果没有,请发表评论。您的代码正在将ext1(应仅包含扩展名xlsx)与excelPath(包含完整文件路径C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input.xlsx)进行比较。也许您想将ext1与xlsx进行比较?@PhilGrigsby Yes在公共静态最终字符串extXlsx=xlsx上抛出错误无效修饰符;公共静态最终字符串extXls=xls;我已经编辑了这篇文章。请现在检查。将这两个字符串放在publicstaticvoidmain之上。