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

java文本寫入文件

發布時間:2023-01-18 15:42:22

java把控制台輸入的文本追加到文本文件中

/**
* 從控制台接收用戶輸入的數據,並存儲在磁碟上
* @param filePath
*/
static void userPrint(String filePath){
BufferedReader br = null;
BufferedWriter bw = null;
try{
//通過System.in返回一個InputStream對象用於構造一個InputStreamReader對象
//再用來構造一個Buffered對象
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new FileWriter(filePath,true)); //true表示是否追加
String str = br.readLine(); //接收用戶輸入
while(!str.equals("exit")){ //如果用戶輸入exit則退出循環
bw.write(str); //將用戶輸入的字元串寫入文件
bw.newLine(); //換行
bw.flush(); //刷新緩沖區,將緩沖區的字元寫入磁碟!
str = br.readLine(); //繼續接收輸入
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
finally{
try {
bw.close(); //關閉對象前會調用bw.flush();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

❷ java怎麼讀取某個文本文件的內容寫入到另一個文本文件

/** * 讀出寫出 * @param oldFileName 源文件 * @param newFileName 新文件 * @throws IOException */public static void testRead(String oldFileName,String newFileName) throws IOException{ FileOutputStream fos=new FileOutputStream(new File(newFileName)); RandomAccessFile raf=new RandomAccessFile(new File(oldFileName), "rw"); fos.write(raf.read(new byte[8])); fos.flush(); fos.close(); raf.close();} public static void fileWrite() throws FileNotFoundException, IOException { testRead("G:/森雲/測試文件1。txt","G:/newFile.txt");}

❸ 關於Java中向文件寫入數據的問題

可以使用java中的FileWriter類向文件中寫入數據。很簡單。代碼例子如下:

importjava.io.FileWriter;
importjava.io.IOException;

publicclassFilewriter{

privatestaticfinalStringLINE_SEPARATOR=System.getProperty("line.separator");

/**
*
*@paramargs
*@throwsIOException
*/
publicstaticvoidmain(String[]args)throwsIOException{
/**
*創建一個可以往文件中寫入字元數據的字元流輸出流對象
*創建時必須明確文件的目的地
*如果文件不存在,這回自動創建。如果文件存在,則會覆蓋。
*當路徑錯誤時會拋異常
*
*當在創建時加入true參數,回實現對文件的續寫。
*/
FileWriterfw=newFileWriter("C:\demo1.txt",false);
/**
*調用該對象的write方法,向文件寫入字元。
*
*其實寫入到了臨時存儲緩沖區中
*/
fw.write("hello world!");//windows中的換行為 unix下為 。
fw.write("hahaha");
/**
*進行刷新,將字元寫到目的地中。
*/
//fw.flush();
/**
*關閉流,關閉資源。在關閉前會調用flush方法刷新緩沖區。關閉後在寫的話,會拋IOException
*/
fw.close();


}

}

❹ 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往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();
}
}

❻ java如何寫入文件

packagefilewriter;

importjava.io.FileWriter;
importjava.io.IOException;

publicclassIOExceptionDemo{

privatestaticfinalStringLINE_SEPARATOR=System.getProperty("line.separator");
publicstaticvoidmain(String[]args){

FileWriterfw=null;
try{
fw=newFileWriter("k:\Demo.txt",true);
fw.write("hello"+LINE_SEPARATOR+"world!");
}catch(Exceptione){
System.out.println(e.toString());
}finally{
if(fw!=null)
try{
fw.close();
}catch(IOExceptione){
thrownewRuntimeException("關閉失敗!");
}
}
}
}

❼ JAVA 寫入TXT文件

在pw.write(s),後面加入pw.flush()即可。
在建立printWriter實例時(PrintWrite pw = new PrintWrite(fos,,true)),需要用boolean型指定,是不是自動刷新,如果沒有指定自動刷新,則需要自己來flush.

呵,呵,要講清楚啦。
樓主我只要50分額。

❽ java 寫入文本文件字元串怎麼多個寫入

使用Java中的File類,url為文件的絕對地址,str為輸入的字元串內容。
代碼如下:
String str="i love china!"
File txt=new File("url");
if(!txt.exists()){
txt.createNewFile();
}
byte bytes[]=new byte[512];
bytes=str.getBytes(); //新加的
int b=str.length(); //改
FileOutputStream fos=new FileOutputStream(txt);
fos.write(bytes,0,b);
fos.close();

❾ Java中,怎樣把一段文字寫入一個文本文件中

參考下面代碼,把「網路知道」寫到D盤的寫入文字.txt中

importjava.io.*;

publicclassC{
publicstaticvoidmain(String[]args)throwsException{
PrintWriterpw=newPrintWriter(newFileWriter("D:\寫入文字.txt"));
pw.print("網路知道");
pw.close();
}
}

❿ Java中怎麼將字元串按行寫入到txt文件中

java寫入文本文件的方法很多,比如FileWriter

FileWriterfw=newFileWriter("D:/Test.txt");
Strings="helloworld ";
fw.write(s,0,s.length());
s="helloworld2 ";
fw.write(s,0,s.length());
fw.flush();

這樣就寫了兩行了。其中斜線n是換行符

閱讀全文

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

熱點內容
微信小程序源碼轉成抖音 瀏覽:654
優省油app怎麼沒法下載 瀏覽:72
pdf格式轉換excel 瀏覽:625
高爾夫6壓縮機響 瀏覽:310
優盤文件夾自動恢復 瀏覽:76
有伺服器怎麼製作小程序 瀏覽:132
程序員怎麼避開外包公司 瀏覽:604
刺激戰場國際服體驗伺服器滿了怎麼辦 瀏覽:487
python的number是什麼意思 瀏覽:539
剪映app怎麼把視頻鏡像 瀏覽:464
python長連接消息提醒 瀏覽:767
山西省美術演算法 瀏覽:95
華為手機怎麼不給別人刪app 瀏覽:814
c51單片機程序實例 瀏覽:112
騰訊幻核加密貓 瀏覽:782
雅思聽力真經pdf 瀏覽:442
甘肅戴爾伺服器虛擬化設計雲主機 瀏覽:236
怎麼購買蓋世童書APP 瀏覽:248
軍校的程序員 瀏覽:447
程序員被女警察追 瀏覽:455