java寫文件保存代碼 java實(shí)現(xiàn)文件保存

JAVA編程問(wèn)題 如何改寫一個(gè)文件并保存? 要求完整代碼

假定開始沒(méi)有這個(gè)文件,在插入數(shù)據(jù)時(shí)建立文件。

專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)渾南免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

FILE *fp;

fopen(激法館盒弋谷龜貪駭楷"c:\\a.txt","wt+");這個(gè)是打開以寫或讀的方式打開文件。打開后就可以寫入了,用for循環(huán),例如你有4組數(shù)據(jù),

for(int i;i=4;i++)

{

fprintf(fp,"%s%s%s%s",a,b,c,d);

}

fprintf();就實(shí)現(xiàn)了把數(shù)據(jù)寫入文件的功能。跟printf();差不多,只是一個(gè)是往文件里寫,一個(gè)是往屏幕上寫。

上面就實(shí)現(xiàn)了插入操作。

如果你想刪除一個(gè)數(shù)據(jù),就先在數(shù)組中刪除,然后重新進(jìn)行上述寫入文件操作。 要是讀取數(shù)據(jù)的話就在打開文件時(shí):fp = fopen("c:\\a.txt",r+);

要是還不明白的話就看下書,這兩個(gè)函數(shù)就可以滿足你的需要。

java如何保存文件

這是我原來(lái)做的例子,里面有文件儲(chǔ)存的內(nèi)容,代碼不多,給你參考參考.

/**

* 五個(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ú)立打開");

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);

}

}

}

怎么用java語(yǔ)言編寫一個(gè)保存文件程序

既有保存也有打開,希望你滿意

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)

{

}

}

}

}

}

本人初學(xué)Java,我用eclipse編寫的代碼文件保存位置在哪里?

打開eclipse,在你要查找的項(xiàng)目單擊鼠標(biāo)右鍵,選中屬性選項(xiàng),就會(huì)有一個(gè)窗口彈出來(lái),里面有一個(gè)相對(duì)路徑和絕對(duì)路勁,這個(gè)絕對(duì)路徑就是你文件的存放位置

分享名稱:java寫文件保存代碼 java實(shí)現(xiàn)文件保存
本文來(lái)源:http://muchs.cn/article0/hjcjoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站建設(shè)、品牌網(wǎng)站制作軟件開發(fā)、搜索引擎優(yōu)化品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名