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

java保存到文件

發布時間: 2022-06-11 13:36:44

① 如何把java文本框內容保存到文本文件里

參考下面代碼:

importjava.io.*;
importjavax.swing.*;
importjava.awt.FlowLayout;
importjava.awt.event.*;
{
JButtonb;JTextFieldt;
publicWriterTo(){
super("文本框內容寫入文件");
JLabell=newJLabel("請輸入內容:");
t=newJTextField(20);
b=newJButton("寫入");
b.addActionListener(this);
this.add(l);
this.add(t);
this.add(b);
this.setLayout(newFlowLayout());
this.pack();
this.setVisible(true);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==b){
if(t.getText().equals("")){
JOptionPane.showMessageDialog(null,"請輸入內容~","錯誤",JOptionPane.ERROR_MESSAGE);
t.grabFocus();
}else{
write(t.getText());
JOptionPane.showMessageDialog(null,"寫入成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
publicvoidwrite(Stringline){
try{
Filef=newFile("c:/文本框.txt");//向指定文本框內寫入
FileWriterfw=newFileWriter(f);
fw.write(line);
fw.close();
}catch(Exceptione){

}
}
publicstaticvoidmain(String[]args){
newWriterTo();
}

}

② java里數據怎麼保存到硬碟或TXT文件里去

Java是通過使用I/O文件操作類,創建輸入輸出流,將數據保存在指定的路徑下的文件裡面。
示例代碼:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteFileTest {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("D:/test.txt");//初始化file
fop = new FileOutputStream(file);//初始化輸出流
// 若文件不存在,則創建它
if (!file.exists()) {
file.createNewFile();
}
// 獲取位元組的內容數組
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);//寫出到指定路徑文件中位元組的內容數組
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) { //捕捉異常
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) { //捕捉異常
e.printStackTrace();
}
}
}
}

③ Java 如何把數據保存到TXT文件,

首先,打開一個txt文件,File
file
=
new
File("文件路徑");
然後,封裝輸出流,DataOutputStream
os
=
new
DataOutputStream(new
FileOutputStream(file));
接著,往os裡面寫數據,os.writeInt(...)
os.writeByte(...)
os.writeChar(...)等等,你要寫什麼樣類型的數據,就調用什麼樣類型的方法。
最後,記得關掉輸出流,調用os.close()

④ java怎麼保存文件

可以使用java.io.FileOutputStream流保存任意文件或者用java.io.ObjectOutputStream流保存類文件

⑤ java將數據保存到txt文件

