當前位置:首頁 » 編程語言 » ioJava讀取文件

ioJava讀取文件

發布時間: 2023-03-28 10:59:04

java io流中哪些流可以獲取文件內容

可以通過BufferedReader 流的形式進行流讀取,之後通過readLine方法獲取到讀取的內容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此時獲取到的bre就是整個文告迅鄭件的緩存
while ((str = bre.readLine())!= null) // 判斷最後一行不存在,為空結束循環
{
System.out.println(str);//原襪頌樣輸出讀到的內容
};
備註: 流用完之後必須close掉,如上面的就應昌世該是:bre.close(),否則bre流會一直存在,直到程序運行結束。

❷ java根據路徑讀取文件

其讀取方法為:

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.util.ArrayList;


publicclassreadFile{
privatestaticArrayList<String>listname=newArrayList<String>();
publicstaticvoidmain(String[]args)throwsException{
readAllFile("C:/Users/HP/Desktop");
System.out.println(listname.size());
}
publicstaticvoidreadAllFile(Stringfilepath){
Filefile=newFile(filepath);
if(!file.isDirectory()){
listname.add(file.getName());
}elseif(file.isDirectory()){
System.out.println("文件");
String[]filelist=file.list();
for(inti=0;i<filelist.length;i++){
Filereadfile=newFile(filepath);
if(!readfile.isDirectory()){
listname.add(readfile.getName());
}elseif(readfile.isDirectory()){
readAllFile(filepath+"\"+filelist[i]);//遞歸
}
}
}
for(inti=0;i<listname.size();i++){
System.out.println(listname.get(i));
}
}
}

❸ Java中對文件進行讀寫操作的基本類是什麼

Java.io包中包括許多類提供許多有關文件的各個方面操作。x0dx0a1 輸入輸出抽象基類InputStream/OutputStream ,實現文件內容操作的基本功能函數read()、 write()、close()、skip()等;一般都是創建出其派生類對象(完成指定的特殊功能)來實現文件讀寫。在文件讀寫的編程過程中主要應該注意異常處理的技術。 x0dx0a2 FileInputStream/FileOutputStream: x0dx0a用於猛磨御本地文件讀寫(二進制格式讀寫並且是順序讀寫,讀和寫要分別創建出不同的文件流對象); x0dx0a本地文件讀寫編程的基本過程為: x0dx0a① 生成文件流對象(對文件讀操作時應該為FileInputStream類,而文件寫應該為FileOutputStream類); x0dx0a② 調用FileInputStream或FileOutputStream類中的功能函數如read()、write(int b)等)讀寫文件內容; x0dx0a③ 關閉文件(close())。 x0dx0a3 PipedInputStream/PipedOutputStream: x0dx0a用於管道輸入輸出(將一個程序或一個線程的輸出結果直接連接到另一個程序或一個線程的輸入埠,實現兩者數據直接傳送。操作時需要連結); x0dx0a4管道的連接: x0dx0a方法之一是通過構造枝岩函數直接將某一個程序的輸出作為另一個程序的輸入,在定義對象時指明目標管道對象 x0dx0aPipedInputStream pInput=new PipedInputStream(); x0dx0aPipedOutputStream pOutput= new PipedOutputStream(pInput); x0dx0a方法之二是利用雙方類中的任一個成員函數 connect()相連接 x0dx0aPipedInputStream pInput=new PipedInputStream(); x0dx0aPipedOutputStream pOutput= new PipedOutputStream(); x0dx0apinput.connect(pOutput); x0dx0a5 管道的輸入與輸出: x0dx0a輸出管道對象調用write()成員函數輸出數據(即向管道的輸入端發送數據);而輸入管道對象調用read()成員函數可以讀起數據(即從輸出管道中獲得數據)。游盯這主要是藉助系統所提供的緩沖機制來實現的。 x0dx0a6隨機文件讀寫: x0dx0aRandomAccessFile類(它直接繼承於Object類而非InputStream/OutputStream類),從而可以實現讀寫文件中任何位置中的數據(只需要改變文件的讀寫位置的指針)。 x0dx0a隨機文件讀寫編程的基本過程為: x0dx0a① 生成流對象並且指明讀寫類型; x0dx0a② 移動讀寫位置; x0dx0a③ 讀寫文件內容; x0dx0a④ 關閉文件。x0dx0ax0dx0a七里河團隊答疑助人,希望我的回答對你有所幫助

❹ 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

