如何在自定义路径上保存txt

时间:2016-04-10 20:07:38

标签: java path

我有一个保存文本文件的代码,但该文本文件的路径和名称是硬编码的。

如何让用户选择他/她自己的路径和文件名?

                int[][] sudokuNumbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    try (
            PrintStream output = new PrintStream(new File("C:\\Users\\David\\Desktop\\Proyecto4.3\\output.txt"));) {
        for (int i = 0; i < sudokuNumbers.length; i++) {
            String s= "";
            for (int j = 0; j < sudokuNumbers[i].length; j++) {
                s+= "|" + sudokuNumbers[i][j] + "|";
            }
            output.println(s);
        }
        output.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

这项任务相对容易实施,但您需要几种方法来帮助您。如果用户要提供自定义路径(特定于他们自己),则需要确保在创建和写入文件之前创建路径以使其存在。另一件需要考虑的事情是,可能还需要授予应用程序权限,以便在本地文件系统中执行任务(如果这是创建文件的位置)或其他任何地方。

显然,您需要为用户提供一种提供路径和文件名的方法。在我看来,最好的方法是使用Folder Chooser对话框。我说这个的原因是因为它不像用户完全键入所需的路径和文件名那样容易出错(拼写,系统格式等)。基本上,您将用户提供给本地文件系统的控制越少,您的应用程序就越好。基于预设文件系统位置内的用户名创建文件夹(目录)并使用文件名的用户名和日期/时间是相对常见的,并且可以全部自动生成,这消除了任何文件系统错误的可能性。如果可能,您的应用程序应该保持对此类任务的完全控制,但可以理解的是,对于特定应用程序而言并非总是如此,因为用户与文件系统的交互是必需的,因为它的唯一功能是基于这种互动。例如,文件系统资源管理器可以创建,剪切,复制,粘贴,删除和编辑文件和文件夹。

为了演示我正在谈论的内容,我已经快速创建了一个小型Java可运行程序,您可以使用它来查看提供的自定义方法的工作原理。代码评论很好,以便您可以了解正在发生的事情。不幸的是,所有评论都会使代码文件看起来很大,而实际上如果您要将其删除则不会。

为简单起见,当我们运行提供的小型控制台应用程序时,它首先要求输入用户名。此用户名将用于两个特定目的。

  

1)它将仅用于为特定用户创建特定文件夹。

     

2)保存文件时,文件名将以用户名开头,然后跟随日期/时间数据,最后包含   &#34; .TXT&#34;文件扩展名。

同样,仅作为示例,一旦用户将他/她的名字输入控制台,将显示“文件夹选择”对话框窗口,以便可以选择主文件夹位置,其中将创建用户子文件夹和最终用户数据文件将保存在他/她的文件夹名称下。

选择主文件夹后,文件数据会自动保存,完成后控制台会显示成功。然后我们的小控制台应用程序就结束了。

这是Java runnable:

package choosefolder;

import java.awt.Component;
import java.awt.Container;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.time.LocalDateTime;
import java.util.Scanner;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;

public class ChooseFolder {
    // Declare & initialize required String variables.
    // These are global to the entire Class. 
    private static String userName = "";
    private static String savePath = "";

    public static void main(String[] args) {
        //Setup a Scanner to get User input from console.
        Scanner input = new Scanner(System.in);

        // Ask for and get the Users Name and convert it to lowercase
        // so as to maintain nameing consistency. No sense having "John Doe"
        // one day and "john Doe" the next. This would consider 2 different 
        // Users since the second name has a lowercase "j" in "John" and 
        // therefore create two separate folder. Converting all supplied names
        // to lowercase eliminates this problem.
        while (userName.equals("")) {
            System.out.println("\nPlease Enter User Name:");
            userName = input.nextLine().toLowerCase();
        }

        // Open a Folders selection dialog window and allow for
        // selection of a home folder where all Users saved files 
        // will be saved. Once we get the selected folder we then
        // add the Users name to the path and create it.
        savePath = openFoldersDialog("", "Select Home Folder To Save User Folders & File In...") + "\\" + userName;

        // Create a special path just for the Users Name (ie: C:\My Files\John Doe\).
        if (!createPath(savePath)) {
            // If false was returnd from the createPath() method
            // then there was an error while creating the path.
            // Read console output to get the Error.
            System.out.println("\n\u001B[31mError creating Users Save Path! Exiting...\u001B[39;49m");
            System.exit(0);
        }

        // Get the local date & time so we can add it to 
        // our file namewhen its created.
        LocalDateTime dateTime = LocalDateTime.now();

        // Create the new file path & file name. This will be the path
        // and file name where our Users data will be saved. We take the 
        // Users path e created and then add the file name to the end. The
        // file name consists of the supplied User's name, the date/time and
        // the .txt file name extension. Unwanted characters are also removed
        // from the Date/Time. So, our path may look something like: 
        //       C:\My Files\John Doe-20160419T155758107.txt
        String filePathAndName = savePath + "\\" + userName + "-" + 
                                 dateTime.toString().replace("-", "").
                                 replace(":", "").replace(".", "") + ".txt";

        // Here is your code. This is the data saved to file....
        int[][] sudokuNumbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        try (
            // Notice the filePathAndName variable which we declared and initialized above.
            PrintStream output = new PrintStream(new File(filePathAndName));) {
            for (int i = 0; i < sudokuNumbers.length; i++) {
                String s= "";
                for (int j = 0; j < sudokuNumbers[i].length; j++) {
                    s+= "|" + sudokuNumbers[i][j] + "|";
                }
                output.println(s);
            }
            output.close();
        } 
        catch (FileNotFoundException e) { 
            System.out.println(e.getMessage()); 
        }
        finally {
            // Notify in console of success.
            System.out.println("\n\u001B[34mFile Saved SUCCESSFULLY in:\n\u001B[39;49m" + filePathAndName);
        }
    }

    /**
     * Opens a Folder (directory) selection dialog window. All parameters for this method are optional but if you supply 
     * something to the last parameter then you must supply something to the parameters preceding it even if it's a null
     * string ("") (see usage examples below)
     * 
     * Example Usage:
     * 
     *          openFolderDialog();                                  //Allowed
     *          openFolderDialog("C:\\");                            //Allowed
     *          openFolderDialog("", "Select The Folder You Need");  //Allowed
     *          openFolderDialog("", "", "Select Folder");           //Allowed
     *          openFolderDialog("", "", "");                        //Allowed
     *          openFolderDialog( , "Your Folders, "Select Folder"); //Not Allowed
     *          openFolderDialog( , , "Select Folder");              //Not Allowed
     *          openFolderDialog("C:\\", "Your Folders, );           //Not Allowed
     * 
     * I think you get the idea here.
     * 
     * @param dialogOptions OPTIONAL - 3 String Type Parameters
     *    startPath           - String - Default is the running applications' home folder. The path
     *                          supplied here tells the dialog where to start displaying folders from.
     *                          If you just want to use default but would like to supply something to
     *                          the other optional parameters then just supply a null string ("") here.
     * 
     *    dialogTitle         - String - Dialog Window Title - Default is "Select Folder...". You can 
     *                          supply whatever title you like for the dialog window.
     * 
     *    openButtonCaption - String - The Select Button Caption - Default is "Select". You can supply 
     *                          whatever you like for a button caption and it can be more than one word
     *                          but do keep in mind that the button increases in length to accommodate the
     *                          the supplied string.
     * 
     * @return The selected Folder (directory) full path as string. Returns Null is nothing was selected.
     */
    public static String openFoldersDialog(String... dialogOptions) {
        String startPath = "";
        try {
            startPath = new File(".").getCanonicalPath();
        } catch (IOException ex) {}

        String dialogTitle = "Select Folder...";
        String openButtonCaption = "Select";

        if (dialogOptions.length != 0) { 
            if (dialogOptions.length >= 1) { 
                if (!dialogOptions[0].isEmpty()) { startPath = dialogOptions[0]; } 
            }
            if (dialogOptions.length >= 2) { 
                if (!dialogOptions[1].isEmpty()) { dialogTitle = dialogOptions[1]; } 
            }
            if (dialogOptions.length == 3) { 
                if (!dialogOptions[2].isEmpty()) { openButtonCaption = dialogOptions[2]; } 
            }
        }

        File sp = new File(startPath);
        JFileChooser fc = new JFileChooser(sp);

        //Remove the "Files Type:" combo box and label from dialog.
        removeJFCFilesTypeComponent(fc);

        fc.setDialogTitle(dialogTitle);
        fc.setAcceptAllFileFilterUsed(false);
        fc.setRequestFocusEnabled(true);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int userSelection = fc.showDialog(null, openButtonCaption);  //.showSaveDialog(null);
        if (userSelection == JFileChooser.APPROVE_OPTION) {
            String f = fc.getSelectedFile().getPath();
            return f;
        }
        return null;
    }   

    /**
     * Removes the Filter ComboBox from the supplied JFileChooser object.
     * This helps make the JFileChooser dialog window look more like a 
     * folder selection dialog.
     * @param filechooser The object name of the JFileChooser object.
     */
    public static void removeJFCFilesTypeComponent(Container filechooser) {
        Component[] components = filechooser.getComponents();
        for (Component component : components) {
            if (component instanceof JComboBox) {
                Object sel = ((JComboBox) component).getSelectedItem();
                if (sel.toString().contains("AcceptAllFileFilter")) {
                    component.setVisible(false);
                    // OR
                    //con.remove(component);
                }
            }
            if (component instanceof JLabel) {
                String text = ((JLabel) component).getText();
                if (text.equals("Files of Type:")) {
                    component.setVisible(false);
                    // OR
                    //con.remove(component);
                }
            }
            if (component instanceof Container) {
                removeJFCFilesTypeComponent((Container) component);
            }
        }
    }

    /**
     * Creates the supplied path into the local file system if it doesn't already exist. You must 
     * make sure permissions are properly in place before using this method otherwise the path may
     * not be created within the file system.
     * @param pathString The path to create (must not include a file name). Will not create path if it already exists.
     * @return Boolean True if successful and Boolean False if not.
     */
    public static boolean createPath(String pathString) {
        if (pathString.isEmpty()) { return false; }
        if (doesFolderExist(pathString)) { return true; }

        try {
            File foldersToMake = new File(pathString);
            foldersToMake.mkdirs();
            return true;
        }
        catch (Exception ex) { 
            System.out.println("*** CreatePath() - Exception Encountered:\n*** " + ex.getMessage()); 
            return false; 
        }
    }

    /**
     * This method simply return boolean true or false if the supplied path exists.
     * @param folderPathString (String) The full path to check. No File Name is allowed 
     * to be in this path.
     * @return
     */
    public static boolean doesFolderExist(String folderPathString) {
        boolean tf = false;
        try {
            if (folderPathString.isEmpty() || "".equals(folderPathString)) { return tf; }
            File f = new File(folderPathString);
            if(f.exists() && f.isDirectory()) { tf = true; }
        }
        catch (Exception e) { 
            System.out.println("*** DoesFolderExist() - Exception Encountered:\n*** " + e.getMessage());
            tf = false; 
        }
        return tf;
    }

}

我希望这可以帮助你(和其他人)。