这里给大家介绍一下利用java对.txt和word的读写操作。
Txt的读取(方法很多,我的提供大家参考)
.txt的读:
参考代码:
package cn.nuist.jr;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class RWtxt {
public static void main(String[] args) {
//逐行读
BufferedReader br = null;
try {
Reader reader = new FileReader("d:/aa.txt");
br = new BufferedReader(reader);
String line;
while(null != (line = br.readLine())){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(null != br){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
参考截图:
将内容写入.txt文件(从控制台):
参考代码:
package cn.nuist.jr;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class RWtxt {
public static void main(String[] args) {
try {
//FileInputStream fin =new FileInputStream(FileDescriptor.in);
Scanner sc = new Scanner(System.in);
System.out.print("请输入一串字符:");
String content = sc.next();
File file = new File("d:/bb.txt");
if(file.exists()){
FileWriter fw = new FileWriter(file,false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close(); fw.close();
System.out.println("bb done!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
参考截图:
注意:1.这里bb.txt是已经建的,如果没有需要新建!
可以加上File file =newFile("d:/cc.txt");//cc.txt是你准备写入的文件,事先并没有建立
if(file.exists()){
file.createNewFile();
}
2.是从控制台输入时,如果想我上面使用.next()方法,当出现空格时,他不输出后面的值。所以此时可以使用nextLine()方法。
Word文档的读写
此时介绍使用工具:Apache POI。需要下载相应的jar包。
下载地址:http://poi.apache.org/download.html
导入jar包:新建一个lib文件夹,把需要的包导入(可直接复制粘贴),然后点击这个jar包,BuildPath -->Add to Build Path。
参考截图:
Word读(docx类型):
参考代码:
package cn.nuist.jr;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class RWword {
static File file =null;
public static void main(String[] args) {
String content = "";
FileInputStream fi = null;
try {
file = new File("d:/test.docx");
fi = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fi);
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
content = extractor.getText(); //getText()返回文档所有文本
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fi != null){
}
}
System.out.println("文本内容是:" + content);
}
}
参考截图:
因为word除了.docx之外,还有.doc的格式,所以下面是介绍.doc格式的读取,以及介绍了几种读取方法,解决读取过程中的一些问题。
参考代码:
package cn.nuist.jr;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class RWword {
static File file =null;
public static void main(String[] args) {
String content = "";
//StringBuilder content = null ; // 方法2 -- 输出是记得使用.toString()转还一下
FileInputStream fi = null;
try {
file = new File("d:/Test.doc");
fi = new FileInputStream(file.getAbsolutePath());
POIFSFileSystem pfs = new POIFSFileSystem(fi);
HWPFDocument document = new HWPFDocument(pfs);
//content = document.getDocumentText(); //方法1 直接读取即可
//content = document.getText(); //getText()返回文档所有文本 -- 方法2
Range range = document.getRange();
content = range.text();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fi != null){
}
}
System.out.println("文本内容是:" + content);
}
}
Word的写入:
参考代码:
package cn.nuist.jr;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class RWword {
static OutputStream os= null;
public static void main(String[] args) {
try{
XWPFDocument document = new XWPFDocument();
//创建一个段落
XWPFParagraph para = document.createParagraph();
//一个XWPFRun代表具有相同属性的一个区域。
XWPFRun run = para.createRun();
Scanner sc = new Scanner(System.in);
System.out.print("请输入一串字符:");
String content = sc.nextLine();
// run.setBold(true); //加粗
// run.setColor("FF0000"); //设置颜色
run.setText(content);
OutputStream os = new FileOutputStream("D:\\s.docx"); //也可以写成.doc形式
document.write(os); //把doc输出到输出流
os.close();
System.out.println("写入成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(os != null){
//try {
// os.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
}
}
参考截图:
s.docx中的文件内容:
利用POI还可以对excel,pdf进行操作,感兴趣可以先自行去百度了解。
以上是利用java对.txt文件和word的读取操作。如果您有什么疑惑或者发现本篇文章什么有问题的地方。请您指出,谢谢!
2017即将结束,2018让我们一同努力。祝各位新的一年健康快乐。