输入
注意:本例中的方法并不实际符合您的开发场景,请不要直接copy
1、从文件输入,并将结果按行读入放到一个字符串中
import java.io.*;
import java.nio.charset.StandardCharsets;
public class MyIo{
public static String inputStreamDemo(String fileName) throws IOException {
File file = new File(fileName);
if (file.exists()){
InputStream inputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,StandardCharsets.UTF_8));
String line = "";
String ans = "";
while ((line = bufferedReader.readLine())!= null){
ans = ans + line + "\r\n";
}
bufferedReader.close();
inputStream.close();
}
return null;
}
}
java中的输入流利用了装饰者模式,通过重重初始化,最终将对一个文件的读取装饰成一个 BufferedReader 对象,然后利用其getLine方法按行读取内容。值得一提的是,在windows环境中,回车其实是由 \r\n 两个字符构成,而在linux系列操作系统中,回车是由 \n 字符构成,所以我们有时候会发现,从Linux服务器导出的文件,在windows环境打开并没有换行效果。
2、从键盘输入
public static String scannerDemo(){
Scanner scanner = new Scanner(System.in);
String ans = "";
while (scanner.hasNext()){
String tmp = scanner.nextLine();
ans = ans + tmp + "\n";
}
return ans;
}
从键盘输入指的是,当程序运行起来之后,将鼠标光标放到运行程序的地方,在键盘出入,程序可以获取到。
3、从docx(文档)中输入,结果放到一个字符串中
import com.chaojilaji.util.service.WordService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public static String docxDemo(String folderName) throws IOException {
File file = new File(folderName);
File[] files = file.listFiles();
if (Objects.nonNull(files) && files.length > 0) {
for (File file1 : files) {
if (file1.isDirectory()) continue;
// TODO: 2019/6/14 读取docx
String newName = folderName+"\\"+file1.getName();
InputStream inputStream = new FileInputStream(newName);
XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
String value = "";
for (XWPFParagraph xwpfParagraph : paragraphs){
value = value + "\r\n" + xwpfParagraph.getText();
}
return value;
}
}
return null;
}
读取docx的步骤主要是按照段落读的,代码中针对每个docx文件做的循环操作就是读取段落。
4、从xlsx(表格)中输入
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public static void xlsxDemo(String fileName) throws IOException{
File file = new File(fileName);
String ans = "";
if (file.exists()){
InputStream inputStream = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(inputStream));
int sheetNumber = workbook.getNumberOfSheets();
for(int i=0;i<sheetNumber;i++){
HSSFSheet sheet = workbook.getSheetAt(i);
int rowsNumber = sheet.getPhysicalNumberOfRows();
for (int j=0;j<rowsNumber;j++){
if (j==0){
continue;
}
HSSFRow row = sheet.getRow(j);
int cellNumber = row.getPhysicalNumberOfCells();
for (int k=0;k<cellNumber;k++){
if (k<3){
HSSFCell cell = row.getCell(k);
if (Objects.nonNull(cell)){
ans = ans + "\n" +cell.getStringCellValue();
}
}
}
ans = ans +"\n\n"+ "---------------------------------------内容分隔符--------------------------------------"+"\n\n";
}
}
}
return ans;
}
一个表格文件由 sheet列表组成,一个sheet由行列表组成,一行由cell列表构成,所以读取的时候也需要根据您实际的代码情况进行修改。