1. java 從資料庫取出數據並保存到本地文本中
先看資料庫表, 我裡面有46條記錄,其中有三條重復,我就拿其中一條emp_id 為"
DWR65030M" 做例子
裡面有兩條記錄 ,實現了
2. JAVA里怎麼生成txt並保存到區域網內某台計算機的指定文件夾里
我有個 文件傳輸的程序~~~ 可能你需要改一下你的 IP!!
通過Socket 處理IO流!(遠程都測試過的 絕對行!)
流程是這樣的~~:
客戶端選擇文件,並發送!
服務端接受數據,並保存為文件!
//////////////////////////////////////////////客戶端程序:
////////////////////文件一:Window.java
package clientwindow;
import client.ChooseFile;
import client.SendFile;
import javax.swing.JOptionPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Window {
public boolean ready;
public static JTextArea textArea;
private JTextField portTextField;
private JTextField hostTextField;
private JTextField fileTextField;
private JFrame frame;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application
*/
public Window() {
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("客戶端");
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 390, 275);
frame.setLocation(300, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
final JMenu fileMenu = new JMenu();
fileMenu.setText("文件");
menuBar.add(fileMenu);
final JMenuItem exitMenuItem = new JMenuItem();
exitMenuItem.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JOptionPane.showMessageDialog(null, "Bye Bye!");
System.exit(1);
}
});
exitMenuItem.setText("退出");
fileMenu.add(exitMenuItem);
final JMenu helpMenu = new JMenu();
helpMenu.setText("幫助");
menuBar.add(helpMenu);
final JMenuItem viewMenuItem = new JMenuItem();
viewMenuItem.setText("查看");
helpMenu.add(viewMenuItem);
final JLabel fileLabel = new JLabel();
fileLabel.setText("文件位置:");
fileLabel.setBounds(30, 40, 80, 15);
frame.getContentPane().add(fileLabel);
fileTextField = new JTextField();
fileTextField.setBounds(116, 37, 163, 22);
frame.getContentPane().add(fileTextField);
//選擇文件
final JButton viewButton = new JButton();
viewButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
new ChooseFile();
if(ChooseFile.FILENAME == null || ChooseFile.PATH == null){
JOptionPane.showMessageDialog(null, "請選擇存儲文件!");
}
else{
fileTextField.setText(ChooseFile.PATH);
textArea.append("選擇文件:"+ChooseFile.FILENAME+"\n");
ready = true;
}
}
});
viewButton.setText("瀏覽");
viewButton.setBounds(296, 37, 78, 22);
frame.getContentPane().add(viewButton);
final JLabel addLabel = new JLabel();
addLabel.setText("主機地址:");
addLabel.setBounds(30, 93, 80, 15);
frame.getContentPane().add(addLabel);
hostTextField = new JTextField();
hostTextField.setBounds(116, 90, 163, 22);
hostTextField.setText("127.0.0.1");
hostTextField.setEditable(false);
frame.getContentPane().add(hostTextField);
final JLabel portLabel = new JLabel();
portLabel.setText("埠");
portLabel.setBounds(285, 93, 42, 15);
frame.getContentPane().add(portLabel);
portTextField = new JTextField();
portTextField.setBounds(322, 90, 52, 22);
portTextField.setText("6666");
portTextField.setEditable(false);
frame.getContentPane().add(portTextField);
//發送文件
final JButton sendButton = new JButton();
sendButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try{
if(ready){
new SendFile(fileTextField.getText(),"127.0.0.1",6666);//這里更改IP
}
else{
JOptionPane.showMessageDialog(null, "請選擇文件!");
}
}
catch(Exception exception){
System.out.println(exception);
}
}
});
sendButton.setText("發送");
sendButton.setBounds(296, 137, 78, 22);
frame.getContentPane().add(sendButton);
//退出程序
final JButton exitButton = new JButton();
exitButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JOptionPane.showMessageDialog(null, "Bye Bye!");
System.exit(1);
}
});
exitButton.setText("退出");
exitButton.setBounds(296, 182, 78, 22);
frame.getContentPane().add(exitButton);
final JLabel sendLabel = new JLabel();
sendLabel.setText("發送狀態:");
sendLabel.setBounds(30, 140, 80, 15);
frame.getContentPane().add(sendLabel);
textArea = new JTextArea();
//textArea.add(JTextArea.);
textArea.setBounds(116, 138, 163, 66);
textArea.setEditable(false);
textArea.setLineWrap(true);
frame.getContentPane().add(textArea);
//textArea.add(scrollPane);
}
}
///////////////////////////////////////////文件二:ChooseFile.java
/**
*
*/
package client;
import javax.swing.*;
/**
* @author 泥鰍
*
*/
// 該類用於選擇本地文件,並返迴文件路徑和文件名
public class ChooseFile {
private JFileChooser jfile;
private JDialog jd;
public static String PATH;
public static String FILENAME;
public ChooseFile(){
jfile = new JFileChooser();
jd = new JDialog();
// jd.add(jfile);
// jfile.setVisible(true);
// jd.setLocation(320, 270);
// jd.setSize(280, 230);
// jd.setVisible(true);
if(JFileChooser.APPROVE_OPTION == jfile.showOpenDialog(jd)){
// System.out.println("OK");
String oldstr = jfile.getSelectedFile().getPath();
//返迴路徑
PATH = changePath(oldstr);
//返迴文件名
FILENAME = jfile.getSelectedFile().getName();
}
}
//將路徑格式轉換成 java所識別的路徑格式
public String changePath(String oldstr){
// String newstr;
/*
for(int i=0;i<=oldstr.length();i++){
if(oldstr.charAt(i)=='\\'){
}
}
*/
return oldstr.replaceAll("\\\\", "\\\\\\\\");
}
}
//////////////////////////////////////////文件三:SendFile.java
/**
*
*/
package client;
import clientwindow.Window;
import java.io.*;
import java.net.*;
/**
* @author 泥鰍
*
*/
public class SendFile {
private Socket socket;
private FileInputStream input;
private DataOutputStream output;
private File file;
public SendFile(String fileName,String ip,int port) throws IOException{
//生成一個 File對象 以獲得選擇文件長度(大小)
file = new File(fileName);
/* if((int)file.length()>2000000000){
//文件太大就不發送
JOptionPane.showMessageDialog(null, "文件太大,請重新選擇!");
}
else{
*/ Window.textArea.append("文件大小:"+String.valueOf(file.length())+"Bytes"+"\n");
socket = new Socket(ip,port);
input = new FileInputStream(fileName);
output = new DataOutputStream(socket.getOutputStream());
byte[] b = new byte[1024];
int length = 0;
while((length = input.read(b)) !=-1){
output.write(b,0,length);
System.out.println("發送位元組:"+length);
}
// }
Window.textArea.append("發送完畢"+"\n");
System.out.println("發送完畢");
input.close();
output.flush();
output.close();
}
}
/////////////////////////////////////////////////服務端:
////////////////////////文件一:Window.java
/**
*
*/
package client;
import clientwindow.Window;
import java.io.*;
import java.net.*;
/**
* @author 泥鰍
*
*/
public class SendFile {
private Socket socket;
private FileInputStream input;
private DataOutputStream output;
private File file;
public SendFile(String fileName,String ip,int port) throws IOException{
//生成一個 File對象 以獲得選擇文件長度(大小)
file = new File(fileName);
/* if((int)file.length()>2000000000){
//文件太大就不發送
JOptionPane.showMessageDialog(null, "文件太大,請重新選擇!");
}
else{
*/ Window.textArea.append("文件大小:"+String.valueOf(file.length())+"Bytes"+"\n");
socket = new Socket(ip,port);
input = new FileInputStream(fileName);
output = new DataOutputStream(socket.getOutputStream());
byte[] b = new byte[1024];
int length = 0;
while((length = input.read(b)) !=-1){
output.write(b,0,length);
System.out.println("發送位元組:"+length);
}
// }
Window.textArea.append("發送完畢"+"\n");
System.out.println("發送完畢");
input.close();
output.flush();
output.close();
}
}
////////////////////////////////////////文件二:GetFile.java
/**
*
*/
package server;
import java.net.*;
import java.io.*;
import serverwindow.Window;
/**
* @author 泥鰍
*
*/
public class GetFile extends Thread{
private ServerSocket server;
private Socket socket;
private FileOutputStream output;
private DataInputStream input;
public GetFile() throws IOException{
}
public void run(){
try{
server = new ServerSocket(6666);
socket = new Socket();
System.out.println("伺服器啟動...");
socket = server.accept();
byte[] b = new byte[1024];
input = new DataInputStream(socket.getInputStream());
output = new FileOutputStream(StoreFile.PATH,true);
int length = 0;
while((length = input.read(b) )!= -1){
output.write(b,0,length);
System.out.println("接受成功:"+"接受位元組"+length);
}
Window.textArea.append("保存完畢!"+"\n");
System.out.println("保存完畢!");
server.close();
socket.close();
input.close();
output.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
}
/////////////////////////文件三:StoreFile.java
/**
*
*/
package server;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import java.io.File;
import javax.swing.JOptionPane;
/**
* @author 泥鰍
*
*/
public class StoreFile {
private JDialog jd;
private JFileChooser chooseFile;
public static String PATH;
public static String FILENAME;
private File fileName;
public StoreFile(){
jd = new JDialog();
chooseFile = new JFileChooser();
if(JFileChooser.APPROVE_OPTION == chooseFile.showSaveDialog(jd)){
String oldstr = chooseFile.getSelectedFile().getPath();
String newStr = changePath(oldstr);
fileName = new File(newStr);
if(fileName.exists()){
JOptionPane.showMessageDialog(null, "請文件已經存在,輸入其他文件名!");
}
else{
//返迴路徑
PATH = newStr;
//返迴文件名
FILENAME = chooseFile.getSelectedFile().getName();
}
/* if(oldstr == null){
JOptionPane.showMessageDialog(null, "請選擇存儲文件!");
}
*/
}
}
//將路徑格式轉換成 java所識別的路徑格式
public String changePath(String oldstr){
return oldstr.replaceAll("\\\\", "\\\\\\\\");
}
}
3. Java 如何把數據保存到TXT文件,
Java通過使用I/O文件操作類,來創建輸入輸出流,將數據保存在file tet文件裡面。示例如下:
package*&####&*_1_*&####&*;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
publicclassWriteFileExample{
publicstaticvoidmain(String[]args){
FileOutputStreamfop=null;
Filefile;
Stringcontent="Thisisthetextcontent";
try{
file=newFile("c:/newfile.txt");
fop=newFileOutputStream(file);
//iffiledoesntexists,thencreateit
if(!file.exists()){
file.createNewFile();
}
//getthecontentinbytes
byte[]contentInBytes=content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(fop!=null){
fop.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
4. java不用資料庫怎樣保存數據
比較常用的就是保存到XML文件,我的博客(網路搜索「圖顯IP」,第一位就是我的網站)就是用種方式,這樣就可以不用受資料庫的限制,輕量級,方便部署,哪怕是虛擬主機,不用單獨購買資料庫