当前位置:首页 » 存储配置 » java文本存储

java文本存储

发布时间: 2023-07-29 00:46:26

java如何实现文本保存

try{ FileOutputStream fos=new FileOutputStream("test.txt",true);//true表明会追加内容 PrintWriter pw=new PrintWriter(fos); pw.write(你想写入的内容); pw.flush(); }catch(FileNotFoundException e){ e.printStackTrace(); }finally{ try{ pw.close(); }catch(Exception e){ e.printStackTrace(); } }

㈡ 怎样用Java实现从文本文档中读取数据并存入数据库

不知道你要什么样的文本,文本中的内容是否是有格式的:

这里提供下思路,供参考:
1.文本文件,基本上式字符格式的了,可以用Readerio流
2.如果是格式化的文本,可以按数据的长度读取,readIntreadByte...
3.保存到数据库当然用JDBC了,如果你读取出来封装成POJO了,也可以选择OM框架



importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;

/**
*文件读取和写入数据库
*@author樊云升
*
*/
publicclassFilesReader{

publicFilesReader(){

}

/**
*读取文件内容
*@paramFILE
*@return
*/
publicStringre_content(StringFILE){
Stringcontent="";
try{
BufferedReaderbufRead=newBufferedReader(newInputStreamReader(newFileInputStream(FILE)));
Stringstr;
while((str=bufRead.readLine())!=null){
content+=str+" ";
}
}catch(IOExceptionioe){
ioe.printStackTrace();
}
returncontent;
}

/**
*将特定字符写入数据库中(原来我写的是重写文件,你这里这里将content写入数据库就OK)
*@parampath
*@return
*/
publicbooleanwriteFile(Stringcontent){
try{
//数据库写入代码
}catch(Exceptione){
out.close();
returnfalse;
}
returntrue;
}

publicstaticvoidmain(String[]args){
Stringcontent=newFilesReader().re_content("D:\AJAX.htm");
newFilesReader().writeFile(content);
}

}

㈢ 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向数据库中存储大量文字

你把数据库的一个表中的一个字段设置为“备注(TEXT)”类型,就可以存放大量的内容了。
这与
java本身没有什么关系。
主要是数据库的结构设计问题。
用java读取和写入的方法其实
没有什么特别
的,
rs.getString("...");

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:432
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:146
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:234
java驼峰 发布:2025-02-02 09:13:26 浏览:651
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726