首先創建一個新的txt文件,然後new File(「txt文件路徑」),
封裝一個輸入輸出流,將要寫入的數據寫入到txt中,刷新流,關閉流。
代碼如下:
public static void main(String[] args) throws IOException{
String str = "這個項目什麼時候上線";
File file;//創建文件夾
FileOutputStream stream = null;//new文件流
try {
file = new File("C:/Users/qisf/Desktop/Aa.txt");
stream = new FileOutputStream (file);//將文件夾放在文件流中
if (!file.exists()) {
file.createNewFile();
}
byte[] contentInBytes = str.getBytes();//轉化成位元組形
stream.write(contentInBytes);//寫入
stream.flush(); //寫完之後刷新
stream.close();//關閉流
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

⑥ 編寫好的JAVA程序如何導出保存並運行

首先,你需要在記事本中寫一個「你好,下午好」的程序。

⑦ java如何保存文件

這是我原來做的例子,裡面有文件儲存的內容,代碼不多,給你參考參考.
/**
* 五個按鈕的故事,西西哈。
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileMessage extends Frame implements ActionListener
{
private static final long serialVersionUID = 10L;
Dialog dia;
private Panel p;
private File fi;
Process po=null;
private String s;
private TextArea ta;
private FileDialog fd;
private Button b1,b2,b3,b4,b5;
private Button b6;
public FileMessage()
{
super("文本文件處理");
setBackground( Color.LIGHT_GRAY );
setLocation(200,300);
setResizable( false);
setVisible( true);
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit( 0);
}
});
}
public void init()
{
ta=new TextArea("\n\n\n\n\n\t\t\t\t文本顯示區");
ta.setSize(30,5);
ta.setEditable(false);
add( ta,"North");
p=new Panel();
add( p,"Center");
b1=new Button("瀏覽");
b2=new Button("保存");
b3=new Button("清空");
b4=new Button("關閉");
b5=new Button("獨立打開");
b6=new Button("確定");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
fd=new FileDialog(this,"請選擇文件",FileDialog.LOAD);
fd.setDirectory("f:\\note");
pack();
dia=new Dialog(this,"注意",true);
dia.setLayout(new BorderLayout());
Panel p1=new Panel();
p1.add( b6);
dia.add(new Label(" 請先選擇文件"),BorderLayout.CENTER);
dia.add( p1,BorderLayout.SOUTH);
dia.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dia.setVisible( false);
}
});
dia.setLocation(310,370);
dia.setSize(200,130);
}
public static void main(String[] args)
{
new FileMessage().init();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
fd.setVisible(true);
s=fd.getDirectory()+fd.getFile();
fi=new File(s);
byte[] b=new byte[(int)fi.length()];
try
{
new FileInputStream(fi).read(b);
ta.setText(new String(b,0,(int)fi.length()));
}
catch(Exception e1){}
ta.setEditable(true);
}
else if(e.getSource()==b2)
{
try
{
if(ta.getText().equals("保存成功")||ta.getText() .equals( ""))
{}
else
{
new FileOutputStream(fi).write(ta.getText().getBytes());
ta.setText("保存成功");
ta.setEditable(false);
}
}
catch(FileNotFoundException e1)
{
ta.setText(e1.getMessage());
}
catch(IOException e1)
{
ta.setText("出現IOException異常");
}
}
else if(e.getSource()==b4)
System.exit(0);
else if(e.getSource()==b3)
{
ta.setText("");
ta.setEditable( false);
}
else if(e.getSource()==b5)
{
if(s==null)
{
dia.setVisible(true);
}
else
{
try
{
po=Runtime.getRuntime().exec("notepad.exe "+s);
}
catch(Exception ei)
{}
}
}
else if(e.getSource() ==b6)
{
dia.setVisible(false);
}
}
}

⑧ java中包可以保存工程需要的各種文件

是的
輸入文件內容和文件名稱,將文件保存即可。
Java是一門面向對象編程語言,1990年代初由詹姆斯·高斯林等人開發出Java語言的雛形,最初被命名為Oak,後隨著互聯網的發展,經過對Oak的改造,1995年5月Java正式發布。
Java具有簡單性、面向對象、分布式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
2009年,甲骨文公司宣布收購Sun。2010年,Java編程語言的共同創始人之一詹姆斯·高斯林從Oracle公司辭職。2011年,甲骨文公司舉行了全球性的活動,以慶祝Java7的推出,隨後Java7正式發布。2014年,甲骨文公司發布了Java8正式版。

⑨ java編輯程序保存為什麼文件

可以保存。
首先需要在記事本中編寫一個hello,下午好的程序,編寫完成後保存該文件並將文件名改為與類名相同,把文件的格式從txt改成java文件,更改完畢後打開cmd指令,輸入javac,如果下方出現許多東西,說明環境變數已經配置成功,否則就要去配置環境變數,找到java文件所在的位置也在cmd中找到它。

熱點內容
面java 發布:2025-02-07 23:36:21 瀏覽:614
編譯原理練習題第三章答案 發布:2025-02-07 23:35:05 瀏覽:752
爐石寫腳本 發布:2025-02-07 23:31:24 瀏覽:985
stdstring源碼 發布:2025-02-07 23:26:46 瀏覽:782
伺服器在手機上怎麼開 發布:2025-02-07 23:25:07 瀏覽:734
我的世界怎麼進2s2t伺服器 發布:2025-02-07 23:08:47 瀏覽:925
丁霞訪問 發布:2025-02-07 22:56:19 瀏覽:855
java中set集合 發布:2025-02-07 22:43:34 瀏覽:31
播放這個wifi密碼是多少 發布:2025-02-07 22:34:54 瀏覽:100
視頻存儲時間長了有雪花 發布:2025-02-07 22:24:34 瀏覽:569