Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:44:03+00:00 2026-05-23T23:44:03+00:00

In Java I want to create a file from and save the data on

  • 0

In Java I want to create a file from and save the data on it. The File name with path is taken from user. Now if user give invalid path like C:\temp\./user\fir/st.csv which is an invalid path because "." and / are in the path and on windows operating system "\" is used as path separator.

Before executing the program(a command line tool), there was no temp folder in C:\ directory, but when I run the program it creates temp folder then in temp it creates user then in user it create fir folder and finally st.csv in it. While I want that if such type of invalid path or file name is given by the user user should be noticed by message "Invalid path or file name".

What should I do? Program code is like below:

public class FileTest {
    public static void main(String args[]) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Please enter path:");
            String path = br.readLine();
            File file = new File(path);
            String path1 = file.getParent();
            File file2 = new File(path1);
            if (!file2.exists()) {
                System.out.println("Directory does not exist , So creating directory");
                file2.mkdirs();
            }
            if (!file2.exists()) {
                System.out.println("Directory can not be created");
            } else {
                FileWriter writer = new FileWriter(file);
                PrintWriter out = new PrintWriter(writer);
                System.out.println("Please enter text to write on the file, print exit at new line to if finished");
                String line = "";
                while ((line = br.readLine()) != null) {
                    if (line.equalsIgnoreCase("exit")) {
                        System.out.println("Thanks for using our system");
                        System.exit(0);
                    } else {
                        out.println(line);
                        out.flush();
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now if I give the path as C:\tump\./user\fir/st.csv then it create tump folder in C drive , then user in tump, then fir in user folder then st.csv file in it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:44:03+00:00Added an answer on May 23, 2026 at 11:44 pm

    In my case which I required this works

    if(!path.equals(file.getCanonicalPath())){
                    System.out.println("FAILED:Either invalid filename, directory or volume label , syntax error");
                    System.exit(0);
                }
    

    By adding this code just after
    File file=new File(path);
    it will work fine and will notice the user if given path is incorrect

    As there is only two options either java will create the file on some path which will be canonical path or if not able to create the file it will give exception. So if there is any mismatch in the path given by the user and canonical path then it means user type wrong path which java can not create on the file system, so we will notice the user, or if java give exception then we can catch it and will notice the user for incorrect path

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to dynamically create some image from Java and save it to a
Possible Duplicate: File to byte[] in Java I want to read data from file
I want to create a Java application bundle for Mac without using Mac. According
From within my Java program I want to determine which .NET Framework is installed
I have a java applet that creates a JPEG file. I want to pass
I'm trying create an immutable object and initialise it from xml config file in
I have a XmlDocument in java, created with the Weblogic XmlDocument parser. I want
I wonder why would a C++, C#, Java developer want to learn a dynamic
In a Java application I want to be able to take a timestamp at
I have a little Java problem I want to translate to Python. Therefor I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.