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的结果读出来,才能看到是否导出错误,执行语句成功,不等于导出成功。