當前位置:首頁 » 編程語言 » java如何txt文件

java如何txt文件

發布時間: 2023-12-12 09:54:27

java如何讀取txt文件

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


㈡ java 怎麼導入txt的數據

txt文件里格式是什麼樣的?

import java.nio.file.*;
import java.io.*;
import java.util.stream.*;
try(var writer = new PrintWriter("/tmp/numbers.txt")){
IntStream.rangeClosed(200, 20000).forEach(writer::println); //生成一個每行一個整數的文本文件
Files.lines(Paths.get("/tmp/numbers.txt")).mapToInt(Integer::parseInt).toArray(); // 將文件中的數字讀到一個數組里
} catch(Exception e){
e.printStackTrace();
}

㈢ java如何讀取txt文件內容

給你兩個方法,你可以看看;
//獲取值返回String文本
publicStringtxt2String(StringfilePath){
Filefile=newFile(filePath);
StringBuilderresult=newStringBuilder();
try{
BufferedReaderbr=newBufferedReader(newFileReader(file));//構造一個BufferedReader類來讀取文件
Strings=null;
while((s=br.readLine())!=null){//使用readLine方法,一次讀一行
result.append(s+System.lineSeparator());
}
br.close();
}catch(Exceptione){
e.printStackTrace();
}
returnresult.toString();
}

//獲取值不返回String文本
publicvoidreadTxtFile(StringfilePath){
try{
Stringencoding="GBK";
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file),encoding);//考慮到編碼格式
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exceptione){
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}

㈣ java 怎麼將數據寫入TXT文件

定義一個輸出文件,然後輸出就可以了,具體見下面的代碼

importjava.io.*;

publicclassStreamDemo

{

publicstaticvoidmain(Stringargs[])

{

Filef=newFile("c:\temp.txt");

OutputStreamout=null;

try

{

out=newFileOutputStream(f);

}

catch(FileNotFoundExceptione)

{

e.printStackTrace();

}

//將字元串轉成位元組數組

byteb[]="HelloWorld!!!".getBytes();

try

{

//將byte數組寫入到文件之中

out.write(b);

}

catch(IOExceptione1)

{

e1.printStackTrace();

}

try

{

out.close();

}

catch(IOExceptione2)

{

e2.printStackTrace();

}//以下為讀文件操作

InputStreamin=null;

try

{

in=newFileInputStream(f);

}

catch(FileNotFoundExceptione3)

{

e3.printStackTrace();

}

//開辟一個空間用於接收文件讀進來的數據

byteb1[]=newbyte[1024];

inti=0;

try

{

//將b1的引用傳遞到read()方法之中,同時此方法返回讀入數據的個數

i=in.read(b1);

}

catch(IOExceptione4)

{

e4.printStackTrace();

}

try

{

in.close();

}

catch(IOExceptione5)

{

e5.printStackTrace();

}

//將byte數組轉換為字元串輸出

System.out.println(newString(b1,0,i));

}

}

㈤ java如何讀取一個txt文件的所有內容

importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.io.Reader;


publicclassH{
/**
*功能:Java讀取txt文件的內容
*步驟:1:先獲得文件句柄
*2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
*3:讀取到輸入流後,需要讀取生成位元組流
*4:一行一行的輸出。readline()。
*備註:需要考慮的是異常情況
*@paramfilePath
*/
publicstaticvoidreadTxtFile(StringfilePath){
try{
Stringencoding="GBK";
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file),encoding);//考慮到編碼格式
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exceptione){
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}

}

publicstaticvoidmain(Stringargv[]){
StringfilePath="L:\20121012.txt";
//"res/";
readTxtFile(filePath);
}}

熱點內容
sql2008錯誤233 發布:2025-07-03 02:28:52 瀏覽:167
創建資料庫語句mysql 發布:2025-07-03 02:14:34 瀏覽:145
python量化投資 發布:2025-07-03 02:05:11 瀏覽:804
proxy代理伺服器地址 發布:2025-07-03 01:56:52 瀏覽:909
ps選區存儲 發布:2025-07-03 01:55:21 瀏覽:841
sql2008連接數 發布:2025-07-03 01:55:20 瀏覽:245
androidstring 發布:2025-07-03 01:53:55 瀏覽:182
密碼sql注入 發布:2025-07-03 00:44:07 瀏覽:555
oa伺服器需要什麼硬體 發布:2025-07-03 00:36:05 瀏覽:512
4mol的naoh怎麼配置 發布:2025-07-03 00:19:10 瀏覽:856