導航:首頁 > 文件教程 > java寫入指定文件

java寫入指定文件

發布時間:2023-01-01 20:22:38

⑴ 用java將數據寫入文件

不支持直接在文件的最前面插入一行。
實現這個功能的大概步驟是:
1、新建個臨時文件。
2、在臨時文件中寫入新數據。
3、把老文件中的數據都復制到臨時文件中。
4、刪除老文件。
5、臨時文件重命名為老文件的名字。

⑵ 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中如何把一個文件放到指定的文件夾中

首先獲得fileoutput對象時,寫入具體的目錄就可以了。
比如:你要寫入到d:\java\test目錄下。
方法一:
java代碼
string
name
=
"out.html";
string
dir
=
"d:\\java\\test";
file
file
=
new
file(dir,name);
fileoutputstream
out
=
new
fileoutputstream(file);
方法二:
java代碼
fileoutputstream
out
=
new
fileoutputstream(dir+"\\"+name);

⑷ java怎麼把文本內容在指定文件夾里

java寫入文件到指定文件夾的方法主要有兩種:利用PrintStream和利用StringBuffer


例如將文本「I'm the text to be write」寫入到文件夾D:/test下,並命名為test.txt,則兩種方式簡單實現代碼如下:


1. 利用PrintStream寫文件

publicvoidPrintStreamDemo(){
try{
FileOutputStreamout=newFileOutputStream("D:/test.txt");
PrintStreamp=newPrintStream(out);
p.println("I'mthetexttobewrite");
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
}


2. 利用StringBuffer寫文件

publicvoidStringBufferDemo()throwsIOException{
Filefile=newFile("D:/test.txt");
if(!file.exists())
file.createNewFile();
FileOutputStreamout=newFileOutputStream(file,true);
StringBuffersb=newStringBuffer();
sb.append("I'mthetexttobewrite");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}

提示:利用StringBuffer寫文件可以設定使用何種編碼,有效解決中文問題。

⑸ 怎麼把指定文件存在指定目錄下java

java寫入文件到指定文件夾的方法主要有兩種:利用PrintStream和利用StringBuffer
例如將文本「I'm the text to be write」寫入到文件夾D:/test下,並命名為test.txt,則兩種方式簡單實現代碼如下:
1. 利用PrintStream寫文件
public void PrintStreamDemo(){
try {
FileOutputStream out=new FileOutputStream("D:/test.txt");
PrintStream p=new PrintStream(out);
p.println("I'm the text to be write");
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
2. 利用StringBuffer寫文件

public void StringBufferDemo() throws IOException{

File file=new File("D:/test.txt");
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);
StringBuffer sb=new StringBuffer();
sb.append("I'm the text to be write");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}

提示:利用StringBuffer寫文件可以設定使用何種編碼,有效解決中文問題。

⑹ java怎樣向一個文件(如txt文件)中寫入一段數據,保存後下一次打開繼續使用

  1. 這個JDK的類庫都幫我們實現好了。如FileWriter類:
    public FileWriter(File file,boolean append)
    throws IOException

  2. 根據給定的 File 對象構造一個 FileWriter 對象。如果第二個參數為 true,則將位元組寫入文件末尾處,而不是寫入文件開始處。

  3. 例子程序如下:
    import java.io.File ;
    import java.io.Writer ;
    import java.io.FileWriter ;
    public class WriterDemo02{
    public static void main(String args[]) throws Exception}


⑺ java寫入文件只能是string嗎

不是的,還有
一,FileWritter寫入文件
FileWritter, 字元流寫入字元到文件。默認情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個true (布爾)值作為FileWritter構造函數的第二個參數,它會保留現有的內容,並追加新內容在文件的末尾。
二,BufferedWriter寫入文件
緩沖字元(BufferedWriter )是一個字元流類來處理字元數據。不同於位元組流(數據轉換成位元組),你可以直接寫字元串,數組或字元數據保存到文件。
三,FileOutputStream寫入文件
文件輸出流是一種用於處理原始二進制數據的位元組流類。為了將數據寫入到文件中,必須將數據轉換為位元組,並保存到文件。請參閱下面的完整的例子。

⑻ JAVA中如何將生成的數據寫入到文件中

packagecom.pig.database.file.txt;

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.PrintStream;

/**
*@authorzhuruhong
*
*若要變更這個產生的類別註解的範本,請移至
*視窗>喜好設定>Java>程式碼產生>程式碼和註解
*/
publicclassWriteTxtFileByName{
privateStringfilename=null;

publicWriteTxtFileByName(Stringfilename){
this.filename=filename;
}

publicvoidwriteFileByName(Stringcontent){
FiledocFile=newFile(filename);
try{
docFile.createNewFile();
FileOutputStreamtxtfile=newFileOutputStream(docFile);
PrintStreamp=newPrintStream(txtfile);
p.println(content);
txtfile.close();
p.close();
}catch(IOExceptione){
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
WriteTxtFileByNamewfbn=newWriteTxtFileByName("title");
wfbn.writeFileByName("content");
}
}

給你一個實例。

閱讀全文

與java寫入指定文件相關的資料

熱點內容
現在哪裡開設了編程課 瀏覽:230
漫一網路培訓學校有哪些 瀏覽:752
酷狗app如何在電腦同步播放 瀏覽:668
雲浮微信群 瀏覽:344
公司文件打不開 瀏覽:267
LOL的設置文件在哪裡 瀏覽:254
線上開庭是什麼app 瀏覽:526
新中大軟體怎麼設置文件夾 瀏覽:807
git刪除文件夾 瀏覽:353
皇室戰爭怎麼升級競技場 瀏覽:447
雅虎統計工具 瀏覽:741
edgepdf文件分類 瀏覽:901
cad為什麼捕捉不到外部參照文件 瀏覽:935
重慶一共有多少個網站 瀏覽:34
k8s配置文件env創建失敗 瀏覽:197
編程序在電腦上叫做什麼 瀏覽:92
qq閱讀可賺 瀏覽:21
怎樣查找web儲存文件圖片 瀏覽:681
人口檔案資料庫包括什麼信息 瀏覽:709
手機有什麼好玩的星戰網路游戲 瀏覽:15

友情鏈接