java 各種輸入

輸入

注意:本例中的方法並不實際符合您的開發場景,請不要直接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列表構成,所以讀取的時候也需要根據您實際的代碼情況進行修改。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章