Write File to Desktop [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In java under Windows, how do I find a redirected Desktop folder?
How to get the Desktop path in java
I want to write my results to the desktop of the user rather than to the same directory as file class that I am running.
I am using Mac OS.. How about in Window?1
Thanks

The user's home directory is:
System.getProperty("user.home")
In general +"/Desktop" would do, but is not portable.
String userHomeFolder = System.getProperty("user.home");
File textFile = new File(userHomeFolder, "mytext.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
try {
...
} finally {
out.close();
}
This would write the file "mytext.txt" to the home directory.

Related

regarding java file system? [duplicate]

This question already has answers here:
Find place for dedicated application folder
(2 answers)
Closed 11 months ago.
I am new to the java file system.I store the serializable object in some file location for example d:\my files\file.txt(location is hardcoded).what should I do so that it will work on all the platforms(Linux, Windows, and UNIX).Thanks in advance
If you are trying to save files related to the program, you could use a relative path and '/' should work as a path separator across all platforms.
If for some reason you definitely want absolute paths, you'll need to detect system os using System.getProperty("os.name") refer https://www.baeldung.com/java-detect-os and you can store os specific hard coded paths like
HashMap<String, String> osPathMap= new HashMap<>();
osPathMap.put("Windows 10", "d:\my files\file.txt");
osPathMap.put("Linux", "Some linux path");
and get the path using osPathMap.get(os_name)
You can use System.getProperty with the parameters : user.dir or user.home , they give you the working directory path and the user home directory path on String format independently with the OS :
public static void main(String[] args) {
// print the path of the working directory
System.out.println(System.getProperty("user.dir"));
// print the path of the user directory
System.out.println(System.getProperty("user.home"));
}

Read file in java linux? [duplicate]

This question already has answers here:
How to handle ~ in file paths
(5 answers)
Closed 3 years ago.
File f = new File("~/NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
cat ~/NetBeansProjects/ChatApp/src/chatapp/Server.java, prints the content of the file.
But the above program doesn't print "File exist".
The ~ is resolved by the shell, whereas Java do not resolve it. Try something like this:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
The "home" wildcard (~) cannot be resolved in the JVM. You need to load that property via the Java API:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}

How do I batch rename files in java? [duplicate]

This question already has answers here:
How do I rename (not move) a file in Java 7?
(6 answers)
Closed 8 years ago.
I've got a list of files:
"f1.txt"
"f2.txt"
"f3.txt"
"f4.txt"
and want to batch rename to:
"file1.txt"
"file2.txt"
"file3.txt"
"file4.txt"
Ideally I would like to do this as a little java program but don't mind it being done in something like Windows PowerShell.
Thanks in advance
Lee
Just buil a function that get two parametrs oldname,and newname and put it inside
// File with old name
File file = new File("oldname");
// File with new name
File file2 = new File("newname");
if(file2.exists()) throw new java.io.IOException("file exists");
// Rename file
boolean success = file.renameTo(file2);
if (!success) {
}
java.io.FileWriter out= new java.io.FileWriter(file2, true );//append=yes
Powershell, will rename all files in $path:
$path = "C:\pathToFiles"
cd $path
ls | % { Rename-Item $_.Name $_.Name.replace("f","file") }

How do you open a program by using ActionListener in Java? [duplicate]

This question already has answers here:
Open excel document in java
(2 answers)
Using Java, how do I cause Word to open and edit a file? [duplicate]
(2 answers)
Closed 8 years ago.
I am busy creating a GUI in Java with buttons such that if I press a button it must open another program like Excel or Word and also a folder. Is there anyway that this is possible?
You would do something like this...
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(new File("c:\\a.doc"));
}
If they have word then this file will open in word.
Try the below code. Replace the string notepad with your program name with it's path.
To open a folder use "explorer c:\\z" in the place of notepad string to open folder z for Windows OS. Use "nautilus /directory" to open directory for Linux OS.
try{
java.lang.Runtime.getRuntime().exec("notepad");
}
catch(Exception e){
System.err.println(e.getMessage());
}

How to create a file & folder in Java? [duplicate]

This question already has answers here:
create a text file in a folder
(4 answers)
Closed 9 years ago.
Okay, updated this right now
As of now I have this:
File saveGame = new File(Config.saveDir,Config.saveName);
Now how would I create that into a text file? My saveDir has already been created (mkDir), and my saveName is defined as "xyz.txt", so what method do I use to create this file (and later add text into it)?
new File(...) doesn't crete a file on disk, it only creates a path for a Java program to use to refer to a file that may or may not exist on disk (hence the File.exists() method).
Try userFile.createNewFile(); to actually create the file on disk.
To make the directory you would need to use the File.mkdirs() method, but don't call it on userFile or it will make a directory with the Savegame.txt in it.
Edit:
File dir = new File(Config.userpath + "/test/");
File file = new File(dir, "," + Config.name + " Savegame.txt");
dir.mkdir(); // should check to see if it succeeds (if(dir.mkdir())...)
file.createNewFile(); // should also check that this succeeds.

Categories

Resources