「java保存文件」java保存文件到项目中
本篇文章给大家谈谈java保存文件,以及java保存文件到项目中对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
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如何保存文件
这是我原来做的例子,里面有文件储存的内容,代码不多,给你参考参考.
/**
* 五个按钮的故事,西西哈。
*/
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.io.FileOutputStream流保存任意文件或者用java.io.ObjectOutputStream流保存类文件
java编辑程序保存为什么文件
可以保存。
首先需要在记事本中编写一个hello,下午好的程序,编写完成后保存该文件并将文件名改为与类名相同,把文件的格式从txt改成java文件,更改完毕后打开cmd指令,输入javac,如果下方出现许多东西,说明环境变量已经配置成功,否则就要去配置环境变量,找到java文件所在的位置也在cmd中找到它。
java 文件保存和打开
// 打开文件 filePath文件路径xx.txt文件名
Runtime.getRuntime().exec("notepad.exe filePath:/xx.txt");
// 保存文件 echo + 内容(sourse) + gt; +filePath/xx.txt
Runtime.getRuntime().exec("cmd /k echo sourse gt; d:/xx.txt");
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOTest {
public static void main(String[] args){
File f = new File("E:\\1.txt");
//读
String oldStr = "";
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line = br.readLine())!=null){
oldStr += line;
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//写
String newStr = "zxc";
FileWriter fw;
try {
fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(oldStr+"\r\n"+newStr);//不想换行就把\r\n去掉
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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保存文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java保存文件到项目中、java保存文件的信息别忘了在本站进行查找喔。
发布于:2022-12-13,除非注明,否则均为
原创文章,转载请注明出处。