• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating file or folder but using user input for name problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi new to java, and java ranch,

File dir = new File("file"); //creates folder named file
File dir1 = new File("file.txt"); //creates file

but I want to take in a file name from user e.g.

BufferedReader br = new BufferedReader(is);
String filename = br.readLine( );
File dir = new File(filename + ".txt");

its expecting a string literal is there a way round it to take a string???
 
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Java and the ranch.

The File constructor is not expecting a string literal. When you did new File("file"), "file" was converted into a String. The File constructor taking a String is File(String pathname).

Have you looked at filename + ".txt" to see if it is what you expect?

What is the error that makes you think File is expecting a string literal?
 
Conn Osullivan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I try

BufferedReader br = new BufferedReader(is);
String filename = br.readLine( );
File dir = new File(filename);

or

BufferedReader br1 = new BufferedReader(is);
String fileName = br1.readLine( );
File file = new File(fileName + ".txt");

nothing created but


File dir = new File("file"); //creates folder named file
File dir1 = new File("file.txt"); //creates file ???

 
Jacob Anawalt
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should look at filename + ".txt" to see that it is what you expected. Maybe you are creating a file named .txt.

If you don't have a debugger to step through the code with, try printing:



Maybe that's not the problem though and it's somewhere else because you're mis-understanding when the file is really created. For example File dir = new File("file") doesn't create a file or a directory. It creates a new File instance by converting the given pathname String into an abstract pathname. If you then do dir.mkdir(), a directory is created. If you instead did dir.createNewFile() it would have created an empty file named file, not a directory, as long as a file or directory by that name did not already exist.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

The File class is called “legacy” by the Java Tutorials. Go through that section and it should give you the answers.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic