Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以让我的用户自己命名文本文件吗?_Java - Fatal编程技术网

Java 我可以让我的用户自己命名文本文件吗?

Java 我可以让我的用户自己命名文本文件吗?,java,Java,通常,我们编写file writer命令时会为用户提供文件路径和文本文件的名称。 示例:创建分配文本文件 FileWriter writer = new FileWriter(".......\assignment"); 但是有没有可能让我的用户自己给文本文件命名呢 因为我的程序需要讲师为作业输入有关键读取的书目详细信息,所以讲师可以使用作业名称命名文本文件。使用 public static void main(String[] args) { System.out.println("

通常,我们编写file writer命令时会为用户提供文件路径和文本文件的名称。 示例:创建分配文本文件

FileWriter writer = new FileWriter(".......\assignment");
但是有没有可能让我的用户自己给文本文件命名呢

因为我的程序需要讲师为作业输入有关键读取的书目详细信息,所以讲师可以使用作业名称命名文本文件。

使用

public static void main(String[] args) {
    System.out.println("Please enter a file name: ");
    Scanner scan = new Scanner(System.in);
    String str = scan.next();
    try (FileWriter writer = new FileWriter(str)) {

    } catch (IOException e) {
        e.printStackTrace();
    }
}
编辑

要使用用户主目录中的文件,可以使用

try (FileWriter writer = new FileWriter(new File(
        System.getProperty("user.home"), str))) {

} catch (IOException e) {
    e.printStackTrace();
}
您可以使用以下命令将用户提供的
字符串
传递给构造函数:

public static void main(String[] args) {
    System.out.println("Please enter a file name: ");
    Scanner scan = new Scanner(System.in);
    String str = scan.next();
    try (FileWriter writer = new FileWriter(str)) {

    } catch (IOException e) {
        e.printStackTrace();
    }
}
编辑

要使用用户主目录中的文件,可以使用

try (FileWriter writer = new FileWriter(new File(
        System.getProperty("user.home"), str))) {

} catch (IOException e) {
    e.printStackTrace();
}
您可以使用以下命令将用户提供的
字符串
传递给构造函数:

public static void main(String[] args) {
    System.out.println("Please enter a file name: ");
    Scanner scan = new Scanner(System.in);
    String str = scan.next();
    try (FileWriter writer = new FileWriter(str)) {

    } catch (IOException e) {
        e.printStackTrace();
    }
}
编辑

要使用用户主目录中的文件,可以使用

try (FileWriter writer = new FileWriter(new File(
        System.getProperty("user.home"), str))) {

} catch (IOException e) {
    e.printStackTrace();
}
您可以使用以下命令将用户提供的
字符串
传递给构造函数:

public static void main(String[] args) {
    System.out.println("Please enter a file name: ");
    Scanner scan = new Scanner(System.in);
    String str = scan.next();
    try (FileWriter writer = new FileWriter(str)) {

    } catch (IOException e) {
        e.printStackTrace();
    }
}
编辑

要使用用户主目录中的文件,可以使用

try (FileWriter writer = new FileWriter(new File(
        System.getProperty("user.home"), str))) {

} catch (IOException e) {
    e.printStackTrace();
}

您可以使用
Scanner
从键盘获取文件名。如果使用命令提示符获取文件名,则可以使用以下代码-

Scanner input = new Scanner(System.in);
String fileName = input.next();   
然后将
'fileName'
传递给
PrintWriter
构造函数

PrintWriter writer = new PrintWriter(fileName);
writer.close();

您可以使用
Scanner
从键盘获取文件名。如果使用命令提示符获取文件名,则可以使用以下代码-

Scanner input = new Scanner(System.in);
String fileName = input.next();   
然后将
'fileName'
传递给
PrintWriter
构造函数

PrintWriter writer = new PrintWriter(fileName);
writer.close();

您可以使用
Scanner
从键盘获取文件名。如果使用命令提示符获取文件名,则可以使用以下代码-

Scanner input = new Scanner(System.in);
String fileName = input.next();   
然后将
'fileName'
传递给
PrintWriter
构造函数

PrintWriter writer = new PrintWriter(fileName);
writer.close();

您可以使用
Scanner
从键盘获取文件名。如果使用命令提示符获取文件名,则可以使用以下代码-

Scanner input = new Scanner(System.in);
String fileName = input.next();   
然后将
'fileName'
传递给
PrintWriter
构造函数

PrintWriter writer = new PrintWriter(fileName);
writer.close();

我猜你写的是一个控制台应用程序,而不是GUI应用程序

在这种情况下,请使用以下内容:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileChooser {

    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter(chooseFile());
        // Use your FileWriter
    }

    public static File chooseFile() {
        String fname = null;
        File file = null;

        System.out.println("Please choose file name:");
        while (true) {
            try (Scanner in = new Scanner(System.in)) {
                // Reads a single line from the console
                fname = in.nextLine();
                file = new File(fname);
                if (!file.createNewFile()) {
                    throw new RuntimeException("File already exist");
                }
                break;
            } catch (Exception ex) {
                System.out.println(ex.getMessage() + ", please try again:");
            }
        }

        return file;
    }
}
编辑

如果您正在编写
Swing
GUI,则可以使用:


我猜你写的是一个控制台应用程序,而不是GUI应用程序

在这种情况下,请使用以下内容:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileChooser {

    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter(chooseFile());
        // Use your FileWriter
    }

    public static File chooseFile() {
        String fname = null;
        File file = null;

        System.out.println("Please choose file name:");
        while (true) {
            try (Scanner in = new Scanner(System.in)) {
                // Reads a single line from the console
                fname = in.nextLine();
                file = new File(fname);
                if (!file.createNewFile()) {
                    throw new RuntimeException("File already exist");
                }
                break;
            } catch (Exception ex) {
                System.out.println(ex.getMessage() + ", please try again:");
            }
        }

        return file;
    }
}
编辑

如果您正在编写
Swing
GUI,则可以使用:


我猜你写的是一个控制台应用程序,而不是GUI应用程序

在这种情况下,请使用以下内容:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileChooser {

    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter(chooseFile());
        // Use your FileWriter
    }

    public static File chooseFile() {
        String fname = null;
        File file = null;

        System.out.println("Please choose file name:");
        while (true) {
            try (Scanner in = new Scanner(System.in)) {
                // Reads a single line from the console
                fname = in.nextLine();
                file = new File(fname);
                if (!file.createNewFile()) {
                    throw new RuntimeException("File already exist");
                }
                break;
            } catch (Exception ex) {
                System.out.println(ex.getMessage() + ", please try again:");
            }
        }

        return file;
    }
}
编辑

如果您正在编写
Swing
GUI,则可以使用:


我猜你写的是一个控制台应用程序,而不是GUI应用程序

在这种情况下,请使用以下内容:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileChooser {

    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter(chooseFile());
        // Use your FileWriter
    }

    public static File chooseFile() {
        String fname = null;
        File file = null;

        System.out.println("Please choose file name:");
        while (true) {
            try (Scanner in = new Scanner(System.in)) {
                // Reads a single line from the console
                fname = in.nextLine();
                file = new File(fname);
                if (!file.createNewFile()) {
                    throw new RuntimeException("File already exist");
                }
                break;
            } catch (Exception ex) {
                System.out.println(ex.getMessage() + ", please try again:");
            }
        }

        return file;
    }
}
编辑

如果您正在编写
Swing
GUI,则可以使用:




对提示用户输入名称。将该
String
传递给
FileWriter
构造函数。可以演示如何将该字符串传递给FileWriter构造函数吗?@user3539809简单的谷歌搜索将向您展示许多示例。比如说,是的。提示用户输入名称。将该
String
传递给
FileWriter
构造函数。可以演示如何将该字符串传递给FileWriter构造函数吗?@user3539809简单的谷歌搜索将向您展示许多示例。比如说,是的。提示用户输入名称。将该
String
传递给
FileWriter
构造函数。可以演示如何将该字符串传递给FileWriter构造函数吗?@user3539809简单的谷歌搜索将向您展示许多示例。比如说,是的。提示用户输入名称。将该
String
传递给
FileWriter
构造函数。可以演示如何将该字符串传递给FileWriter构造函数吗?@user3539809简单的谷歌搜索将向您展示许多示例。例如,thx表示答复,但我是否允许用户仅命名文本文件,但使用默认文件路径?@user3539809编辑以给出一个相对于用户主页的默认路径示例。您可以使用其他内容作为base.thx进行回复,但我可以允许用户仅使用默认文件路径命名文本文件吗?@user3539809已编辑,以提供一个相对于用户主页的默认路径示例。您可以使用其他内容作为base.thx进行回复,但我可以允许用户仅使用默认文件路径命名文本文件吗?@user3539809已编辑,以提供一个相对于用户主页的默认路径示例。您可以使用其他内容作为base.thx进行回复,但我可以允许用户仅使用默认文件路径命名文本文件吗?@user3539809已编辑,以提供一个相对于用户主页的默认路径示例。你可以使用其他东西作为基础。我正在用netbean~XD编写gui应用程序,我不确定你为什么接受@Elliott Frisch的答案,如果你……只是一个错误,因为我看到了它的逻辑。。。你能告诉我如何使用gui吗?我所做的是String name=jTextField1.getText();FileWriter writer=新的FileWriter(名称);但我希望所有用户的文件路径都相同,这意味着我只允许用户更改名称它是
swing
GUI吗?或者AWT?swing GUI,我创建了一个文本字段,让用户输入文件名。除此之外,我还创建了一个按钮来执行iti,即使用netbean~XD编写gui应用程序。我不确定您为什么会接受@Elliott Frisch的答案,如果您……只是一个错误,因为我看到的是逻辑。。。你能告诉我如何使用gui吗?我所做的是String name=jTextField1.getText();FileWriter writer=新的FileWriter(名称);但我希望所有使用的文件路径都相同