怎用用java导入、导入word形式的考试题目?

Python056

怎用用java导入、导入word形式的考试题目?,第1张

使用java中的io进行读取

BufferedReader bufferedReader = null

File file = new File("文档地址+文档名.docx")

if(!file.exists()){

System.out.println("文件不存在")

} else {

bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"))

String lineText = null

while((lineText = bufferedReader.readLine()) != null){

if (linText != null &&!lineText.eq("")){

System.out.println("一次读取一行,一行内容为:" + lineText)

环境支持

1.1 添加poi支持:包下载地址http://www.apache.org/dyn/closer.cgi/poi/release/

1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件

package com.ray.poi.util

import java.io.ByteArrayInputStream

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.IOException

import org.apache.poi.poifs.filesystem.DirectoryEntry

import org.apache.poi.poifs.filesystem.DocumentEntry

import org.apache.poi.poifs.filesystem.POIFSFileSystem

import org.textmining.text.extraction.WordExtractor

/**

* 读写doc

* @author wangzonghao

*

*/

public class POIWordUtil {

/**

* 读入doc

* @param doc

* @return

* @throws Exception

*/

public static String readDoc(String doc) throws Exception {

// 创建输入流读取DOC文件

FileInputStream in = new FileInputStream(new File(doc))

WordExtractor extractor = null

String text = null

// 创建WordExtractor

extractor = new WordExtractor()

// 对DOC文件进行提取

text = extractor.extractText(in)

return text

}

/**

* 写出doc

* @param path

* @param content

* @return

*/

public static boolean writeDoc(String path, String content) {

boolean w = false

try {

// byte b[] = content.getBytes("ISO-8859-1")

byte b[] = content.getBytes()

ByteArrayInputStream bais = new ByteArrayInputStream(b)

POIFSFileSystem fs = new POIFSFileSystem()

DirectoryEntry directory = fs.getRoot()

DocumentEntry de = directory.createDocument("WordDocument", bais)

FileOutputStream ostream = new FileOutputStream(path)

fs.writeFilesystem(ostream)

bais.close()

ostream.close()

} catch (IOException e) {

e.printStackTrace()

}

return w

}

}

测试

package com.ray.poi.util

import junit.framework.TestCase

public class POIUtilTest extends TestCase {

public void testReadDoc() {

try{

String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc")

System.out.println(text)

}catch(Exception e){

e.printStackTrace()

}

}

public void testWriteDoc() {

String wr

try {

wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc")

boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr)

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}