java檔案讀取(word,txt)

來源:互聯網
上載者:User

標籤:buffere   parent   string   code   amp   put   dwr   pen   puts   

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(); }

 

java檔案讀取(word,txt)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.