當前位置:首頁 » 編程語言 » java存入文件

java存入文件

發布時間: 2023-08-01 06:24:52

java 怎麼將數據寫入TXT文件

定義一個輸出文件,然後輸出就可以了,具體見下面的代碼

importjava.io.*;

publicclassStreamDemo

{

publicstaticvoidmain(Stringargs[])

{

Filef=newFile("c:\temp.txt");

OutputStreamout=null;

try

{

out=newFileOutputStream(f);

}

catch(FileNotFoundExceptione)

{

e.printStackTrace();

}

//將字元串轉成位元組數組

byteb[]="HelloWorld!!!".getBytes();

try

{

//將byte數組寫入到文件之中

out.write(b);

}

catch(IOExceptione1)

{

e1.printStackTrace();

}

try

{

out.close();

}

catch(IOExceptione2)

{

e2.printStackTrace();

}//以下為讀文件操作

InputStreamin=null;

try

{

in=newFileInputStream(f);

}

catch(FileNotFoundExceptione3)

{

e3.printStackTrace();

}

//開辟一個空間用於接收文件讀進來的數據

byteb1[]=newbyte[1024];

inti=0;

try

{

//將b1的引用傳遞到read()方法之中,同時此方法返回讀入數據的個數

i=in.read(b1);

}

catch(IOExceptione4)

{

e4.printStackTrace();

}

try

{

in.close();

}

catch(IOExceptione5)

{

e5.printStackTrace();

}

//將byte數組轉換為字元串輸出

System.out.println(newString(b1,0,i));

}

}

㈡ Java怎麼使用表格輸入數據並保存為文件

在 Java 中使用表格輸入數據並保存為文件,可以使用 Swing 包中的 JTable 組件和 I/O 類庫中的文件操作方法。
首先,你需要創建一個 JTable 對象,並在表格中輸入數據。例如:
String[] columnNames = {"Name", "Age", "Gender"};Object[][] data = {{"Alice", 20, "Female"}, {"Bob", 25, "Male"}};JTable table = new JTable(data, columnNames);

然後,你需要使用 I/O 類庫中的文件操作方法,將表格中的數據保存到文件中。例如,你可以使用 FileWriter 類來寫入文件:
FileWriter writer = new FileWriter("table.txt");for (int i = 0; i < table.getRowCount(); i++) { for (int j = 0; j < table.getColumnCount(); j++) {
writer.write(table.getValueAt(i, j).toString());
writer.write("\t");
}
writer.write("\n");
}
writer.close();

上面的代碼會將表格中的數據寫入文件 "table.txt" 中,每行數據之間用製表符隔開,每列數據之間用換行符隔開。
注意,在使用文件操作方法時,你需要處理文件讀寫可能出現的異常。你可以使用 try-catch 語句將文件操作代碼包裝起來,以便在發生異常時能夠正確處理。
這是一個簡單的例子,你可以根據自己的需求來調整代碼。例如,你可以使用其他的 I/O 類,比如 BufferedWriter 或 PrintWriter 等,來更方便地寫入文件。你還可以使用其他的文件格式,比如 CSV、Excel 等,來保存數據。

㈢ 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中如何將輸出結果放入文件中

這個就需要java中的I/O流來對文件進行讀寫,舉個例子:以FileWriter類來寫文件

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

publicclassTest{
publicstaticvoidrwFile(){
FileWriterfw=null;
try{
fw=newFileWriter("f:\text.txt",true);
fw.write("123");//這里向文件中輸入結果123
fw.flush();
}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();
}
}

這個代碼是向文件F盤的text.txt中輸入123

熱點內容
vs編譯器會自己加空格嗎 發布:2025-02-07 07:23:05 瀏覽:174
光遇切換賬號安卓要輸入些什麼 發布:2025-02-07 07:10:20 瀏覽:501
多角線演算法 發布:2025-02-07 07:08:56 瀏覽:273
有效提高ftp傳輸速度 發布:2025-02-07 07:06:47 瀏覽:703
寒靈之劍腳本 發布:2025-02-07 06:57:12 瀏覽:119
解壓的窗口 發布:2025-02-07 06:44:34 瀏覽:798
android身份證 發布:2025-02-07 06:36:43 瀏覽:431
python的庫在哪 發布:2025-02-07 06:30:24 瀏覽:349
帶鎖的鉛筆如何改密碼 發布:2025-02-07 06:18:05 瀏覽:165
ubuntu搭建samba伺服器 發布:2025-02-07 05:52:54 瀏覽:55