A. java 怎樣從文件中讀取特定的內容,比如從第一個換行讀取到第二個換行。求代碼
C盤下新建1.txt
B. Java 怎樣讀取TXT文件並把每行內容賦值到一個數組裡面,最好能有全部代碼。
在Java中,使用JFrame和JTextArea讀取TXT文件並顯示其內容。首先,創建一個JFrame窗口,設置標題為"io"。然後,獲取容器並創建一個JTextArea用於顯示文本內容。接著,創建一個Panel用於放置按鈕,以及一個JScrollPane來滾動顯示文本區域。將文本區域添加到滾動面板中,使文本能夠自動換行。
為了讀取文件內容,我們創建了一個JButton,命名為「讀取文件」。當點擊此按鈕時,觸發一個ActionListener。在ActionListener中,首先通過File對象定義文件路徑,然後使用InputStreamReader和FileInputStream來讀取文件內容。注意使用"gbk"編碼,根據文件編碼選擇合適的編碼方式。
使用read(char[] c)方法讀取文件,該方法將讀取到的內容寫入到字元數組c中,並返回讀取的長度。若文件為空,使用JDialog顯示提示信息;否則,將讀取的內容設置到JTextArea中。
在讀取過程中,可能遇到IOException,需要進行異常處理。最後,確保在使用完InputStreamReader後正確關閉流,避免資源泄露。
將文本區域和按鈕添加到容器中,設置布局為BorderLayout,將按鈕放置在底部。設置窗口大小和可見性。這樣,當用戶點擊「讀取文件」按鈕時,程序會讀取指定路徑的TXT文件內容,並顯示在JTextArea中。
當然,你需要根據實際情況修改文件路徑和編碼。此示例提供了一個基本框架,你可以根據需要進行調整和擴展。
C. java讀取文件每一行
java循環問題,讀文件的每一行可以通過BufferedReader流的形式進行讀取,之後循環輸出每一行的內容。
BufferedReaderbre=null;
try{
Stringfile="D:/test/test.txt";
bre=newBufferedReader(newFileReader(file));//file為文件的路徑+文件名稱+文件後綴
while((str=bre.readLine())!=null)//●判斷最後一行不存在,為空結束循環
{
System.out.println(str);//原樣輸出讀到的內容
};
備註:流用完之後必須close掉,如上面的就應該是:bre.close();
Java中如何一行行地讀文件
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
public?class?ReadTest?{
public?static?void?main(String[]?args)?{
//?讀控制台輸入的文字!
BufferedReader?br?=?null;
String?str?=?null;
try?{
br?=?new?BufferedReader(new?InputStreamReader(System.in));
while?(true)?{
str?=?br.readLine();
if?(str.equals("886"))
break;
System.out.println(str);
}
//?讀文本文件..
br?=?new?BufferedReader(new?FileReader(new?File("C:\Users\Administrator\Desktop\地址.txt")));
for?(str?=?br.readLine();?str?!=?null;?str?=?br.readLine())?{
//列印你讀的文本數據!
System.out.println(str);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
核心就是:readLine()方法,一行一行的讀!
java怎麼讀入文件,並逐行輸出java讀入文件,並逐行輸出,先在D://home建立個文件夾,然後創建一個a.txt文件,然後編輯文件,文本編輯的編碼是utf-8,然後用流逐行讀取輸出,如下:
import?java.io.BufferedInputStream;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.InputStream;
import?java.io.InputStreamReader;
public?class?TestC?{
public?static?void?main(String[]?args){
//獲取要讀取的文件
?File?readFile=new?File("D://home/a.txt");
?//輸入IO流聲明
????????InputStream?in=null;
????????InputStreamReader?ir=null;
????????BufferedReader?br=null;
????????
????????try?{
????????//用流讀取文件
in=new?BufferedInputStream(new?FileInputStream(readFile));
//如果你文件已utf-8編碼的就按這個編碼來讀取,不然又中文會讀取到亂碼
ir=new?InputStreamReader(in,"utf-8");
//字元輸入流中讀取文本,這樣可以一行一行讀取
br=new?BufferedReader(ir);
String?line="";
//一行一行讀取
while((line=br.readLine())!=null){
System.out.println(line);
}
????????}?catch?(Exception?e)?{
e.printStackTrace();
}finally{
//一定要關閉流,倒序關閉
try?{
if(br!=null){
br.close();
}
if(ir!=null){
ir.close();
}
if(in!=null){
in.close();
}
}?catch?(Exception?e2)?{
}
}
????
}
}
結果:
helloworld
您好
123456
java讀取txt文件每一行多少個位元組import?java.io.File;
import?java.io.RandomAccessFile;
/**
?*?2016年8月31日下午7:00:37
?*?
?*?@author?3306?TODO?計算位元組數
?*
?*/
public?class?FileUtil?{
????public?static?void?main(String[]?args)?{
????????String?filePath?=?"d:/test.txt";//?d盤必須存在test.txt文件
????????readEachLine(filePath);
????}
????/**
?????*?列印文件每一行的位元組數
?????*?
?????*?@param?filePath
?????*????????????文件路徑
?????*/
????private?static?void?readEachLine(String?filePath)?{
????????try?{
????????????File?file?=?new?File(filePath);
????????????if?(file.exists())?{//?文件存在
????????????????RandomAccessFile?accessFile?=?new?RandomAccessFile(file,?"r");//?只賦予讀的許可權
????????????????String?line?=?"";
????????????????long?lineIndex?=?1;
????????????????while?(null?!=?(line?=?accessFile.readLine()))?{
????????????????????System.out.println("line"?+?(lineIndex++)?+?":?"?+?line.getBytes().length);//?列印行號和位元組數
????????????????}
????????????????accessFile.close();
????????????}
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
}
D. JAVA 讀取 TXT 文件中的內容
在Java中讀取TXT文件內容,可以使用FileInputStream類讀取文件,再通過 InputStreamReader類將其轉換為可讀取的字元流,接著使用BufferedReader類進行逐行讀取。具體代碼如下:
首先創建FileInputStream對象,使用File類指定文件路徑,例如:
FileInputStream fis = new FileInputStream(new File(path));
接著,創建一個InputStreamReader對象,將FileInputStream作為參數傳遞給它:
InputStreamReader isr = new InputStreamReader(fis);
然後,利用BufferedReader對字元流進行處理:
BufferedReader br = new BufferedReader(isr);
為了存儲每一行讀取的內容,我們定義一個字元串變數str:
String str = "";
使用while循環來讀取文件中的每一行,直到沒有更多的行為止。在循環內部,使用readLine()方法讀取一行,並將其賦值給str變數:
while((str=br.readLine())!=null){
System.out.println(str);
}
這個循環會持續執行,直到讀取到文件的最後一行。每次循環中,readLine()方法會讀取一行文本,並將其存儲在str變數中,然後將str的值列印出來。當readLine()返回null時,表示文件讀取完畢,循環結束。
需要注意的是,使用完這些對象後,應該確保關閉它們以釋放資源。可以使用try-with-resources語句來自動完成關閉操作,如下所示:
try (FileInputStream fis = new FileInputStream(new File(path));
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);) {
String str = "";
while((str=br.readLine())!=null){
System.out.println(str);
}
}
這樣可以確保在讀取文件過程中,資源得到及時釋放,避免資源泄露。
以上就是使用Java讀取TXT文件內容的完整步驟,包括創建文件流對象、讀取文件內容以及處理讀取到的數據。