我想用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.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();
}
}
}
答案 0 :(得分:1)
我认为你需要这种方法。请将String与equals函数进行比较。 &#34; ==&#34;仅代表地址。
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
}
我认为这是你的目标。如果没有,请发表评论。