導航:首頁 > 編程語言 > 讀取文本文件java

讀取文本文件java

發布時間:2022-05-24 05:31:31

java如何讀取txt文件

讀取txt文件(一整個獲取)


㈡ java怎麼讀取文本文件中的所有字元

可以用文件流FileInputStream的方式讀取,如果文本文件太大了,不建議一次性往內存中讀,那往往會使之溢出。也可以一行行的讀取,用BufferReader讀,具體的實例都可以網路得到的。

㈢ 使用java語言編程,怎麼讀取整個文本文件

File f = new File("z:/prism/bad.prism");
FileReader r = new FileReader(f);
BufferedReader b = new BufferedReader(r);
String s;
while((s = b.readLine()) != null){
String[] str = s.split("\\|");
Test test = new Test();
test.insert(str[0],Integer.parseInt(str[1]));
}
b.close();
r.close();

讀取某一行這是這么讀,自己計數就行了,有多少行還是這么讀

㈣ 求問Java如何讀取文本文件

下面的代碼是讀取文本文件的例子,程序會讀取text.txt文件,並將它的內容顯示出來。 1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileReader; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 7 public class ReadTextFileExample 8 { 9 public static void main(String[] args) 10 { 11 File file = new File("test.txt"); 12 StringBuffer contents = new StringBuffer(); 13 BufferedReader reader = null; 14 15 try 16 { 17 reader = new BufferedReader(new FileReader(file)); 18 String text = null; 19

㈤ java中怎麼用解碼讀取txt文本文檔的內容

BufferedReader
in
=
new
BufferedReader(new
InputStreamReader(read,"UTF-8"));
是用UTF-8編碼讀取。那改用其他編碼讀就好。你如你的文件可能是GBK編碼的,所以改成
BufferedReader
in
=
new
BufferedReader(new
InputStreamReader(read,"GB2312"));
應該就行了

㈥ 用java編寫一個應用程序,讀取一個文本的內容。

import java.io.*;
public class Input
{
public static void main(String[] args) throws Exception
{
FileInputStream fis = new FileInputStream("D:/abc.txt");
//把字元輸入流轉換成位元組流,並且套上緩沖流管子
BufferedReader buf = new BufferedReader(new InputStreamReader(fis));
String s = null;
while((s=buf.readLine())!=null)
System.out.println(s);
buf.close();
}
}

㈦ java中讀入和輸出文本文件

/**
*測試3:從文本文件中讀取數據
*/
staticvoidtestExample03(){
//1、在內存中打開要讀取文件的字元流對象
try{
Readerreader=newFileReader("e:/ReadMe.log");
//2、從字元流中讀取數據
//一次讀取一個字元(麻煩)
/*intnum=reader.read();
System.out.println((char)num);
num=reader.read();
System.out.println((char)num);*/
//一次讀取一個數組(必須確定數組的長度)
/*char[]cbuf=newchar[10];
reader.read(cbuf);
System.out.println(newString(cbuf));*/
//循環讀取,一次就讀一個
intch=reader.read();
StringBufferbuffer=newStringBuffer();
while(ch!=-1){//讀取成功
buffer.append((char)ch);
ch=reader.read();
}
System.out.println(buffer.toString());
//3、關閉流
reader.close();
}catch(FileNotFoundExceptione){
System.out.println("要讀取的文件不存在:"+e.getMessage());
}catch(IOExceptione){
System.out.println("文件讀取錯誤:"+e.getMessage());
}
}
/**
*測試4:向文本文件中寫入數據
*/
staticvoidtestExample04(){
System.out.println("請輸入內容:");
Stringtext=input.next();
try{
//1、打開流
Writerw=newFileWriter("e:/測試.txt",true);
//2、寫入內容
w.write(text);
//3、關閉流
w.close();
}catch(IOExceptione){
System.out.println("文件寫入錯誤:"+e.getMessage());
}
}
/**
*測試5:使用效率高的字元流讀寫數據
*/
staticvoidtestExample05(){
try{
//1、創建流對象
Readerreader=newFileReader("e:/ReadMe.log");
//構建高效流對象
BufferedReaderbuffReader=newBufferedReader(reader);

//2、讀取一行字元串
Stringline=buffReader.readLine();
StringBufferbuffer=newStringBuffer();
while(line!=null){
buffer.append(line+" ");
line=buffReader.readLine();
}
System.out.println(buffer.toString());;
//3、關閉流
buffReader.close();
reader.close();

Writerw=newFileWriter("e:/NewReadMe.txt");
BufferedWriterbuffWriter=newBufferedWriter(w);

buffWriter.write(buffer.toString());

buffWriter.close();
w.close();
System.out.println("寫入成功!");

}catch(FileNotFoundExceptione){
System.out.println("要讀取的文件不存在:"+e.getMessage());
}catch(IOExceptione){
System.out.println("文件讀取錯誤:"+e.getMessage());
}
}

㈧ java讀寫文本文件

//讀取
StringpathName="D:\1.txt";
BufferedReaderbr=newBufferedReader(newFileInputStream(pathName));
Stringline="";
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();

//寫入
BufferedWriterbr=newBufferedWriter(newFileWriter(pathName));
br.write("要寫入的內容");
br.flush();//刷新緩沖區的數據到文件
br.close();

㈨ 如何通過JAVA 讀取.wps et及 dps文件格式的內容

下面是三個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文件。

㈩ JAVA中讀取文件(二進制,字元)內容的幾種方

JAVA中讀取文件內容的方法有很多,比如按位元組讀取文件內容,按字元讀取文件內容,按行讀取文件內容,隨機讀取文件內容等方法,本文就以上方法的具體實現給出代碼,需要的可以直接復制使用

public class ReadFromFile {
/**
* 以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以位元組為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個位元組
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個位元組
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}

/**
* 以字元為單位讀取文件,常用於讀文本,數字等類型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個字元
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 對於windows下,\r\n這兩個字元在一起時,表示一個換行。
// 但如果這兩個字元分開顯示時,會換兩次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字元為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個字元
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 讀入多個字元到字元數組中,charread為一次讀取字元數
while ((charread = reader.read(tempchars)) != -1) {
// 同樣屏蔽掉\r不顯示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}

} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

/**
* 以行為單位讀取文件,常用於讀面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結束
while ((tempString = reader.readLine()) != null) {
// 顯示行號
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

/**
* 隨機讀取文件內容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("隨機讀取一段文件內容:");
// 打開一個隨機訪問文件流,按只讀方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件長度,位元組數
long fileLength = randomFile.length();
// 讀文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
// 將一次讀取的位元組數賦給byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}

/**
* 顯示輸入流中還剩的位元組數
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("當前位元組輸入流中的位元組數為:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

閱讀全文

與讀取文本文件java相關的資料

熱點內容
台達PLC編譯按鈕在哪裡 瀏覽:137
非編程計算器多少錢 瀏覽:653
房本還完貸款解壓 瀏覽:816
中國程序員有出名嗎 瀏覽:546
亳州雲伺服器 瀏覽:630
程序員最難的面試 瀏覽:892
配音秀app怎麼誦讀 瀏覽:750
sparkcore源碼 瀏覽:100
程序員中年生活 瀏覽:355
讀取加密信息失敗怎麼回事 瀏覽:510
編譯過程之後是預處理嗎 瀏覽:351
安卓是基於什麼做出來 瀏覽:600
視頻字幕提取APP怎麼使用 瀏覽:59
js通過ip地址連接伺服器嗎 瀏覽:848
java數字金額大寫金額 瀏覽:858
人人影視路由器固件編譯 瀏覽:967
照片通訊錄簡訊怎麼從安卓到蘋果 瀏覽:458
邏輯開發編譯環境 瀏覽:672
ce自己編譯 瀏覽:898
javaexe進程 瀏覽:478