JAVA里怎么生成txt并保存到局域网内某台计算机的指定文件夹里

我只知道通过io流可以生成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("\\\\", "\\\\\\\\");
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-02-16
先保证远程计算机的目录可以写入,然后和操作本地文件没什么区别,一个示例:

try{
FileOutputStream fos = new FileOutputStream(new File("\\\\192.168.0.2\\a.txt"));
}
catch(Exception ex){
ex.printStackTrace();
}

这可以在192.168.0.2的共享根目录下创建一个a.txt文件,详细的文件操作优化方面,可以自己看看参考资料的。本回答被提问者采纳
相似回答