How can I create a file with limited privilege in Java? - 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?

Related

Extracting files from res folder in Executable JAR (txt files to be specific)

I would like to ask if its possible to put text files into my jar, I use them to make my map in my game, but users can get Highscores. now I want to save the Highscores with the map, so I have to save the map on the user their PC. Is there any way how I could do this? I've searched the internet for some ideas but I could not find anything that even came close to what I've wanted. I only had 3/4th of a year java so I don't know much about these things, everything that happens outside the debug of eclipse are problems for me(files are mainly one of those things, null exceptions, etc).
The main question now.
Is it possible to do? If yes, do you have any terms I could search on, or some sites/guides/tutorials? If no, is there any other way how I could save the highscores?
EDIT:
to make clear
Can I get the text file (the text inside the file) to be extracted to a different file in like the home directory of my game (where I save the settings and stuff) the basic maps are inside the jar file, so I want them to be extracted on the first start-up of the program
Greetings Carolien
"extracted to a different file in like the home directory of my game (where i save the settings and stuff) the basic maps are inside the jar file, so i want them to be extracted on the first startup of the program"
You can get the URL by using getClass().getResource()
URL url = getClass().getResource("/res/myfile.txt");
Then create a File object from the URI of the URL
File file = new File(url.toURI());
Then just perform your normal file operations.
if (file.renameTo(new File(System.getProperty("user.home") + "\\" + file.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
Assuming your file structure is like below, it should work fine
ProjectRoot
src
res
myfile.txt
Note: the above is moving the entire file. If you want to extract just the data inside the file, then you can simple use
InputStream is = getClass().getResourceAsStream("/res/myfile.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
The just do normal IO operation with the reader. See here for help with writing the file.

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....

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

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.

Android writing to a CSV file via OpenCsv

I try to write to a Csv file via:
mFileWriter = new FileWriter(
"/sdcard/program/file");
mCsvWriter = new CSVWriter(mFileWriter);
At the moment it throws an exception that the file doesn't exist.
It's true that the file doesn't exist. What's the easiest way to create the file?
Does the FILE not exist, or the DIRECTORY it's supposed to go into?
If you want to create a directory structure, you can always do
File file = new File("/full/path/to/file");
file.mkdirs();
This will create any path leading up to this file that doesn't exist yet.
I suppose the missing quotes around your file name are a typo?

Java File manipulation

So I have an application with a JFileChooser from which I select a file to read. Then I change some words and write a new file. The problem that I am having is that when I write the new file it's saved in the project directory. How do I save it in the same directory as the file that I chose using the JFileChooser. Note: I don't want to use the JFileChooser to choose the location. I just need to save the file in the same directory as the original file that I read.
You choose a file like this:
File fileToRead = JFileChooser.getSelectedFile();
Then you read and change the content and write it back to the same location with a different name:
File fileToWrite = new File( fileToRead.getParent(), "newName.txt" );

Categories

Resources