Specifying file location for PrintWriter class (Java) and automatically appending .txt - java

so let's say I ask the user to specify what he wants to call a new file
System.out.println("What do you want to call the file?");
String outputFile = keyboard.nextLine();
now to write the file I would do:
PrintWriter outputFile = new PrintWriter(fileName);
My question are:
I know by default it saves to the local folder. How do I make it so that it will save it to the users desktop?
How do I automatically append .txt to his given file name so he doesn't have to do it?

You have to know the user home. It can vary with the OS (and the user can sometimes define its own), so the best way to be sure is to ask directly the user. You could also keep a list of "default desktop paths".
if(!fileName.endsWith(".txt")) fileName = fileName+".txt";
Resources:
String.endsWith()

If you're going to ask the user where to put the file, you should probably start with the directory that is given by the system property "user.home", i.e. call System.getProperty("user.home");
Then you could show a list of directories and ask the user to choose one, drilling down until the user is at the directory he wants to use. On Windows machines, the "Desktop" directory is in fact immediately under the user's home directory.

Related

Access file without knowing absolute path, only knowing file name

I'm trying to use a file in my code but I don't want to have specify the absolute file path, only the file name, for example "fileName.txt".
I want to do this so I have the ability to use this code on different laptops where the file may be stored in different folders.
The code below is what I'm using at the moment but I receive a NoSuchFileException when I ran it.
FileSystem fs FileSystems.getDefault();
Path fileIn = Paths.get("fileName.txt");
Any ideas how to overcome this problem so I can find the file without knowing its absolute path?
Ideas on how to find the file without knowing its absolute path:
Instruct the user of the app to place the file in the working directory.
Instruct the user of the app to give the path to the file as a program argument, then change the program to use the argument.
Have the program read a configuration file, found using options 1 or 2, then instruct the user of the app to give the path to the file in the configuration file.
Prompt the user for the file name.
(Not recommended) Scan the entire file system for the file, making sure there is only one file with the given name. Optional: If more than one file is found, prompt the user for which file to use.
if you don't ask the user for the complete path, and you don't have a specific folder that it must be in, then your only choice is to search for it.
Start with a rootmost path. Learn to use the File class. Then search all the children. This implementation only returned the first file found with that name.
public File findFile(File folder, String fileName) {
File fullPath = new File(folder,fileName);
if (fullPath.exists()) {
return fullPath;
}
for (File child : folder.listFiles()) {
if (child.isDirectory()) {
File possible = findFile(child,fileName);
if (possible!=null) {
return possible;
}
}
}
return null;
}
Then start this by calling either the root of the file system, or the configured rootmost path that you want to search
File userFile = findFile( new File("/"), fileName );
the best option, however, is to make the user input the entire path. There are nice file system browsing tools for most environments that will do this for the user.

How can I create a file with limited privilege in Java?

I want to create a File using Java, that allows the program to modify the contents, however any external user should not be able to go to the path and manually delete or update that file. In other words I want to revoke privilege from users to be able to delete/update that file.
For example:
path = "C:\\newfile.txt"
File file = new File(path);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("hello world");
The above code allows the program to write hello world to the file.
I want to make sure that no one can delete the file manually from "C:\". How do I do that?

Java File - Append pre-defined filename to a user-defined directory

I have a program that creates multiple output files e.g. daily_results.txt, overall_results.txt etc.
I want to allow the user to specify the directory that these files will be saved to using JFileChooser.
So if the user selected the directory they wanted their output to be saved to as "C:\temp\". What is the best way to append daily_results.txt to that file object. Is there a more elegant way to do this other than:
File file = new File(userDirectory.getPath() + "daily_results.txt");
Any ideas?
Apologies!
I think this can quite easily be accomplished with the JFileChoosers setSelectedFile method.

How do I set the path from user input for a new process using ProcessBulder?

I want to add new process using ProcessBulder class in Java. But process name is taken from user input then how to set the path?
The user only enter the process name like firefox.exe, but it does not start directly, it throws PathNotFoundException. How can I identify the path of user's input? Or how should I start the process which entered by the user?
It should be either full path or the program should be in a folder known to OS via PATH variable, or the program should be in working dir

Save my Unique ID (String) and retrieving it later when I re-launch my Java application

I want to save a Unique ID (which is a String) which gets created when I launch my Java application. Now I want to save this somewhere (I think in some file on the disk) so that when I relaunch my application I should be able to read it and use that ID.
I want to know what is the good way to saving such ID. I am thinking of creating a Properties file and save it then retrieve it from it when I relaunch application. Is there a better or standard way for this?
EDIT :
Additionally what should be the folder location for storing on the disk. Should it be relative to my execution path or some Logged-in user specific path?
1. If its the same Java application that writes or reads this String, then use Serialization, it will be in non-readable form when saved.
2. If reading and writing is from different program, then use Text file.
3. Using Property file will be also a good approach.
If your app/program needs to store more data at some point sqlite3 might be the best option for you. It is easy to implement and use.
Download sqlite3
EDIT: How many IDs will be stored in the app? If there are just a few, a textfile or property file is enough.
EDIT2: Navigate to your Documents folder on your machine and you will see folders of programs/games. Thats where you should place the file/db. However you can also store it in the installation path on your hard drive. Also make sure your user launches the app trough a shortcut, not the actual execution file
Use the FileWriter and File classes from Java.
It should be something like that:
File f = new File(your path here);
if (f.exists()){
BufferedReader br = new BufferedReader(new FileReader(your path here));
String a = br.readLine();
br.close();
}else{
FileWriter fw = new FileWriter(your path here);
fw.write(your ID String);
fw.flush();
fw.close();
I hope this is want u meant.
Best regards
edit: just noticed too late that your edited your post....

Categories

Resources