當前位置:首頁 » 存儲配置 » 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 瀏覽:233
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:532
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726