java读取文本文件代码

java读取文本文件代码,第1张

java读取文本文件的方法有很多 这个例子主要介绍最简单 最常用的BufferedReader类 完整例子如下 package net chinaunix blog hzm textimport java io BufferedReaderimport java io FileReaderimport java io IOExceptionpublic class ReadFile {private String pathpublic ReadFile(String filePath){path = filePath}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()]int ifor(i= i <readLines() i++){textData[i] = textReader readLine() }textReader close() return textData}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = @SuppressWarnings( unused )String oneLinewhile((oneLine = bf readLine()) != null){numberOfLines++}bf close() return numberOfLines}}package net chinaunix blog hzm textimport java io IOExceptionpublic class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int ifor(i= i<content lengthi++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249

下面是三个java例子,关于读取wps/et/dps的方法

1.读取wps(读取文本): 通过流加载wps文件,读取文字内容

import com.spire.doc.*

import java.io.File

import java.io.FileInputStream

import java.io.FileWriter

import java.io.IOException

public class ReadTextFromWPS {

public static void main(String[] args) throws IOException{

//通过流加载WPS文字文档

FileInputStream inputStream = new FileInputStream(new File("test.wps"))

Document doc = new Document()

doc.loadFromStream(inputStream, FileFormat.Doc)

//获取文本保存为String

String text = doc.getText()

//将String写入Txt

writeStringToTxt(text,"读取WPS文本.txt")

}

public static void writeStringToTxt(String content, String txtFileName) throws IOException {

FileWriter fWriter= new FileWriter(txtFileName,true)

try {

fWriter.write(content)

}catch(IOException ex){

ex.printStackTrace()

}finally{

try{

fWriter.flush()

fWriter.close()

} catch (IOException ex) {

ex.printStackTrace()

}

}

}

}

2. 读取et:直接加载et格式的表格文件,读取数据

import com.spire.xls.*

public class ExcelToText {

public static void main(String[] args) {

//加载et格式的表格文件

Workbook workbook = new Workbook()

workbook.loadFromFile("test.et")

//获取工作表

Worksheet sheet = workbook.getWorksheets().get(0)

//获取指定单元格中的文本数据

CellRange range = sheet.getCellRange("A1")

String text = range.getText().trim()

System.out.println(text)

}

}

3.读取dps:直接加载dps格式的幻灯片文档,读取文本

import com.spire.presentation.IAutoShape

import com.spire.presentation.ISlide

import com.spire.presentation.ParagraphEx

import com.spire.presentation.Presentation

import java.io.FileWriter

public class ExtractText {

public static void main(String[]args) throws Exception{

//加载测试文档

Presentation ppt = new Presentation()

//ppt.loadFromFile("test.pptx")

ppt.loadFromFile("test.dps")

StringBuilder buffer = new StringBuilder()

//遍历文档中的幻灯片,提取文本

for (Object slide : ppt.getSlides())

{

for (Object shape : ((ISlide) slide).getShapes())

{

if (shape instanceof IAutoShape)

{

for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs())

{

buffer.append(((ParagraphEx) tp).getText())

}

}

}

}

//保存到文本文件

FileWriter writer = new FileWriter("ExtractTextfromDPS.txt")

writer.write(buffer.toString())

writer.flush()

writer.close()

}

}

这里须在Java程序中导入spire.office.jar文件。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12066439.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存