這是我原來(lái)做的例子,里面有文件儲(chǔ)存的內(nèi)容,代碼不多,給你參考參考.
創(chuàng)新互聯(lián)專(zhuān)注于將樂(lè)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供將樂(lè)營(yíng)銷(xiāo)型網(wǎng)站建設(shè),將樂(lè)網(wǎng)站制作、將樂(lè)網(wǎng)頁(yè)設(shè)計(jì)、將樂(lè)網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造將樂(lè)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供將樂(lè)網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
/**
* 五個(gè)按鈕的故事,西西哈。
*/
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文本顯示區(qū)");
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("關(guān)閉");
b5=new Button("獨(dú)立打開(kāi)");
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,"請(qǐng)選擇文件",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(" 請(qǐng)先選擇文件"),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("出現(xiàn)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);
}
}
}
如下:
1.首先需要確保你的電腦正確安裝了Java環(huán)境并且環(huán)境變量都配置完成,之后我們?cè)陔娔X上編輯好自己的Java程序,找到文件保存路徑,在下一步要使用。
2.打開(kāi)CMD,打開(kāi)運(yùn)行窗口輸入CMD即可,然后我們?cè)诶锩孢M(jìn)入到Java文件保存的路徑,這里保存在D盤(pán)的Java文件夾中,所以我們輸入d:進(jìn)入到D盤(pán),然后輸入cdJava進(jìn)入到文件夾中在里面我們輸入javacrandom.java并且按下回車(chē),這里random.java是我們編輯的Java程序文件名,同學(xué)們替換成自己的文件名即可。
3.隨后我們?cè)谠鹊奈募A中會(huì)看到出現(xiàn)了一個(gè)同樣名稱(chēng)的但是是以.class結(jié)尾的文件,這個(gè)就是已經(jīng)編譯好的java文件了,我們繼續(xù)回到CMD中,在里面輸入javarandom然后回車(chē)即可看到運(yùn)行結(jié)果了。
既有保存也有打開(kāi),希望你滿(mǎn)意
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Example10_9
{
public static void main(String args[])
{
FileWindows win = new FileWindows();
}
}
class FileWindows extends Frame implements ActionListener
{
FileDialog filedialog_save,filedialog_load;
MenuBar menubar;
Menu menu;
MenuItem itemOpen,itemSave;
TextArea text;
BufferedReader in;
FileReader file_reader;
BufferedWriter out;
FileWriter file_writer;
FileWindows()
{
super("A window with a file");
setSize(260,270);
setVisible(true);
menubar = new MenuBar();
menu = new Menu("File");
itemOpen = new MenuItem("Open file");
itemSave = new MenuItem("Save file");
itemOpen.addActionListener(this);
itemSave.addActionListener(this);
menu.add(itemOpen);
menu.add(itemSave);
menubar.add(menu);
setMenuBar(menubar);
filedialog_save = new FileDialog(this,"Save file dailog",FileDialog.SAVE);
filedialog_load = new FileDialog(this,"Open file dialog",FileDialog.LOAD);
filedialog_save.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
filedialog_save.setVisible(false);
}
});
filedialog_load.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
filedialog_load.setVisible(false);
}
});
addWindowListener(new WindowAdapter()
{
public void WindowAdapter(WindowEvent e)
{
System.exit(0);
}
});
text = new TextArea(10,10);
add(text,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==itemOpen)
{
filedialog_load.setVisible(true);
text.setText(null);
String s;
if(filedialog_load.getFile()!=null)
{
try{
File file = new File (filedialog_load.getDirectory(),filedialog_load.getFile());
file_reader = new FileReader(file);
in = new BufferedReader(file_reader);
while((s = in.readLine())!=null)
{
text.append(s+'\n');
}
in.close();
file_reader.close();
}
catch(IOException e2)
{
}
}
}
else if(e.getSource()==itemSave)
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{
try{
File file = new File(filedialog_save.getDirectory(),filedialog_save.getFile());
file_writer = new FileWriter(file);
out = new BufferedWriter(file_writer);
out.write(text.getText(),0,(text.getText()).length());
out.close();
file_writer.close();
}
catch(IOException e2)
{
}
}
}
}
}
文件應(yīng)該以.java的格式保存 代碼沒(méi)有任何區(qū)別
然后在命令提示符里 首先進(jìn)入到你的.java文件的目錄里,輸入javac 類(lèi)名.java
然后再是 java 類(lèi)名
當(dāng)前文章:java中保存文件的代碼 java怎樣保存寫(xiě)的程序
地址分享:http://www.rwnh.cn/article48/ddgodhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司、響應(yīng)式網(wǎng)站、虛擬主機(jī)、服務(wù)器托管、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容