Read file in java linux? [duplicate] - java

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");
}

Related

java.io.FileNotFoundException getting path to file in java [duplicate]

This question already has answers here:
Jar can't access txt files
(2 answers)
Java JAR can't find file
(1 answer)
Closed 2 years ago.
I have the following code for check if a file exists on some path, if not, that file is copy from the project, and it works.
public static void verificarDB() throws IOException {
File cygnus_db = new File(System.getProperty("user.home")+File.separator+".local"+File.separator+"cygnus"+File.separator+"cygnus_db_local.db");
if(!cygnus_db.exists()) {
File source = new File("src/assets/bd/cygnus_db_local.db");
File target = new File(System.getProperty("user.home")+File.separator+".local"+File.separator+"cygnus"+File.separator+"cygnus_db_local.db");
FileUtils.copyFile(source, target);
}
}
But when i export my project to a runnable jar i get this exception:
java.io.FileNotFoundException: Source 'src/assets/bd/cygnus_db_local.db' does not exist
at org.apache.commons.io.FileUtils.checkFileRequirements(FileUtils.java:1383)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1060)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1028)
at pruebas.Main.verificarDB(Main.java:123)
at pruebas.Main.main(Main.java:30)
What im doing wrong?

How to check if file exists knowing only file name? [duplicate]

This question already has answers here:
How do I check if a file exists in Java?
(19 answers)
Closed 3 years ago.
In Java using Maven project we may read file's content as a stream by knowing only file's name, for example:
InputStream in = getClass().getResourceAsStream("/" + fileName);
But is there way to check if the file exists without indicating the whole path, just passing file name?
file.exists() can be used to check whether such a file exists or not.Like following:
File file = new File("filepath");
if(file.exists()){
// Do your stuff
}

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

Write File to Desktop [duplicate]

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.

Categories

Resources