java保存文件|java里数据怎么保存到硬盘或TXT文件里去

㈠ java对文件的保存问题

File本身就有但参数的构造函数,new File(String path),这个path就是你要读取的文件的绝对路径。然后将这个File作为参数传入输入流中,通过输入流把他读到字节数组中,然后通过输出流打印在文本编辑器中

㈡ Java 如何把数据保存到TXT文件,

首先,打开一个txt文件,Filefile=newFile("文件路径");然后,封装输出流,DataOutputStreamos=newDataOutputStream(newFileOutputStream(file));接着,往os里面写数据,os.writeInt(…)os.writeByte(…)os.writeChar(…)等等,你要写什么样类型的数据,就调用什么样类型的方法。最后,记得关掉输出流,调用os.close()

㈢ java保存和打开文件的方法

可以不用那个方法,这样就能用io里的fileinputstream()来作了,是吧?public void readFile()//用于读取文件内容{try{FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);String str;while((str = br.readLine()) != null){txtEdit.setText(txtEdit.getText()+str+"\n");}br.close();fr.close();}catch(Exception ee){ee.printStackTrace();}}public void saveFile()//保存文件{try{FileWriter fw = new FileWriter(file);fw.write(txtEdit.getText());fw.close();}catch(Exception e){e.printStackTrace();}}我大致知道你的意思应该怎么作了,你给弹出的button++事件就好了,代码不用说了吧,你自己想想吧!我用给你作了下,给你点代码: public void open()throws IOException{ String filepath=jfc1.getSelectedFile().getAbsolutePath(); FileReader fr=new FileReader(filepath); BufferedReader br=new BufferedReader(fr); String str; while((str = br.readLine())!=null) jta.setText(jta.getText()+str+"\n"); br.close(); fr.close(); }public void save()throws IOException{ String filepath2=jfc2.getSelectedFile().getAbsolutePath(); FileWriter fw=new FileWriter(filepath2); BufferedWriter bw=new BufferedWriter(fw); PrintWriter pw=new PrintWriter(bw);pw.print(jta.getText());bw.close(); fw.close();}

㈣ 编写好的JAVA程序如何导出保存并运行

1、首先需要在记事本中编写一个“hello,下午好”程序。

㈤ 怎么用java储存文件(save file)

这是我原来做的例子,里面有文件储存的内容,代码不多,给你参考参考./** * 五个按钮的故事,西西哈。 */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里数据怎么保存到硬盘或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 的文件保存和读取问题

可以通过BufferedReader 流的形式进行流读取,之后通过readLine方法获取到每行的内容,之后通过OutputStreamWriter进行文件写入。 BufferedReader bre = null;OutputStreamWriter pw = null;//定义一个流try {String file = "D:/test/test.txt";bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环{pw.write(str );//将要写入文件的内容,写入到新文件};pw.close();//关闭流bre .close();//关闭流备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

㈧ java怎么保存文件

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

㈨ java怎样保存打开就可执行的文件

会生抄成.

class文件,只能用反编译软件看,在你的eclipse工作空间,一般是workspace下的工程中,路径时你自己指定的.

编译输出路径的默认位置,普通工程:bin,web工程:WEB-INF/classes,maven工程:target/classes.

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


赞 (0)