讀取一個文件,把文件的內容緩存起來(用List,或者StringBuilder等等的容器)。然後讀取另一個文件,扮蔽再把內容緩存到同一個容器裡面。直到最後一個文件讀取完畢。我在對這個緩存內容的容器進行處理。
但是這個可能會越界.....或者可以試試把第一個文件的內容寫入緩做缺消存,然後讀第二個文件時把第一個文件的內容寫入硬碟同時清空緩存,然後讀純知第二個文件時,再在第一個寫到硬碟的文件後面追加內容....如此類推....

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

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

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

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

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

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

❼ java中怎麼用io流讀寫文件

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;

publicclassTest14{
publicstaticvoidmain(String[]args)throwsIOException{
StringfPath="C:/test.txt";
//讀
BufferedReaderbr=newBufferedReader(newFileReader(fPath));
System.out.println(br.readLine());
br.close();////使用後記得關閉
//寫
BufferedWriterbw=newBufferedWriter(newFileWriter(fPath));
bw.write("寫一段話到文件里");
bw.flush();
bw.close();//使用後記得關閉
}
}

❽ 關於java IO讀取文件路徑問題

看你的包名,和你搜孫的絕對路徑,你的aa.txt和bb.txt文件應該是放在包世滑鏈test.io.TestTxt下吧,讓芹和TestOneToOnetxt類一個路徑,那麼你可以這么做:
File f1 = new File(TestOneToOnetxt.class.getResource("aa.txt").getFile());

❾ java怎麼使用io流讀取一個文件夾裡面

  1. 使用工具類Properties

  2. 工具類有Load()方法 用於載入文件

  3. 首先將文件寫成流(輸入)

    File file=new File(confPath);
    in = new FileInputStream(file);

  4. 載入流load(in)如果需要操作則完成具體操作

  5. 輸出流並保存文件

    out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");
    cp.store(out2);


    完成

  6. 具體實例代碼

    public String updateConfParam(ConfParam cpl) throws IOException {

    String error = null;
    Properties cp=new Properties();
    InputStream in= null;
    OutputStreamWriter out1=null;
    OutputStreamWriter out2=null;
    JSONObject jObj = new JSONObject();
    try {
    String confPath=validateSystem(cpl.getConfAddress()+"/"+cpl.getConfName());
    File file=new File(confPath);
    in = new FileInputStream(file);
    cp.load(in);
    out1=new OutputStreamWriter(new FileOutputStream(confPath+".bak"),"GBK");
    cp.store(out1);
    cpl.setParaOldValue(cp.getProperty(cpl.getParaKey()));
    cp.setProperty(cpl.getParaKey(), cpl.getParaValue());
    out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");
    cp.store(out2);

    jObj.put("paraOldValue", cpl.getParaOldValue());
    jObj.put("paraValue", cpl.getParaValue());
    } catch (FileNotFoundException e) {
    error=e.getMessage();
    } catch (IOException e1) {
    error = e1.getMessage();
    }
    finally{
    if(in !=null){
    in.close();
    }
    if(out1 !=null){
    out1.close();
    }
    if(out2 !=null){
    out2.close();
    }
    if(error != null){
    jObj.put("error", error);
    }
    }
    return jObj.toString();
    }

❿ java 如何用io流 讀取list集合里我需要的內容,然後寫入到.txt文件里各位大俠請賜教

importorg.junit.Test;

importjava.io.*;
importjava.util.ArrayList;
importjava.util.List;

publicclassTestIo{

//路徑
兄沒privatestaticfinalStringPATH="D:\demo1.txt";

@Test
publicvoidtestIo()throwsIOException{
List<String>list=newArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");

Filefile=newFile(PATH);

//如果沒有文件就創建
if(!file.isFile()){
羨檔納file.createNewFile();
}

//
BufferedWriterwriter=newBufferedWriter(newFileWriter(PATH));

for(Stringl:list){
writer.write(l);
}
writer.close();
蠢枝System.out.println("完成");

}
}

結果

希望對你有幫助

熱點內容
解壓到當前文件夾右鍵 發布:2024-04-26 03:57:08 瀏覽:979
html5android教程視頻下載 發布:2024-04-26 03:09:59 瀏覽:867
伺服器的描述是什麼 發布:2024-04-26 03:08:32 瀏覽:394
個人加密 發布:2024-04-26 03:01:23 瀏覽:520
linuxusbgadget 發布:2024-04-26 02:52:54 瀏覽:304
我的世界空島世界伺服器地址 發布:2024-04-26 01:39:08 瀏覽:248
尼爾機械紀元加密 發布:2024-04-26 01:37:11 瀏覽:867
在控制台輸出sql語句 發布:2024-04-26 01:08:12 瀏覽:432
動畫java 發布:2024-04-26 01:02:40 瀏覽:12
得力文件夾5302 發布:2024-04-26 00:21:32 瀏覽:91