導航:首頁 > 編程語言 > java寫入文件流

java寫入文件流

發布時間:2024-05-28 06:46:52

1. java 中簡述使用流進行讀寫文本文件的步驟

InputStream
三個基本的讀方法
abstract int read() : 讀取一個位元組數據,並返回讀到的數據,如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b) : 將數據讀入一個位元組數組,同時返回實際讀取的位元組數。如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b, int off, int len) :將數據讀入一個位元組數組,同時返回實際讀取的位元組數。如果返回-1,表示讀到了輸入流的末尾。off指定在數組b中存放數據的起始偏移位置;len指定讀取的最大位元組數。
OutputStream
三個基本的寫方法
abstract void write(int b) :往輸出流中寫入一個位元組。
void write(byte[] b) :往輸出流中寫入數組b中的所有位元組。
void write(byte[] b, int off, int len) :往輸出流中寫入數組b中從偏移量off開始的len個位元組的數據。
其它方法
void flush() :刷新輸出流,強制緩沖區中的輸出位元組被寫出。
void close() :關閉輸出流,釋放和這個流相關的系統資源。

2. Java往TXT文件寫入文字的問題

用java往文件裡面寫入文字可以用到java裡面的I/O流來實現功能, 一般都是用FileWriter類來實現要求。具體的代碼示例如下:

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;

/**
*java讀寫文件
*讀取d:/1.txt文件內容,寫入f:/text.txt文件中.
*
*寫入文件換行用fw.write(" ");
*或者fw.write(" ");
*@authoryoung
*
*/
publicclassFileWriterTest{
//讀寫文件
publicstaticvoidrwFile(){
FileWriterfw=null;
BufferedReaderbr=null;
try{
//定義FileWriter對象,關聯文件f: ext.txt,用來向文件寫內容
fw=newFileWriter("f:\text.txt",true);
//定義bufferedReader對象,用來讀取d:1.txt文件內容
br=newBufferedReader(newInputStreamReader(
newFileInputStream("d:\1.txt"),"UTF-8"));
Stringline=null;
//每次讀取一行內容,循環讀取,讀到文件末尾結束
while((line=br.readLine())!=null){
System.out.println("文件內容:"+line);
fw.write(line);
//刷新緩沖流,
fw.flush();
}
//關閉I/O流
br.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(fw!=null){
try{
fw.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
publicstaticvoidmain(String[]args){
rwFile();
}
}

3. java代碼 如何向TXT文件寫入內容

向txt文件寫入內容基本思路就是獲得一個file對象,新建一個txt文件,打開I/O操作流,使用寫入方法進行讀寫內容,示例如下:

packagecommon;

importjava.io.*;

importjava.util.ArrayList;

publicclassIOTest{

publicstaticvoidmain(Stringargs[]){

ReadDate();

WriteDate();

}

/**

*讀取數據

*/

publicstaticvoidReadDate(){

Stringurl=「e:/2.txt」;

try{

FileReaderread=newFileReader(newFile(url));

StringBuffersb=newStringBuffer();

charch[]=newchar[1024];

intd=read.read(ch);

while(d!=-1){

Stringstr=newString(ch,0,d);

sb.append(str);

d=read.read(ch);

}

System.out.print(sb.toString());

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

/**

*寫入數據

*/

publicstaticvoidWriteDate(){

try{

Filefile=newFile(「D:/abc.txt」);

if(file.exists()){

file.delete();

}

file.createNewFile();

BufferedWriteroutput=newBufferedWriter(newFileWriter(file));

ArrayListResolveList=newArrayList();

for(inti=0;i<10;i++){

ResolveList.add(Math.random()*100);

}

for(inti=0;i

output.write(String.valueOf(ResolveList.get(i))+「 」);

}

output.close();

}catch(Exceptionex){

System.out.println(ex);

}

}

}

原文出自【比特網】,轉載請保留原文鏈接:http://soft.chinabyte.com/database/303/12439303.shtml
閱讀全文

與java寫入文件流相關的資料

熱點內容
lua編譯有什麼用 瀏覽:350
買了伺服器如何架設 瀏覽:929
如何運用mex函數編譯c 瀏覽:896
24歲程序員倒在工作上 瀏覽:919
怎麼算梁的加密區 瀏覽:93
2016版office怎麼解壓 瀏覽:270
怎麼把安卓手機調的更暗 瀏覽:167
蘋果空間新演算法 瀏覽:91
android文字動畫效果 瀏覽:146
java調試命令 瀏覽:213
android子線程looper 瀏覽:782
linux安裝java7 瀏覽:189
單片機fdh 瀏覽:107
單片機原理與應用下載 瀏覽:590
順風車車主app在哪裡下載 瀏覽:235
雷石柏雲伺服器功率 瀏覽:102
全球服是什麼伺服器 瀏覽:237
感測器怎麼連接伺服器 瀏覽:705
大數學pdf 瀏覽:646
哪個app可以登記自己的藏書 瀏覽:89