java读取txt,写入txt,读取word,写入excel总结

1. 读取txt文件:

使用ClassPathResource的原因在https://blog.csdn.net/nnnora/article/details/80734326中阐述过,因为是springboot内置web容器所以不能使用FileInputStream的方式获取文件流。

  1. //读取txt文件  
  2. ClassPathResource classPathResource = new ClassPathResource("test.txt");  
  3. InputStreamReader inputStreamReader = new InputStreamReader(classPathResource.getInputStream());  
  4. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
  5. StringBuffer buffer = new StringBuffer();  
  6. String line = null;  
  7. while( (line = bufferedReader.readLine()) != null ){  
  8.     buffer.append(line);  
  9. }  
  10. bufferedReader.close();  
  11. String buffer = buffer.toString();  
            //读取txt文件
            ClassPathResource classPathResource = new ClassPathResource("test.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(classPathResource.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuffer buffer = new StringBuffer();
            String line = null;
            while( (line = bufferedReader.readLine()) != null ){
                buffer.append(line);
            }
            bufferedReader.close();
            String buffer = buffer.toString();

2. 写入txt文件:

  1. //写入txt  
  2. BufferedWriter bufw = new BufferedWriter(new FileWriter("D:\\test\\result.txt"));  
  3. for(Map.Entry<String, Integer> me : list){  
  4.     bufw.write(me+"");  
  5.     bufw.newLine();  
  6. }  
  7. bufw.close();  
            //写入txt
            BufferedWriter bufw = new BufferedWriter(new FileWriter("D:\\test\\result.txt"));
            for(Map.Entry<String, Integer> me : list){
                bufw.write(me+"");
                bufw.newLine();
            }
            bufw.close();

3. 读取word文档

1. 首先需要引入以下jar包:


分别是操作doc和docx格式需要的jar包,关于引入jar包的语句格式可以去http://jareye.com/查找,可以导出maven或者gradle等工具下的compile语句。

2. 读取word文档:

  1. try {  
  2.     //读取word文件  
  3.     String path = "Shocking level of sexual harassment at music festivals.docx";  
  4.     String buffer = "";  
  5.     if (path.endsWith(".doc")) {  
  6.         ClassPathResource classPathResource = new ClassPathResource(path);  
  7.         WordExtractor wordExtractor = new WordExtractor(classPathResource.getInputStream());  
  8.         buffer = wordExtractor.getText();  
  9.     } else if (path.endsWith("docx")) {  
  10.         ClassPathResource classPathResource = new ClassPathResource(path);  
  11.         XWPFDocument xdoc = new XWPFDocument(classPathResource.getInputStream());  
  12.         POIXMLTextExtractor extractor = new XWPFWordExtractor(xdoc);  
  13.         buffer = extractor.getText();  
  14.     } else {  
  15.         System.out.println("此文件不是word文件!");  
  16.     }  
  17. }catch (Exception e){  
  18.     logger.error("@@ReadFileError", e);  
  19. }  
        try {
            //读取word文件
            String path = "Shocking level of sexual harassment at music festivals.docx";
            String buffer = "";
            if (path.endsWith(".doc")) {
                ClassPathResource classPathResource = new ClassPathResource(path);
                WordExtractor wordExtractor = new WordExtractor(classPathResource.getInputStream());
                buffer = wordExtractor.getText();
            } else if (path.endsWith("docx")) {
                ClassPathResource classPathResource = new ClassPathResource(path);
                XWPFDocument xdoc = new XWPFDocument(classPathResource.getInputStream());
                POIXMLTextExtractor extractor = new XWPFWordExtractor(xdoc);
                buffer = extractor.getText();
            } else {
                System.out.println("此文件不是word文件!");
            }
        }catch (Exception e){
            logger.error("@@ReadFileError", e);
        }

4. 写入excel文档

  1. //写入excel  
  2. //第一步,创建一个workbook对应一个excel文件  
  3. HSSFWorkbook workbook = new HSSFWorkbook();  
  4. //第二步,在workbook中创建一个sheet对应excel中的sheet  
  5. HSSFSheet sheet = workbook.createSheet("词频文件");  
  6. //第三步,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制  
  7. HSSFRow row = sheet.createRow(0);  
  8. //第四步,创建单元格,设置表头  
  9. HSSFCell cell = row.createCell(0);  
  10. cell.setCellValue("单词");  
  11. cell = row.createCell(1);  
  12. cell.setCellValue("频次");  
  13.   
  14. //第五步,写入实体数据  
  15. int i = 0;  
  16. for(Map.Entry<String, Integer> entity : list){  
  17.     HSSFRow row1 = sheet.createRow(i + 1);  
  18.     row1.createCell(0).setCellValue(entity.getKey());  
  19.     row1.createCell(1).setCellValue(entity.getValue());  
  20.     i++;  
  21. }  
  22. //将文件保存到指定的位置  
  23. try {  
  24.     fos = new FileOutputStream("wordsResult.xls");  
  25.     workbook.write(fos);  
  26.     logger.info("写入成功");  
  27.     fos.close();  
  28. catch (IOException e) {  
  29.     logger.error("@@WriteFileError", e);  
  30. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值