① 在java中怎樣用 I/O流讀取txt文件

packagecom.code;

importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;

publicclassTest3{

/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
//例如對c:/a.txt進行讀取
Stringstring;
try{
=newFileInputStream("c://a.txt");
BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(fileInputStream));
string=bufferedReader.readLine();
System.out.println(string);
while(string!=null){
string=bufferedReader.readLine();
System.out.println(string);


}




}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}


}

}

② 關於Java.IO讀取文件

import java.io.DataInputStream; //導包
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class nh {//類名
public static void main(String args[]){
try {
FileInputStream fi = new FileInputStream("c:/read.txt"); //創io流 設路徑
int size = fi.available(); //位元組數
byte[] b = new byte[size];//創位元組數組
fi.read(b);//讀取
fi.close();//關流
for(byte bo:b){//遍歷
System.out.print((char)bo);//列印
}

} catch (FileNotFoundException e) {//可能異常(找不到文件等)
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
有不懂的再找我....

③ java的幾種IO流讀取文件方式

一、超類:


位元組流: InputStream(讀入流) OutputStream(寫出流)


字元流: Reader(字元 讀入流) Writer (字元寫出流)

二、文件操作流


位元組流: FileInputStream ,FileOutputStream


字元流: FileReader, FileWriter(用法與位元組流基本相同,不寫)

//1.指定要讀 的文件目錄及名稱


File file =new File("文件路徑");


//2.創建文件讀入流對象


FileInputStream fis =new FileInputStream(file);


//3.定義結束標志,可用位元組數組讀取


int i =0 ;


while((i = fis.read())!=-1){


//i 就是從文件中讀取的位元組,讀完後返回-1


}


//4.關閉流


fis.close();


//5.處理異常

//1.指定要寫到的文件目錄及名稱


File file =new File("文件路徑");


//2.創建文件讀入流對象


FileOutputStream fos =new FileOutputStream(file);


//3.定義結束標志


fos.write(要寫出的位元組或者位元組數組);


//4.刷新和關閉流


fos.flush();


fos.close();


//5.處理異常

三、緩沖流:


位元組緩沖流: BufferedInputStream,BufferedOutputStream


字元緩沖流:BufferedReader ,BufferedWriter


緩沖流是對流的操作的功能的加強,提高了數據的讀寫效率。既然緩沖流是對流的功能和讀寫效率的加強和提高,所以在創建緩沖流的對象時應該要傳入要加強的流對象。

//1.指定要讀 的文件目錄及名稱


File file =new File("文件路徑");


//2.創建文件讀入流對象


FileInputStream fis =new FileInputStream(file);


//3.創建緩沖流對象加強fis功能


BufferedInputStream bis =new BufferedInputStream(fis);


//4.定義結束標志,可用位元組數組讀取


int i =0 ;


while((i = bis.read())!=-1){


//i 就是從文件中讀取的位元組,讀完後返回-1


}


//5.關閉流


bis.close();


//6.處理異常

//1.指定要寫到的文件目錄及名稱


File file =new File("文件路徑");


//2.創建文件讀入流對象


FileOutputStream fos =new FileOutputStream(file);


//3.創建緩沖流對象加強fos功能


BufferedOutputStream bos=new BufferedOutputStream(fos);


//4.向流中寫入數據


bos.write(要寫出的位元組或者位元組數組);


//5.刷新和關閉流


bos.flush();


bos.close();


//6.處理異常

四、對象流


ObjectInputStream ,ObjectOutputStream


不同於以上兩種類型的流這里只能用位元組對對象進行操作原因可以看上篇的編碼表比照原理

ObjectOutputStream對象的序列化:


將java程序中的對象寫到本地磁碟里用ObjectOutputStream


eg:將Person類的對象序列化到磁碟

  1. 創建Person類


    注1:此類要實現Serializable介面,此介面為標志性介面


    注2:此類要有無參的構造函數


    注3:一旦序列化此類不能再修改


    class Person implements Serializable{


    public Person(){}


    }


    2.創建對象流對象


    註:要增強功能可以將傳入文件緩沖流


    ObjectOutputStream oos =new ObjectOutputStream(


    new FileOutputStream(new File("文件路徑")));


    3.寫入對象 ,一般會將對象用集合存儲起來然後直接將集合寫入文件


    List<Person> list =new ArrayList<>();


    list.add(new Person());


    ...(可以添加多個)


    oos.writeObject(list);


    4.關閉流,處理異常


    oos.flush();


    oos.close();

五、轉換流:

這類流是用於將字元轉換為位元組輸入輸出,用於操作字元文件,屬於字元流的子類,所以後綴為reader,writer;前綴inputstream,outputstream;

注 :要傳入位元組流作為參賽


InputStreamReader: 字元轉換輸出流


OutputStreamWriter:字元轉換輸入流

//1.獲取鍵盤輸入的位元組流對象

inInputStream in =Stream.in;

/*2.用轉換流將位元組流對象轉換為字元流對象,方便調用字元緩沖流的readeLine()方法*/


InputStreamReader isr =new InputStreamReader(in);


/*5.創建字元轉換輸出流對象osw,方便把輸入的字元流轉換為位元組輸出到本地文件。*/


OutputStreamWriter osw =new OutputStreamWriter(new
FileOutputStream(new File("文件名")));



/*3.現在isr是字元流,可以作為參數傳入字元緩沖流中*/


BufferedReader br =new BufferedReader(isr);

/*4.可以調用字元緩沖流br的readLine()方法度一行輸入文本*/


String line =null;


while((line =br.readLine()){


osw.write(line);//osw是字元流對象,可以直接操作字元串

}



註:InputStreamReader isr =new InputStreamReader(new "各種類型的位元組輸入流都行即是:後綴為InputStream就行");


OutputStreamWriter osw =new OutputStreamWriter(new
"後綴為OutputStream就行");

六、區別記憶


1.對象流是可以讀寫幾乎所有類型的只要是對象就行,而位元組字元流,只能讀寫單個位元組字元或者位元組字元數組,以上沒有讀寫位元組字元數組的;注意對象流只有位元組流!


2.字元和位元組循環讀入的結束條件int i=0; (i =fis.read())!=-1
用字元數組復制文件(fr 讀入流 ,fw寫出流),位元組流也是相同的用法

int i = 0; char[] c = new char[1024];


while((i = fr.reade()) !=-1)){


fw.write(c,0,i);


}

123456

3.對象流裡面套緩沖流的情景:


new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(「文件路徑」))));

