java文件讀取(word,txt)


1.讀取txt內容:

public String getTxt(String uri){
        String content="";
        if(!"".equals(uri) && uri != null){
            try {
            File file=new File(uri);//存放地址
            InputStream in= new FileInputStream(file);
            byte[] b = new byte[3];
            in.read(b);//讀取
            InputStreamReader read2;
            //通過字節判斷編碼格式
            String coding="";
            if (b[0] == -17 && b[1] == -69 && b[2] == -65){
               read2 = new InputStreamReader(in,"UTF-8");//utf-8
               coding="UTF-8";
            }else
            {
               read2 = new InputStreamReader(in,"GBK");//其他編碼
               coding="GBK";
            }
            BufferedReader read=new BufferedReader(read2);
            String ddd = "";
            int d=0;
            StringBuilder str=new StringBuilder();
            while((ddd=read.readLine())!=null){//循環獲取內容
                content+=ddd;
                str.append(ddd);
                d++;
            }
            read2.close();
            read.close();
            in.close();
              
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

2.修改指定內容

            InputStream in= new FileInputStream(file);
            byte[] b = new byte[3];
            in.read(b);//讀取
            InputStreamReader read2;
            //通過字節判斷編碼格式
            String coding="";
            if (b[0] == -17 && b[1] == -69 && b[2] == -65){
               read2 = new InputStreamReader(in,"UTF-8");//utf-8
               coding="UTF-8";
            }else
            {
               read2 = new InputStreamReader(in,"GBK");//其他編碼
               coding="GBK";
            }
            BufferedReader read=new BufferedReader(read2);
            String ddd = "";
            int d=0;
            StringBuilder str=new StringBuilder();
            while((ddd=read.readLine())!=null){//循環獲取內容
                content+=ddd;
                str.append(ddd);
                d++;
            }
            read2.close();
            read.close();
            in.close();
            
            String strb = str.toString().replace("要修改的內容", "修改后的內容");
            Writer writer = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(uri), coding));
            writer.write(strb.toCharArray());
            writer.flush();
            writer.close();
       
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

3.word文檔內容讀取

    public String getDocx(String uri){
        //解析docx模板並獲取document對象
        XWPFDocument document;
        //獲取XWPFRun對象輸出整個文本內容
        StringBuffer tempText = new StringBuffer();
        try {
            document = new XWPFDocument(POIXMLDocument.openPackage(uri));
        //獲取整個文本對象
        List<XWPFParagraph> allParagraph = document.getParagraphs();
        for (XWPFParagraph xwpfParagraph : allParagraph) {
                List<XWPFRun> runList = xwpfParagraph.getRuns();
                for (XWPFRun xwpfRun : runList) {
                    tempText.append(xwpfRun.toString());
                }
        }
        //存放文檔新地址
        String newPath="";
        //讀取源文檔內容到新文檔
        File file = new File(newPath);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdir();
            file.getParentFile().createNewFile();
        }
        FileOutputStream stream = new FileOutputStream(newPath);
        document.write(stream);//寫入新文檔
        stream.close();
        
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//文檔內容
return tempText.toString(); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM