A. 編寫好的java程序如何導出保存並運行
首先,你需要在記事本中寫一個「你好,下午好」的程序。
B. JAVA 把指定的文件 復制到目標文件夾下 怎麼寫啊
// 由絕對路徑得到輸出流
//path源路徑
//fileCopeToPath 目標路徑
String path="D:\我的文檔\My Pictures\1.jpg ";
String fileCopeToPath=" D:\我的文檔\test ";
File file =new File(path);
if(file.exists){
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos= new FileOutputStream( fileCopeToPath+ File.separator +path.subString(path.lastIndexOf("/\"),path.length));
byte[] b = new byte[fis.available()];
int len = 0;
while ((len = fis.read(b)) != -1)
{
fos.write(b, 0, len);
fos.flush();
}
fos.close();
fis.close();
}
C. Java中怎麼將前台導出的文件存到當前項目目錄的文件夾下
request.getSession().getServletContext().getRealPath("/")項目根目錄,再加上你要保存的文件夾.
D. 如何將java中生成的文件放到指定文件夾
首先獲得FileOutput對象時,寫入具體的目錄就可以了。
比如:你要寫入到D:\java\test目錄下。
方法一:
Java代碼
String
name
=
"out.html";
String
dir
=
"D:\\java\\test";
File
file
=
new
File(dir,name);
FileOutputStream
out
=
new
FileOutputStream(file);
方法二:
Java代碼
FileOutputStream
out
=
new
FileOutputStream(dir+"\\"+name);
E. 在java中,如何將內存中的數據結構直接導出到一個文件中
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
public class test {
public static void main(String[] args) throws Exception {
Map map = new HashMap();
map.put("1", "test1");
map.put("2", "test2");
map.put("3", "test3");
map.put("4", "test4");
//寫對象
FileOutputStream fos = new FileOutputStream(new File("test.txt"));//聲明要寫入的文件
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(map);
//讀對象
FileInputStream fis = new FileInputStream(new File("test.txt"));
ObjectInputStream in = new ObjectInputStream(fis);
HashMap target = (HashMap)in.readObject();
Set set = target.keySet();
for(Object obj: set){
String key = (String)obj;
System.out.println(key+"-"+target.get(key));
}
}
}
F. Java請問如何輸出jar內部文件到指定文件夾
jar本質上是一個zip文件,可以使用apache commons-compress包來操作它,遍歷壓縮包中的所有文件,找到需要的文件並以文件流的形式寫到指定的外部文件中。用法可以自己搜索。
G. 在java中如何把一個文件放到指定的文件夾中
首先獲得fileoutput對象時,寫入具體的目錄就可以了。
比如:你要寫入到d:\java\test目錄下。
方法一:
java代碼
string
name
=
"out.html";
string
dir
=
"d:\\java\\test";
file
file
=
new
file(dir,name);
fileoutputstream
out
=
new
fileoutputstream(file);
方法二:
java代碼
fileoutputstream
out
=
new
fileoutputstream(dir+"\\"+name);
H. 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("\\\\", "\\\\\\\\");
}
}
I. java如何導出csv文件 用戶點擊導出可以導出到他想保存的地方
這種通常是如下做法:
1:提供查詢頁面,讓用戶輸入查詢條件
2:根據查詢條件到資料庫去檢索,並獲取到對應的記錄
3:生成csv到本地(可以省略,生成臨時文件到tomcat的臨時目錄)
4:用讀取文件,用response寫流到客戶端
J. java mysqlmp導出到對應文件夾下,程序執行未報錯,但是對應目錄下為空
把Process的結果讀出來,才能看到是否導出錯誤,執行語句成功,不等於導出成功。