4.記憶流及其功能的方法:


前綴表示功能,後綴表示流的類型;


比如說FileInputStream 前綴:File,表示操作的磁碟,後綴:intputstream,表示是位元組輸入流。


同理 FileReader:表示操作文件的字元流


ObjectInputStream :操作對象的位元組輸入流

5.拓展:獲取鍵盤輸入的字元的緩沖流的寫法:


new BufferedReader(new InputStreamReader(System.in)));


將位元組以字元形式輸出到控制台的字元緩沖流的寫法:


new BufferedWriter( new OutputStreamWriter(System.out))

④ java用IO流一行行的讀取文件

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class ThreadDemo{
public static void main(String[] args) throws IOException{
BufferedReader fr=new BufferedReader(new FileReader("a.txt"));//a.txt代表文件
String line=null;
while((line=fr.readLine())!=null){
System.out.println(line);
}
}
}

⑤ java io 可以讀取什麼格式的文件

任何文件都能讀取
主要分兩大類:
文本文件

其他位元組文件(圖片,視頻等非文本文件)
文本文件可以按字元(或者一行一行讀取)
其他文件要用位元組流讀取

⑥ java io讀取文件名字

代碼如下:
/**
* 文件讀寫
*/
public void addLogger(String fileName) {
String line = "";
File file = new File("E://tkmFile//knlgfile//template//template.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while (reader.read() != -1) {
line += reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
line = " " + line.substring(0, line.lastIndexOf("]"));
line += ",[/"750/", /"" + fileName + "/", /"" + fileName + "/"]" + "];";
System.out.println(line);
FileWriter fw = null;
try {
fw = new FileWriter(Constants.templatePath);
fw.write(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();
fw = null;
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

⑦ java io流如何讀取文件效率高

你在類的使用上不太恰當。

如果你想讀取文本內容,你應該使用InputStreamReader這個類,使用這個類並且指定文本內容的字元集,就可以讀出正確的內容。

InputStream這個類是用來讀取二進制位元組的,比如做文件復制的時候,應該是用InputStream這個類。

不管是InputStreamReader還是InputStream,都可以聲明臨時緩沖數組,不同的是InputStreamReader聲明的數組是:char[] cs = new char[1024]

而InputStream聲明的數組是:byte[] bs = new byte[1024]

⑧ java IO 文件讀取問題

很明顯,methord2的while判定語句中readline了一次而沒有輸出,System.out.println(br.readLine())讀了下一行,才輸出。
改成 while ( (s=br.readLine()) != null) {
System.out.println(s);
}

⑨ java IO如何完整讀取一個文本文件

把你的代碼貼出來看看,幫你找找在哪裡出問題了。。
最好就是附帶你要讀取的1W行文本文件