可以使用Java中的File类,通过用户输入获取本地目录。

示例代码:

import java.io.*;
import java.util.Scanner;

public class ReadFileFromServerToLocal {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入本地目录:");
        String localPath = scanner.nextLine();

        File localDir = new File(localPath);
        if (!localDir.exists()) {
            localDir.mkdirs();
        }

        String serverFilePath = "/path/to/file.txt";
        try {
            FileInputStream fis = new FileInputStream(serverFilePath);
            FileOutputStream fos = new FileOutputStream(localDir.getAbsolutePath() + File.separator + "file.txt");

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fis.close();
            fos.close();

            System.out.println("文件已下载到本地目录:" + localDir.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

在运行程序时,会提示用户输入本地目录。程序会根据用户输入的目录创建文件夹,然后将服务器上的文件下载到该目录下。

注意:需要替换serverFilePath变量的值为实际的文件路径

java读取文件服务器上的文件到本地文件本地目录怎么用java写一个而不是写死

原文地址: https://www.cveoy.top/t/topic/cSwt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录