JAVA 文件数据读取 docx doc txt

读文件目录

    /**
     * 读取文本
     * @param path 文件路径
     * @return 文本字符串
     */
    public static JSONArray readAllFile(String path){
        JSONArray allFilePath = new JSONArray();
        File file = new File(path);		//获取其file对象
        File[] fs = file.listFiles();	//遍历path下的文件和目录,放在File数组中
        for(File f:fs){					//遍历File[]数组
            if(!f.isDirectory())		//若非目录(即文件),则打印
                allFilePath.add(f);
        }
        return allFilePath;
    }

 

读取docx和doc文件



    public static String readDoc(String path){
        File file = new File(path);
        String str = "";
        try {
            FileInputStream fis = new FileInputStream(file);
            HWPFDocument doc = new HWPFDocument(fis);
            String doc1 = doc.getDocumentText();
            System.out.println(doc1);
            StringBuilder doc2 = doc.getText();
            System.out.println(doc2);
            Range rang = doc.getRange();
            String doc3 = rang.text();
            System.out.println(doc3);
            fis.close();
            str = doc1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String  readDocx(String path){
        File file = new File(path);
        String str = "";
        try {
            FileInputStream fis = new FileInputStream(file);
            XWPFDocument xdoc = new XWPFDocument(fis);
            XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
            String doc1 = extractor.getText();
//            System.out.println(doc1);
            fis.close();
            str = doc1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

 

你可能感兴趣的:(JAVA学习)